Docker 简明教程
Docker - Building a Web Server Docker File
我们已经学习了如何使用 Docker 文件构建我们自己的自定义映像。现在让我们看看如何构建一个 Web 服务器映像,该映像可用于构建容器。
We have already learnt how to use Docker File to build our own custom images. Now let’s see how we can build a web server image which can be used to build containers.
在我们的示例中,我们将使用 Ubuntu 上的 Apache Web 服务器来构建我们的映像。让我们按照下面的步骤来构建我们的 Web 服务器 Docker 文件。
In our example, we are going to use the Apache Web Server on Ubuntu to build our image. Let’s follow the steps given below, to build our web server Docker file.
Step 1 − 第一步是构建我们的 Docker 文件。让我们使用 vim 并创建一个包含以下信息的 Docker 文件。
Step 1 − The first step is to build our Docker File. Let’s use vim and create a Docker File with the following information.
FROM ubuntu
RUN apt-get update
RUN apt-get install –y apache2
RUN apt-get install –y apache2-utils
RUN apt-get clean
EXPOSE 80 CMD [“apache2ctl”, “-D”, “FOREGROUND”]
关于上述说法需要重点关注以下内容:
The following points need to be noted about the above statements −
-
We are first creating our image to be from the Ubuntu base image.
-
Next, we are going to use the RUN command to update all the packages on the Ubuntu system.
-
Next, we use the RUN command to install apache2 on our image.
-
Next, we use the RUN command to install the necessary utility apache2 packages on our image.
-
Next, we use the RUN command to clean any unnecessary files from the system.
-
The EXPOSE command is used to expose port 80 of Apache in the container to the Docker host.
-
Finally, the CMD command is used to run apache2 in the background.
输入文件详细信息后,只需保存文件。
Now that the file details have been entered, just save the file.
Step 2 − 运行 Docker build 命令以构建 Docker 文件。可以使用下列命令来执行此操作 −
Step 2 − Run the Docker build command to build the Docker file. It can be done using the following command −
sudo docker build –t=”mywebserver” .
我们将其标记为 mywebserver 映像。映像构建后,会收到映像已构建的成功消息。
We are tagging our image as mywebserver. Once the image is built, you will get a successful message that the file has been built.
Step 3 − 现在已构建 Web 服务器文件,可以从映像创建容器。我们可以使用 Docker run 命令执行此操作。
Step 3 − Now that the web server file has been built, it’s now time to create a container from the image. We can do this with the Docker run command.
sudo docker run –d –p 80:80 mywebserver
需要对上述命令注意以下几点 −
The following points need to be noted about the above command −
-
The port number exposed by the container is 80. Hence with the –p command, we are mapping the same port number to the 80 port number on our localhost.
-
The –d option is used to run the container in detached mode. This is so that the container can run in the background.
如果你在 Web 浏览器中访问 Docker 主机的 80 端口,现在会看到 Apache 已启动并正在运行。
If you go to port 80 of the Docker host in your web browser, you will now see that Apache is up and running.