zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Docker 部署 Nginx

2023-09-14 09:14:08 时间

需求:挂载本地nginx配置文件到nginx容器内部使用 挂载静态页面到nginx容器内部

http://192.168.56.128:81/hello.html 可以访问到nginx容器中的页面。

Centos7 安装 Nginx:https://liush.blog.csdn.net/article/details/125027693

1. 拉取镜像

docker pull nginx

2. 编辑nginx配置文件

反向代理容器内部/opt目录下的静态资源

2.1 在虚拟机opt目录下创建子目录

mkdir /opt/nginxconf
mkdir /opt/html

2.2 编辑hello.html

vim /opt/html/hello.html

<a href="http://www.baidu.com">baidu</a>

2.3 拷贝nginx配置文件到子目录下

cp /usr/local/nginx/conf/nginx.conf  /opt/nginxconf

2.4 修改nginx配置:

location / {
    root /opt;
}

 

2. 创建Nginx容器

① 创建启动守护式容器,命名mynginx,映射虚拟机端口85指定容器nginx默认端口80,挂载虚拟机目录/opt/html/到容器目录/opt下

docker run -d --name mynginx -p 85:80 -v /opt/html/:/opt nginx

② 拷贝虚拟机文件到 命名为 mynginx 容器目录 /etc/nginx 下

docker cp /opt/nginxconf/nginx.conf mynginx:/etc/nginx/

③ 重启容器 mynginx

docker restart mynginx

 

3. 测试访问

http://192.168.56.128:85/hello.html