Docker deploy your first application

Previous, you have completed the Docker Desktop installed and enabled Kubernetes, we can now move the container deployment application. In general, the development of workflow includes the following:

1, first create Docker mirror, a single container to create and test the application for each component.

2, the container and the supporting infrastructure assembled into a complete application, you can use Docker stack file or Kubernetes of YAML to achieve.

3, test, and deploy a full share of containerized applications.

In this space, we will focus on this step of the workflow 1: creating image based on an existing vessel. Remember, Docker image capture proprietary file system containers in which the process will run; we need to create a mirror image, which contains exactly what we need to run the application.

Container development environment than traditional development environments easier to set up, once you learn how to build a mirror, we will discuss below. This is because all the required isolation container dependencies of the application development environment will Docker mirror; Docker addition, no need to install anything on the development of the computer (i.e. development environment deployment depends only on the image Docker) . This way, you can easily develop into different stacks of applications, without having to change any environment on the development machine.

Configuration

1, an example project from GitHub clone presentation.

git clone -b v1 https://github.com/docker-training/node-bulletin-board
cd node-bulletin-board/bulletin-board-app

This is a simple bulletin board application, written in node.js. In this example, let us assume that you wrote this application, is now trying to vessel deployment up, the directory structure shown in Figure 1.1.

001.png

Figure 1.1

Check the file name for Dockerfile, Dockerfile document describes how to assemble private container file system, and also contains some description of how to run this image-based container metadata. Dockerfile bulletin board application shown in Figure 1.2.

002.png


Figure 1.2

容器化部署应用程序的第一步是编写Dockerfile。您可以把这些Dockerfile命令当作是一个创建镜象的步骤。这个步骤如下:

1、使用版本号6.11.5的镜像作为本次构建的基础镜像。6.11.5基础镜像是由node.js官方提供的镜像

2、指定镜像文件系统的工作目录为/usr/src/app,即每次登录基于该镜像创建的容器,目录会自动切换到/usr/src/app

3、将package.json文件复制到镜像的当前位置,即/usr/src/app/package.json

4、在镜像文件系统中运行npm install命令(它将读取package.json以确定应用程序的节点依赖关系,并安装它们)

5、将应用程序的其余源代码从主机复制到镜像中。

您可以看到,这些步骤与您在主机上设置和安装应用程序时可能采取的步骤大致相同,但使用Dockerfile允许我们在一个可移植、独立的Docker镜像(node.js官方镜像)再次构建。

上面的步骤为镜像构建了文件系统,但是Dockerfile中还有一行,CMD命令行。CMD ["要运行的程序","参数1","参数2"] 是告诉容器启动时要运行的命令或脚本,Dockerfile中命令告知此镜像要支持的容器化进程是npm start。

注意:Dockerfile只能有一条CMD命令,如果有多条,则执行最后一条。

一个Dockerfile始终是从FROM命令开始,上述是一个简单的Dockerfile文件,还有更多的Dockerfile指令,请参阅Dockerfile reference(https://docs.docker.com/engine/reference/builder/)。

构建镜像并测试

现在我们有了源代码和Dockerfile,是时候构建我们的第一个镜像了,并确保从该镜像启动的容器能按预期工作。

注意:本示例使用的是Linux容器。使用Docker Desktop的用户,右键单击系统托盘中的Docker徽标,确保您的环境正在运行Linux容器,若不是,请点击“Switch to Linux containers...",如图1.3所示,我当前演示环境使用的已是Linux容器。

Docker部署æ,¨çš "ç¬¬ä¸ ä¸ªåº €" ç "C <º

图1.3

1、进入Powershell,并确保您当前目录是node-bulletin-board/bulletin-board-app,如图1.4所示,执行命令

# docker image build -t bulletinboard:1.0 .

004.png

图1.4

在构建最后会输出如下字样,表示构建镜像成功。

Successfully built 49f9b9fb7daf

Successfully tagged bulletinboard:1.0

上述命令表是创建一个名为bulletinboard的镜像(image),tag为1.0,如图1.5所示。

Docker deploy your first application

图1.5

2、基于bulletinboard镜像运行一个名为bb的容器并以后台方式运行,将容器内部端口8080,映射到宿主机的8000端口上。

# docker container run --publish 8000:8080 --detach --name bb bulletinboard:1.0

我们可以使用如下命令来查看run指命后可以跟哪些参数

# docker container run --help

我们登录到容器,查看到当前目录就是Dockerfile中设置的工作目录,如图1.6所示。

Docker deploy your first application

图1.6

3、通过http://localhost:8000来访问您的应用,您将看到如下界面,如图1.7所示,则表示应用容器化部署成功,接下来就是进行测试、构建、发布、分享等过程。

Docker deploy your first application

图1.7

4、删除一个容器,即表示该容器的生命周期结束。

# docker container rm --force bb

总结

After the completion of the operation, we have been able to perform a simple container of the application, and make sure our applications run successfully in its container. The next step will be yaml documents prepared Kubernetes, describing how to run and manage the container on Kubernetes.

In addition, we should strengthen the construction of practice using Dockerfile mirror.

Guess you like

Origin blog.51cto.com/firefly222/2461500