Docker mount mode to start Nginx

foreword

Previously, Nginx configuration files, static resources, etc. were copied into the container, which caused a problem: when modifying static resources or viewing logs, it is troublesome to enter the container, so it is changed to mount to start Nginx.

1. Check the files and folders in the container in advance:

/usr/share/nginx/html/
/etc/nginx/conf.d/
/etc/nginx/nginx.conf
/var/log/nginx/

2. Create a mount directory on the host

mkdir -p /usr/local/docker/nginx/conf/
mkdir -p /usr/local/docker/nginx/log/

3. Copy the nginx configuration file from the container to the host mount directory

# 生成容器
docker run --name nginx -p 8989:80 -d nginx:latest
# 将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /usr/local/docker/nginx/conf/nginx.conf
# 将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /usr/local/docker/nginx/conf/conf.d
# 将容器html文件夹下内容复制到宿主机
docker cp nginx:/usr/share/nginx/html /usr/local/docker/nginx

4. Delete the original container

docker stop nginx
docker rm nginx

5. Change to mount mode to start

docker run -p 8989:80 --name nginx -v /usr/local/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/docker/nginx/conf/conf.d:/etc/nginx/conf.d -v /usr/local/docker/nginx/log:/var/log/nginx -v /usr/local/docker/nginx/html:/usr/share/nginx/html -d nginx:latest

6. Quickly modify html static resources

# 直接在宿主机/usr/local/docker/nginx/html文件夹中修改即可
cd /usr/local/docker/nginx/html

Guess you like

Origin blog.csdn.net/weixin_41474364/article/details/125166504