[docker series] use docker to install nginx to provide web services

insert image description here


Generally, to learn a technology, we will first use a simplest example or the most typical example to explain the introductory content to you, so this article will introduce you to use docker to install nginx container service. From the perspective of basic usage, this article covers almost the core content of docker: image pulling, container operation, port mapping, and file mapping . Although basic, it is very important, so it is necessary for beginners to read it carefully.

First, pull the mirror

The docker pull command is used to pull the application image, and the docker pull nginxcommand is used to pull the latest version of the nginx image. The following is the response result of the pull mirror process:

# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c229119241af: Pull complete 
2215908dc0a2: Pull complete 
08c3cb2073f1: Pull complete 
18f38162c0ce: Pull complete 
10e2168f148a: Pull complete 
c4ffe9532b5f: Pull complete 
Digest: sha256:2275af0f20d71b293916f1958f8497f987b8d8fd8113df54635f2a5915002bf1
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

From the above we can see that the nginx image is docker.iopulled from this URL.

Use the docker imagescommand to check which image files are downloaded in the current operating system.

# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    12766a6745ee   33 hours ago   142MB
hello-world   latest    feb5d9fea6a5   6 months ago   13.3kB
  • REPOSITORY mirror warehouse and mirror name, if the mirror warehouse is not displayed, the default isdocker.io
  • TAG mirror version or milestone tag, latest represents the latest version
  • IMAGE ID Unique ID of the image
  • CREATED when this image was created
  • SIZE indicates the size of the image file

2. Run the image to start the container

Start a container with the docker runcommand, the container name is nginx-zimug.

# docker run -d --name nginx-zimug -p  80:80  nginx
81bb1211144bc0991a789f860458548643c60b5459466c14d0604be9a4ccbfd7
  • -dIndicates that the container is running in the background
  • --namegive the container a name
  • -pPort mapping, the format is 宿主机端口:容器端口, the meaning above is to map the port 80 in the container to the port 80 of the host, and provide external access services.
  • The last field is the image name

The browser HTTP protocol accesses port 80 of the host, if it is port 80, it can be omitted. The access results obtained are as follows, indicating that our nginx service has been successfully started.

You can docker psview running containers as follows:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
81bb1211144b   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 11 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx-zimug

3. File Mapping

First of all, it is clear that the contents of the files in the container can be modified, but once the container is restarted, all modifications to the data files and configuration files written to the container will be lost . Therefore, in order to save the running state and execution result of the container, we need to map some important data files, log files, and configuration files in the container to the host.
Taking nginx as an example, nginx has three important file directories:

path in the container Custom mapping path in the host
Directory where website pages are stored /usr/share/nginx/html /root/nginx/html
nginx configuration file directory /etc/nginx/nginx.conf /root/nginx/conf/nginx.conf
log directory /var/log/nginx /root/nginx/logs

Create a new file directory on the host

mkdir -p  /root/nginx/logs  /root/nginx/html  /root/nginx/conf;

Copy the files in the container to the host
Copy the nginx configuration file to the host

docker cp nginx-zimug:/etc/nginx/nginx.conf /root/nginx/conf;

Put a simulated html file into the html directory
Save the following file as index.html and put it in the directory of the host machine /root/nginx/html, because there is a mapping relationship, it is actually also put into the /usr/share/nginx/htmldirectory of the container.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用docker搭建nginx web服务</title>
</head>
<body>
    <h1>访问成功</h1>
    <p>厉害了!</p>
</body>
</html>

Fourth, start the container service again

-vThe parameter expresses the mapping relationship between the host file and the file in the container, and the format is -v 宿主机目录:容器文件系统目录: Start a new container named nginx-prod

docker run -d -p 80:80 \
--name nginx-prod \
-v /root/nginx/html:/usr/share/nginx/html \
-v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/logs:/var/log/nginx  nginx

Before starting the new container, delete the old container of nginx-zimug. If the old container is not deleted, the port of the new container will conflict with the port of the old container. Delete the container with the following command:

docker stop nginx-zimug;
docker rm nginx-zimug;

Execute the above docker runcommand to start a new container. After startup, access port 80 of the host through the browser, and the response result is as follows to prove that nginx provides web services normally.

At the same time, you can modify the nginx configuration on the host, and you can also view the runtime log file. The modification result will affect the operation of the container nginx service, because there is a mapping relationship between the configuration file of the host and the configuration file in the container.

insert image description here

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/123888685