Docker starts and mounts Nginx and operates the container after startup

1. Start and mount the Nginx container

1. Search mirror

docker search nginx

2. Pull the image

docker pull nginx:latest

3. Create a directory to mount

mkdir -p /home/kevin/project1/nginx/{
    
    conf,logs}

4. Start the temporary Nginx container [the purpose is to obtain the files inside]

docker run -d -P --name nginx1 nginx

5. Copy the nginx configuration file from the container nginx1 [preferably without the container name]

# docker cp 容器id:容器内路径 目的主机路径
docker cp nginx1:/etc/nginx/nginx.conf /home/kevin/project1/nginx/conf/nginx.conf
docker cp nginx1:/etc/nginx/conf.d/default.conf /home/kevin/project1/nginx/conf/default.conf
docker cp nginx1:/usr/share/nginx/html /home/kevin/project1/nginx

docker rm -f nginx1

ps: After copying, you can delete the temporary Nginx container

docker rm -f nginx1

6. Start the container and mount it

The includ_conf is mounted here to create a configuration file that can be imported for nginx

Note : The includ_conf path is not originally included in the started nginx. By mounting it at startup, the container will automatically create the path
[in the conf/nginx.conf file, set it in the root of the location under the server, the file path of the front page (This path must be mounted)]

docker run -d --restart=always \
--privileged=true \
--name nginx01 \
-p 8081:80 \
-p 8082:8082 \
-v /home/kevin/project1/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/kevin/project1/nginx/includ_conf:/etc/nginx/includ_conf \
-v /home/kevin/project1/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf \
-v /home/kevin/project1/nginx/html:/usr/share/nginx/html \
-v /home/kevin/project1/nginx/logs:/var/log/nginx  \
nginx

Explanation : Two ports 8081 and 8082 are exposed to the outside world, but there is only the default 80 in the Nginx configuration; 8082 is not configured in Nginx, this is reserved for subsequent startup projects, and only needs to be in nginx.conf at that time Just configure port 8082

7. Determine whether Nginx is available

Enter the command on the server to check whether the welcome interface is opened
or open the page on the public network to view

curl localhost:8081

2. Operate the started container (-p, -v)

The important thing is repeated: before operating the container that has been started , be sure to stop dokcer first

1. Expose new ports

view current container

docker inspect nginx01

Find the port status of the container
(the first picture: hostconfig.json corresponding to the current container; the second picture: config.v2.json corresponding to the current container)
insert image description here
insert image description here

1.1 Stop docker (you must stop docker first, otherwise modifying the configuration file directly will not take effect)

systemctl stop docker

1.2 Modify the port in the config.v2.json file of this container

configuration file path

cd /var/lib/docker/containers/容器id全称
vim config.v2.json

insert image description here

目的:在容器中暴露出8083端口
# 修改前
"ExposedPorts":{
    
    "80/tcp":{
    
    },"8082/tcp":{
    
    }}

# 修改后
"ExposedPorts":{
    
    "80/tcp":{
    
    },"8082/tcp":{
    
    },"8083/tcp":{
    
    }}

1.3 Modify the port in the hostconfig.json file of this container

configuration file path

cd /var/lib/docker/containers/容器id全称
vim hostconfig.json

insert image description here
The first number is the container port, the latter is the host port

目的: -v 8083:8083
# 修改前
"PortBindings":{
    
    "80/tcp":[{
    
    "HostIp":"","HostPort":"8081"}],"8082/tcp":[{
    
    "HostIp":"","HostPort":"8082"}]}

# 修改后  添加"8083/tcp":[{"HostIp":"","HostPort":"8083"}]
"PortBindings":{
    
    "80/tcp":[{
    
    "HostIp":"","HostPort":"8081"}],"8082/tcp":[{
    
    "HostIp":"","HostPort":"8082"}],
"8083/tcp":[{
    
    "HostIp":"","HostPort":"8083"}]}

1.4 Restart the docker service

systemctl restart docker

1.5 Check that the configuration item has been modified successfully

docker inspect nginx01

2. Mount a new directory

2.1 Stop docker (you must stop docker first, otherwise modifying the configuration file directly will not take effect)

systemctl stop docker

2.2 Modify the port in the config.v2.json file of this container

configuration file path

cd /var/lib/docker/containers/容器id全称
vim config.v2.json

For the places marked with comments, just modify the directory you want to mount

例如:
新挂载的宿主机目录		/home/kevin/project1/nginx/includ_conf_test
新挂载的容器目录		/etc/nginx/includ_conf_test
{
    
    
    ......
	
	"MountPoints": {
    
    
		......,
		"/home/kevin/project1/nginx/includ_conf_test": {
    
    	# 宿主机目录
			"Source": "/home/kevin/project1/nginx/includ_conf_test", # 宿主机目录
			"Destination": "/etc/nginx/includ_conf_test", # 容器目录
			"RW": true,
			"Name": "",
			"Driver": "",
			"Type": "bind",
			"Propagation": "rprivate",
			"Spec": {
    
    
				"Type": "bind",
				"Source": "/home/kevin/project1/nginx/includ_conf_test", # 宿主机目录
				"Target": "/etc/nginx/includ_conf_test"  # 容器目录
			},
			"SkipMountpointCreation": false
		}
	},
	......
}

2.3 Modify the port in the hostconfig.json file of this container

configuration file path

cd /var/lib/docker/containers/容器id全称
vim hostconfig.json

The former is the host directory, and the latter is the container directory
. Just write the directory to be mounted according to the following style

{
    
    
	"Binds": [
		......,
		"/home/kevin/project1/nginx/includ_conf_test:/etc/nginx/includ_conf_test"
		# 新挂载宿主机目录:新挂载容器目录
	],
	......
}

2.4 Restart the docker service

systemctl restart docker

2.5 Check that the configuration item has been modified successfully

docker inspect nginx01

Guess you like

Origin blog.csdn.net/weixin_42516475/article/details/129421207