当前位置: 代码网 > 服务器>服务器>云虚拟主机 > Docker 安装 Nginx 并挂载目录的实现示例

Docker 安装 Nginx 并挂载目录的实现示例

2024年11月03日 云虚拟主机 我要评论
1.拉取镜像docker pull nginx2.创建容器docker docker run -d --name nginx -p 80:80 nginxbash 执行docker ps查看3.创建挂

1.拉取镜像

docker pull nginx

2.创建容器

docker docker run -d --name nginx -p 80:80 nginx

bash 执行 docker ps 查看

dockerps

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 验证

image-20240620171653544

到此这篇关于docker 安装 nginx 并挂载目录的实现示例的文章就介绍到这了,更多相关docker nginx挂载目录内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com