1.拉取镜像
docker pull nginx
2.创建容器
docker docker run -d --name nginx -p 80:80 nginx
bash 执行 docker ps
查看
3.创建挂载目录
为什么要创建挂载目录?因为docker容器内部有自己的文件系统,在主机创建挂载目录并关联 nginx 容器的配置目录后,以后可以通过修改主机下的配置目录文件来直接影响到容器内的配置。
mkdir -p /data/nginx/{conf,log,html}
把 nginx 容器中的文件复制到主机
docker cp nginx:/etc/nginx/nginx.conf /data/nginx/conf/nginx.conf docker cp nginx:/etc/nginx/conf.d /data/nginx/conf/conf.d docker cp nginx:/usr/share/nginx/html /data/nginx/
4.停止并删除 nginx 容器
执行 docker stop + 容器id的前两位
docker stop 6a docker rm 6a
5.重新创建容器
docker run -d --name nginx -p 80:80 \ -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \ -v /data/nginx/conf/conf.d:/etc/nginx/conf.d \ -v /data/nginx/log:/var/log/nginx \ -v /data/nginx/html:/usr/share/nginx/html \ --privileged=true nginx
执行docker ps
查看运行状态,如果启动失败执行 docker logs + 容器id前两位
查看报错。
6.验证
curl localhost
出现以下内容说明安装成功
<!doctype html> <html> <head> <title>welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: tahoma, verdana, arial, sans-serif; } </style> </head> <body> <h1>welcome to nginx!</h1> <p>if you see this page, the nginx web server is successfully installed and working. further configuration is required.</p> <p>for online documentation and support please refer to <a href="http://nginx.org/" rel="external nofollow" >nginx.org</a>.<br/> commercial support is available at <a href="http://nginx.com/" rel="external nofollow" >nginx.com</a>.</p> <p><em>thank you for using nginx.</em></p> </body> </html>
或者打开浏览器输入服务器 ip 验证
到此这篇关于docker 安装 nginx 并挂载目录的实现示例的文章就介绍到这了,更多相关docker nginx挂载目录内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论