1.先启动一次,把配置文件copy出来#
创建并运行容器,容器命名为nginx docker run –name nginx -p 80:80 -d nginx
创建目录存放
mkdir /usr/local/docker-nginx
从容器中copy 配置
docker cp nginx:/etc/nginx/nginx.conf /usr/local/docker-nginx/
docker cp nginx:/etc/nginx/conf.d/ /usr/local/docker-nginx/
docker cp nginx:/usr/share/nginx/html/ /usr/local/docker-nginx/
2.干掉之前的容器,正式启动
docker stop nginx
docker rm nginx
参数解释
-p 映射端口,格式为“宿主机端口:容器端口 -v 挂载文件 -e TZ=Asia/Shanghai 设置时区 -privileoed=true 让容器中的root用户拥有直下的root权限
–network host 让容器使用宿主机的IP和端口 –name 容器名字,以后可以使用这个名字启动或者信止容器 -restart=alwavs docker启动时自动启动容器 -d 指定要启动的镜像名
docker run -p 80:80 \
-v /usr/local/docker-nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/docker-nginx/logs:/var/log/nginx \
-v /usr/local/docker-nginx/html:/usr/share/nginx/html \
-v /usr/local/docker-nginx/conf.d:/etc/nginx/conf.d \
-v /data/web:/data/web \
-v /etc/localtime:/etc/localtime \
--name nginx \
--network host \
--privileged=true \
--restart=always \
-d nginx
可以将以下脚本放在/usr/local/docker-nginx/目录下
reload.sh
docker exec nginx nginx -t
docker exec nginx nginx -s reload
stop.sh
docker stop nginx
docker rm nginx
startup.sh
docker run -p 80:80 \
-v /usr/local/docker-nginx/nginx.conf:/etc/nginx/nginx.conf
-v /usr/local/docker-nginx/logs:/var/log/nginx \
-v /usr/local/docker-nginx/html:/usr/share/nginx/html \
-v /usr/local/docker-nginx/conf.d:/etc/nginx/conf.d \
-v /data/web:/data/web \
-v /etc/localtime:/etc/localtime \
--name nginx \
--network host \
--privileged=true \
--restart=always \
-d nginx
#修改Nginx配置后,可以不用重启容器,进行加载
docker exec 容器id nginx -t
docker exec 容器id nginx -s reload
参考链接:
https://blog.csdn.net/red_sky_blue/article/details/124278600