View the docker image
You can first check whether there is an nginx image under docker, and use the following commands to check:
- docker images: List all images.
- docker images nginx: List all nginx images, different versions, etc.
- docker search nginx: Search and view all nginx image information.
Pull and install nginx image
Use the pull command to pull the nginx image:
docker pull nginx
The following content appears, just wait for the download to complete:
run nginx
After downloading the nginx image, you can start and run nginx, use the following command:
docker run -d --name nginx -p 8080:80 nginx
The commands are described as follows:
- -d runs continuously in the background.
- --name custom container name.
- -p maps the host port number to the port number of the docker container.
Here port 8008 is the port number for external web access, and 80 is the port number exposed by the nginx container.
If the startup is successful, you can ip:8008
access the site through .
The above is the success of docker installation and starting nginx container service.
Very simple processing, the next thing we have to do is how to deal with nginx configuration, logs, static resources, etc. These are relatively complicated. There are various ways to handle configuration resource files etc., which are described in detail below.
map to a local directory
View the configuration resource directory of the nginx container
- Enter the container, you can execute the command
docker exec -it container-id/container-name bash
docker exec -it container-id/container-name /bin/bash
// -i: 以交互模式运行
// -t: 为容器重新分配一个伪输入终端
// -i 与 -t 通常同时使用
// container-id 容器id
// container-name 自定义容器名称
// bash (/bin/bash) linux 命令,启动一个bash终端,可与用户交互
- After entering the container bash terminal, you can view the directory location of nginx-related content as follows:
log Log file directory: /var/log/nginx
config configuration file directory: /etc/nginx
web resource storage directory: /usr/share/nginx/html
Note that you can view the corresponding file information at this time, but you cannot use vi to view the file content here, because it is a new terminal command environment, vi cannot be used.
You can use cat
commands such as:
cat nginx.conf
In addition, use the exit command to exit the current terminal command environment of the container.
Map nginx directory
-v
To map the configuration log resource and other directories of the nginx container to the local host directory, you need to use the command when starting the container, such as:
docker run -d --name nginx \
-p 8080:80 \
-v /usr/nginx/html:/usr/share/nginx/html \
-v /usr/nginx/log:/var/log/nginx \
-v /usr/nginx/nginx.conf:/etc/nginx/nginx.conf \
nginx
Among them -v
is the directory mount, which maps the local directory to the container directory, and then directly modifies the contents of the local directory, which will be synchronized to the container.
The above command mounts the resource file directory, log directory, and configuration file of nginx.
At this time, if you look at the configuration file nginx.conf, you can find that there is such a paragraph:
include /etc/nginx/conf.d/*.conf;
This is because the nginx container loads sub-configuration files in the conf.d directory in addition to the main configuration file nginx.conf, usually at least one default.conf. Therefore, when starting the container, you can also mount the directory:
-v /usr/nginx/conf.d:/etc/nginx/conf.d
You can also only use nginx.conf and modify the configuration without using the sub-configuration of conf.d, but the docker container uses self-configuration by default.
After the content resource is mounted, the static web file can be uploaded to the corresponding host directory.
You can also directly modify the configuration file in the host directory, and then restart nginx to take effect.
docker stop nginx
docker start nginx
Copy container resources
Another way to change the contents of configuration resources is to use docker cp
the command.
docker cp: used to copy data between the container and the host
Through this command, you can copy the local content to the container, such as the following command, you can copy the html file to the static resource directory of the nginx container:
docker cp /usr/nginx/html/mian.html nginx:/usr/share/nginx/html
You can also copy configuration files:
// 从容器拷贝到本地目录
docker cp nginx:/etc/nginx/nginx.conf /usr/nginx/nginx.conf
// 从本地目录拷贝到容器
docker cp /usr/nginx/nginx.conf nginx:/etc/nginx/nginx.conf
By copying, you can change static resource files, modify configuration files, view logs, etc.
port problem
Note: When starting nginx
through the command, the port is set through , and the port number of the nginx container selected by default is 80. If the port number of the nginx container is modified through the configuration file, such as changing to , and the nginx container is restarted, the nginx service will be invalid at this time, and the site cannot be accessed effectively. That is, containers other than port 80 cannot be accessed.docker run
-p
server 80
server 8080
If you want to deploy multiple websites, you can start multiple containers, and each container can correspond to a port number. At this time, you can modify the log configuration resource file and other content of each container.
How to modify the port of a running container
- Stop the nginx container from running,
docker stop nginx
- View the id of the nginx container,
docker inspect nginx | grep "Id"
, will display the complete container Id - The common path is /var/lib/docker/containers/{container Id}/hostconfig.json, which stores the container port and local port, which can be modified, and can be viewed and modified through the command: [
vi
External
link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-FlsflcQF-1660536230025)(./host.png 'modify port')] is the container port, which is the local port and can be
80/tcp
modifiedHostPort: 8005
. - restart docker,
systemctl restart docker
- Restart the nginx container,
docker start nginx