【问题标题】:How to change the port of nginx when using with docker使用docker时如何更改nginx的端口
【发布时间】:2018-05-02 00:28:25
【问题描述】:

我是一名 docker 初学者,我做的第一件事是下载 nginx 并尝试将其挂载到 80:80 端口,但 Apache 已经坐在那里了。

docker container run --publish 80:80 nginx

docker container run --publish 3000:3000 nginx

我尝试像 3000:3000 那样在端口 3000 上使用它,但它不起作用。它也没有记录任何我可以用作参考的东西。

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    接受的答案不会改变 nginx 启动的实际端口。

    如果你想改变nginx在容器内启动的端口,你必须修改容器内的/etc/nginx/nginx.conf文件。

    例如,在端口 9080 上启动:

    Dockerfile

    FROM nginx:1.17-alpine
    COPY <your static content> /usr/share/nginx/html
    COPY nginx.conf /etc/nginx/
    EXPOSE 9080
    CMD ["nginx", "-g", "daemon off;"]
    

    nginx.conf

    # on alpine, copy to /etc/nginx/nginx.conf
    user                            root;
    worker_processes                auto;
    
    error_log                       /var/log/nginx/error.log warn;
    
    events {
        worker_connections          1024;
    }
    
    http {
        include                     /etc/nginx/mime.types;
        default_type                application/octet-stream;
        sendfile                    off;
        access_log                  off;
        keepalive_timeout           3000;
        server {
            listen                  9080;
            root                    /usr/share/nginx/html;
            index                   index.html;
            server_name             localhost;
            client_max_body_size    16m;
        }
    }
    

    现在从您的计算机访问服务器:

    docker build . -t my-app
    docker run -p 3333:9080 my-app
    

    在浏览器中导航到 localhost:3333,您将看到您的内容。

    可能有一种方法可以包含默认的 nginx.conf,并且只覆盖 server.listen = PORT 属性,但是我对 nginx 配置不太熟悉,所以我只是覆盖了整个默认配置。

    【讨论】:

    • 你的nginx是从哪里来的,它有什么作用?
    • @PositiveGuy nginx.conf 是您保留在源代码控制下的配置文件 - 在您的 dockerfile 中,您指示 docker 将 nginx.conf 文件从源代码目录复制到路径中的 nginx 容器/etc/nginx/nginx.conf,因为这是 nginx 查找配置的默认位置。换句话说,nginx.conf 与你的 Dockerfile 处于同一级别。
    • 也许只是不使用 root 用户。你也可以使用已经不是 root 的 nginx-images,比如 nginxinc/nginx-unprivileged
    • 保持这篇文章是最新的。在新的 NGINX 版本上,nginx.conf 文件已移动到此位置 /etc/nginx/conf.d/default.conf。所以复制一个自定义的 nginx.conf 文件可能是这样的COPY ["default.conf", "/etc/nginx/conf.d/default.conf"]。假设用户不想添加另一个文件只覆盖默认值。
    【解决方案2】:

    当您开始使用 Docker 时,您可能会在 DockerHub 上找到有关图像的有用信息。例如,对于 nginx,您有一个关于如何expose public ports 的部分。

    你可以使用:

    docker run --publish 3000:80 nginx
    

    本地主机中的 3000 端口将被转发到 80 端口,这是 nginx 映像用于等待 http 连接的端口。

    我还建议您阅读这些官方文档about networking in Docker

    【讨论】:

      【解决方案3】:

      你写的是你是一个初学者,所以首先我会提到nginx镜像的默认配置(我假设你使用的是标准镜像)是监听端口80。 这就是为什么你不能映射到容器内的端口3000,因为没有进程监听这个端口。

      现在,如果我对您的理解正确,并且您将 nginx 与 docker 一起使用 我想您希望能够配置容器的端口(而不是主机端口,因为这很安静琐碎)。

      @mancini0 开始了一个很好的方向,但我将展示如何以更动态的方式实现它。

      我们将使用envsubst 命令替换shell 格式字符串中的环境变量。
      此命令适用于 offical nginx image 和 alpine 版本。

      现在解决问题。

      第 1 步
      将您的 nginx 配置写入模板文件 - 我们称之为:site.template:

      server {
          listen       ${PORT};
          server_name  localhost;
      
          location / {
              root   /usr/share/nginx/html;
              index  index.html index.htm;
          }
      }
      

      注意 PORT 占位符。

      第 2 步 - 使用 docker-compose
      将其挂载到/etc/nginx/conf.d 目录中,然后执行envsubst 命令以使用模板作为default.conf 的参考,default.conf 是用于在容器内设置端口配置的文件:

      web:
        image: nginx:alpine
        volumes:
         - ./site.template:/etc/nginx/conf.d/site.template
        ports:
         - "3000:8080"
        environment:
         - PORT=8080
        command: /bin/sh -c "envsubst < /etc/nginx/conf.d/site.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
      

      请注意:
      1. 之后需要执行 nginx 守护进程。
      2. 我使用了/bin/sh 而不是/bin/bash,因为我的基础镜像是 alpine。


      第 2 步(另一个选项)- 内联 docker run
      如果由于某种原因您不想使用 docker-compose,您可以使用以下 bash 脚本:

      #!/usr/bin/env bash
      
      ##### Variables #####
      PORT=8080 #Or $1 if you pass it from command line
      TEMPLATE_DIR=$(pwd)/site.template
      TEMPLATE_REMOTE_DIR=/etc/nginx/conf.d/site.template
      IMAGE_NAME=nginx:alpine
      
      echo "Starting nginx on port: $PORT ..."
      
      ##### The docker command #####
      docker run -p 3000:$PORT -v $TEMPLATE_DIR:$TEMPLATE_REMOTE_DIR $IMAGE_NAME \
      /bin/sh -c "envsubst < $TEMPLATE_REMOTE_DIR > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
      

      (*) 您也可以使用CMD 命令将其写入您的 Dockerfile,但我不建议您这样做。

      【讨论】:

      • 你不能用“ENV NGINX_PORT 8080”暴露dockerfile中的端口吗?
      • 来自这里:docs.docker.com/engine/reference/builder/#expose EXPOSE 指令实际上并没有发布端口。它充当构建映像的人和运行容器的人之间的一种文档类型,关于打算发布哪些端口。
      • 我写的是 ENV 而不是 EXPOSE
      • 对不起(:我认为将它添加到 docker-compose.yaml 中(就像我一样)然后在图像中硬编码会更灵活。
      【解决方案4】:

      您可以使用 nginx.conf 将其更改为任何端口。

      nginx.conf 将包含所有与 nginx 相关的配置。此处端口的默认值为 80,我们可以更改如下。

      nginx.conf

      server {
        listen 3000;
      
        location / {
          root /usr/share/nginx/html;
          index index.html index.htm;
          try_files $uri $uri/ /index.html =404;
        }
      
        include /etc/nginx/extra-conf.d/*.conf;
      }
      

      并在 DockerFile 中添加一行:

      COPY ./nginx.conf /etc/nginx/conf.d/default.conf
      

      【讨论】:

      • 请解释您的帖子如何提供任何新的帮助。其他答案似乎指向相同,但有更多信息和解释。你更喜欢哪个答案?
      • 如果我们使用 DockerFile,这是直截了当的答案
      【解决方案5】:
      docker run -e NGINX_PORT=8080 nginx:latest -d 
      

      The page

      【讨论】:

        猜你喜欢
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-27
        • 2018-06-30
        • 2018-11-30
        • 2020-11-07
        相关资源
        最近更新 更多