Docker 简明教程

Docker - Setting NGINX

NGINX 是一款流行且轻量级的 Web 应用程序,用于开发服务器端应用程序。它是一款开源 Web 服务器,开发用于在各种操作系统上运行。由于 nginx 是一款流行的 Web 服务器,因此 Docker 已确保其支持 nginx

NGINX is a popular lightweight web application that is used for developing server-side applications. It is an open-source web server that is developed to run on a variety of operating systems. Since nginx is a popular web server for development, Docker has ensured that it has support for nginx.

现在,我们将了解让 Docker 容器用于 nginx 并使其正常运行的不同步骤。

We will now see the various steps for getting the Docker container for nginx up and running.

Step 1 − 第一步是从 Docker Hub 中拉取映像。当您登录 Docker Hub 时,您将能够搜索并查看 nginx 的映像,如下所示。只需在搜索框中输入 nginx,然后单击搜索结果中显示的 nginx (官方)链接。

Step 1 − The first step is to pull the image from Docker Hub. When you log into Docker Hub, you will be able to search and see the image for nginx as shown below. Just type in nginx in the search box and click on the nginx (official) link which comes up in the search results.

nginx official link

Step 2 − 您将看到 Docker Hub 中存储库详细信息中的 pull Docker nginx 命令。

Step 2 − You will see that the Docker pull command for nginx in the details of the repository in Docker Hub.

docker pull command for nginx

Step 3 − 在 Docker 主机上,使用上面显示的 Docker pull 命令,从 Docker Hub 下载最新的 nginx 映像。

Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest nginx image from Docker Hub.

nginx image

Step 4 − 现在,让我们通过以下命令运行 nginx 容器。

Step 4 − Now let’s run the nginx container via the following command.

sudo docker run –p 8080:80 –d nginx

我们正在向 Docker 主机上的端口 8080 公开 nginx 服务器上的端口(端口 80)。

We are exposing the port on the nginx server which is port 80 to the port 8080 on the Docker Host.

nginx server

在运行该命令后,如果您浏览到 URL http://dockerhost:8080 ,将获得以下输出。这表明 nginx 容器正常运行。

Once you run the command, you will get the following output if you browse to the URL http://dockerhost:8080. This shows that the nginx container is up and running.

nginx container

Step 5 − 让我们看另一个示例,其中我们可以在 ngnix 容器中托管一个简单的 Web 页面。在我们的示例中,我们将创建一个简单的 HelloWorld.html 文件,并将其托管在我们的 nginx 容器中。

Step 5 − Let’s look at another example where we can host a simple web page in our ngnix container. In our example, we will create a simple HelloWorld.html file and host it in our nginx container.

我们首先创建一个名为 HelloWorld.html 的 HTML 文件

Let’s first create an HTML file called HelloWorld.html

html file

我们向 HTML 文件中添加 Hello World 的一串简单文本。

Let’s add a simple line of Hello World in the HTML file.

simple line hello world

接下来,运行以下 Docker 命令。

Let’s then run the following Docker command.

sudo docker run –p 8080:80 –v
   “$PWD”:/usr/share/nginx/html:ro –d nginx

需要对上述命令注意以下几点 −

The following points need to be noted about the above command −

  1. We are exposing the port on the nginx server which is port 80 to the port 8080 on the Docker Host.

  2. Next, we are attaching the volume on the container which is /usr/share/nginx/html to our present working directory. This is where our HelloWorld.html file is stored.

working directory

现在,如果我们浏览到 URL * [role="bare"] [role="bare"]http://dockerhost:8080/HelloWorld.html* ,我们将按预期获得以下输出 −

Now if we browse to the URL * [role="bare"]http://dockerhost:8080/HelloWorld.html* we will get the following output as expected −

output expected