拉取镜像
docker pull nginx
不加标签默认会拉取最新的(latest)镜像
创建目录
mkdir -p /home/nginx/www /home/nginx/logs /home/nginx/conf
先不指定映射路径启动一个容器
docker run -d -p 80:80 —name nginx nginx
1fdcd13457a6eaacb511878e10d84ffbe48fe63fd1fb3705f58b2d4195b151d8
这里如果直接指定映射路径运行会报错
$ docker run -d -p 80:80 --name mynginx \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/logs:/var/log/nginx \
-v /home/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf.d:/etc/nginx/conf.d \
-v /home/cert:/home/cert \
nginx
$ dab56c13f9e76aad37fcf73411c78d495a6466f1c0d214949650dbae44adddf4
$ docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:424: container init caused \"rootfs_linux.go:58: mounting \\\"/home/devuser/apps/nginx/conf/nginx.conf\\\" to rootfs \\\"/home/docker_image/overlay2/e40ccaf4d845a9af92487b47cbc4d505c5c776800ef8887c5b43833b10661427/merged\\\" at \\\"/home/docker_image/overlay2/e40ccaf4d845a9af92487b47cbc4d505c5c776800ef8887c5b43833b10661427/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
将运行容器的配置文件复制到宿主机目录下
docker cp 1fdcd13457a6:/etc/nginx/nginx.conf /home/nginx
删除容器并重新运行
$ docker stop 1fdcd1345
$ docker rm 1fdcd1345
$ docker run -d -p 80:80 --name mynginx \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/logs:/var/log/nginx \
-v /home/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf.d:/etc/nginx/conf.d \
-v /home/cert:/home/cert \
nginx
更新配置后重新加载
如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器:
docker kill -s HUP container-name
重启 NGINX 容器命令:
docker restart container-name
类似于
nginx -s reload