Docker Nginx安装使用以及踩坑点总结
拉取nginx镜像
docker pull nginx:latest
运行镜像
暂时不需要配置volume挂载
docker run --name nginx -p 80:80 -d nginx
参数详解:
--name nginx
指定容器的名称
-p 80:80
映射端口
-d
守护进程运行
创建volume目录
mkdir -p /www/nginx/conf.d
mkdir -p /www/nginx/html
mkdir -p /www/nginx/log
mkdir -p /www/nginx/conf
拷贝nginx容器对应的文件默认配置(关键)
docker cp nginx:/etc/nginx/nginx.conf /www/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /www/nginx/conf.d
docker cp nginx:/usr/share/nginx/html /www/nginx
docker cp nginx:/var/log/nginx /www/nginx/log
注意: nginx.conf
是文件,不是目录,很多的文章都没提到这个注意点,直接就创建了一个nginx.conf
目录进行挂载。 如果nginx.conf
是一个目录,在后面挂载的时候会报错。
报错信息:
[root@poloyy ~]# docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx
e0e4b40446a64927603b85854c3a6472b2dfa5681fcbfa0e170c16b15e5c8fdd
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: 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.
[root@poloyy ~]# client_loop: send disconnect: Broken pipe
报错信息的意思:
mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)?
将“/root/nginx.conf”挂载到“/etc/nginx/nginx.conf”的rootfs导致:通过procfd挂载:不是目录:未知:您是否试图将目录挂载到文件上(反之亦然)
报错的原因是:
-
不支持直接挂载文件,只能挂载文件夹
-
想要挂载文件,必须宿主机也要有对应的同名文件
停止容器
docker stop nginx
docker rm nginx
重新运行镜像(挂载volume)
docker run --name nginx -p 80:80 --privileged --restart=always \
-v /www/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /www/nginx/conf.d:/etc/nginx/conf.d:rw \
-v /www/nginx/html:/usr/share/nginx/html:rw \
-v /www/nginx/log:/var/log/nginx \
-d nginx