配置nginx挂载文件注意事项

docker pull nginx 命令安装

查找 Docker Hub 上的 nginx 镜像

[root@rabbitmq1 ~]# docker search nginx
$ docker pull nginx

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

 

以下命令使用 NGINX 默认的配置来启动一个 Nginx 容器实例:

$ docker run --name runoob-nginx-test -p 8081:80 -d nginx
  • runoob-nginx-test 容器名称。
  • the -d设置容器在在后台一直运行。
  • the -p 端口进行映射,将本地 8081 端口映射到容器内部的 80 端口

执行以上命令会生成一串字符串,类似 6dd4380ba70820bd2acc55ed2b326dd8c0ac7c93f68f0067daecad82aef5f938,这个表示容器的 ID,一般可作为日志的文件名。

我们可以使用 docker ps 命令查看容器是否有在运行:

$ docker ps
CONTAINER ID        IMAGE        ...               PORTS                  NAMES
6dd4380ba708        nginx        ...      0.0.0.0:8081->80/tcp   runoob-nginx-test

PORTS 部分表示端口映射,本地的 8081 端口映射到容器内部的 80 端口。

在浏览器中打开 http://192.168.98.105:8081/,效果如下:

 


nginx 部署

首先,创建目录 nginx, 用于存放后面的相关东西。

$ mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf

拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

docker cp 6dd4380ba708:/etc/nginx/nginx.conf ~/nginx/conf
  • www: 目录将映射为 nginx 容器配置的虚拟目录。
  • logs: 目录将映射为 nginx 容器的日志目录。
  • conf: 目录里的配置文件将映射为 nginx 容器的配置文件。

部署命令

$ docker run -d -p 8089:80 --name runoob-nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx

命令说明:

  • -p 8089:80: 将容器的 80 端口映射到主机的 8089 端口。

  • --name runoob-nginx-test-web:将容器命名为 runoob-nginx-test-web。

  • ~/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。

  • -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。

  • -v ~/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。

启动以上命令后进入 ~/nginx/www 目录:

$ cd ~/nginx/www

创建 index.html 文件,内容如下:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>菜鸟教程(runoob.com) </title>
  6. </head>
  7. <body>
  8. <h1>我的第一个标题 </h1>
  9. <p>我的第一个段落。 </p>
  10. </body>
  11. </html>

输出结果为:

相关命令

如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器:

$ docker kill -s HUP container-name

重启 NGINX 容器命令:

$ docker restart container-name

参考链接:https://www.runoob.com/docker/docker-install-nginx.html 

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/qq_35275233">
                <img src="https://profile.csdnimg.cn/8/9/D/3_qq_35275233" class="avatar_pic" username="qq_35275233">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/qq_35275233" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">剑灵尊</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog6.png">                                            </div>
                <div class="text"><span>原创文章 273</span><span>获赞 158</span><span>访问量 29万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extend1&quot;:&quot;个人信息未关注&quot;}">关注</a>
                                                            <a href="https://im.csdn.net/im/main.html?userName=qq_35275233" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>

注意:配置文件里面的静态资源文件是容器的静态文件路径,而不是linux系统的路径,一定要搞清楚,不然无法访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值