跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 用 docker-compose 启动 nginx, network 为 host, nginx 无法启动是怎么回事?
未分類
2 9 月 2020

用 docker-compose 启动 nginx, network 为 host, nginx 无法启动是怎么回事?

用 docker-compose 启动 nginx, network 为 host, nginx 无法启动是怎么回事?

資深大佬 : css3 24

大佬们请教个问题:

场景:docker for mac, 通过 docker-compose 创建 nginx 容器,

遇到问题( nginx 无法正常运行):

  • 若 docker 的 network 为 host 时,容器启动显示正常,但通过 docker ps -a 不显示 PORTS ,应该是假启动,mac 浏览器访问 lcoalhost 时,直接显示无法访问此网站(我理解的根本就没有启动成功,所以打不开页面)
  • 若 docker 的 network 为默认的,容器启动显示正常,通过 docker ps -a 也能显示映射的 PORTS, 应该是启动成功,mac 浏览器访问 lcoalhost 时,显示的是 nginx 的错误页面(为啥会这样)
# docker-compse.yml version: '3' services:   nginx:     container_name: nginx     image: nginx:latest     ports:     - 80:80     network_mode: host     volumes:     - /opt/nginx/nginx.conf:/etc/nginx/nginx.conf #宿主机文件已创建     - /opt/nginx/conf.d:/etc/nginx/conf.d #宿主机目录已创建     - /opt/nginx/log:/var/log/nginx #宿主机目录已创建     restart: always 
$ docker-compose up -d Creating nginx ... done 
docker ps -a # PORTS 是空的 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES cf103d8c33c6        nginx:latest        "/docker-entrypoint.…"   3 seconds ago       Up 2 seconds                            nginx  
$ docker logs -f nginx  # 为啥提示 read-only file system? /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: error: can not modify /etc/nginx/conf.d/default.conf (read-only file system?) /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Configuration complete; ready for start up 

大佬有話說 (19)

  • 資深大佬 : StarUDream

    指定为 “host” 时,所有容器端口都对应属主机端口,在 PORTS 中看不到映射关系。

  • 資深大佬 : hackshen

    看看这个 https://docs.docker.com/network/host/
    The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

  • 資深大佬 : anubu

    ports 部分配置了“80:80”,网络模式没必要了,建议取消配置。应该也不是网络模式导致的异常,建议从权限问题入手排查:
    1. 端口号低于 1024 可能需要特殊权限,确认权限是否足够,可以试试高位端口映射,如“2080:80”
    2. 看报错日志,首先考虑排查挂载的文件权限。如果是手动在外部创建了目录和文件,挂载后可能会有文件权限问题

  • 資深大佬 : aaa5838769

    你本机有没有 80 端口开放?不包括 nginx

  • 資深大佬 : Reficul

    “`
    10-listen-on-ipv6-by-default.sh: error: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
    “`

    你这个配置又是 Volume 挂进去的,权限问题基本上是。

  • 資深大佬 : Xusually

    can not modify /etc/nginx/conf.d/default.conf (read-only file system?)

  • 資深大佬 : wfd0807

    docker for mac 运行容器的宿主实际上是 qemu 虚拟机,所以 host 模式在 mac 上不支持

  • 主 資深大佬 : css3

    @StarUDream 哦哦,就是说 network 是 host 时,端口不用映射了,容器外访问任何端口都会指向容器内对应的端口,是这个意思吧?但我看下有人说 mac 的 dodocker 不支持 hohost 网络模式

  • 主 資深大佬 : css3

    @css3 @StarUDream @hackshen @anubu @aaa5838769 @Reficul @Xusually @wfd0807
    老哥们再看看,我说 append 原始需求和最新遇到的问题了

  • 資深大佬 : wfd0807

    @css3 host.docker.internal 是从容器内访问 qemu 虚拟机的宿主机用的

    proxy_pass http://127.0.0.1:8080; ==> proxy_pass http://host.docker.internal:8080;

  • 主 資深大佬 : css3

    @wfd0807 我的需求是:本地请求=>nginx 容器转发=>返回本地,如何实现呢

  • 資深大佬 : no1xsyzy

    @css3 #9 俺寻思你看看 #2 和 #7 吧
    network_mode: host 不可用

  • 資深大佬 : no1xsyzy

    docker 还是 ports: [“80:80”]
    nginx 的代理上游不应该是 127.0.0.1 而应该是 host.docker.internal

  • 資深大佬 : anubu

    看上去是要利用容器中的 nginx 代理本地宿主机上的一个程序,目前的 proxy_pass 配置肯定是错误的。没用过 Mac 版,按照 Windows 版推测,当前环境至少有三层网络,宿主机》虚拟机》容器,三层分别有各自的 localhost,当前配置挂载到容器中的 nginx 就是反代容器的 localhost:8080,应该是不符合预期的,实际需求应该是希望反代宿主机的 localhost:8080 。

    如果宿主机程序监听 0.0.0.0:8080,可简单修改 prox_pass 为宿主机 IP:8080,应该能达到预期。

  • 資深大佬 : hackshen

    同意 14 nginx 里面配置 proxy_pass http://宿主机 IP:端口号;
    如果你电脑 IP 每天更换可以尝试在宿主机修改 host 绑定 ,比如 xxx.xxx.xxx.xx test.baidu.com, nginx 里面配置 proxy_pass http://test.baidu.com;

  • 主 資深大佬 : css3

    @css3 @StarUDream @hackshen @anubu @aaa5838769 @Reficul @Xusually @wfd0807 @no1xsyzy

    老哥们,解决了,就是 nginx 容器内配置本地的 ip 要写成像 13 老哥的那种,多谢各位了!

  • 主 資深大佬 : css3

    @wfd0807 对的,这样解决了问题,多谢老哥指点

  • 資深大佬 : lyhapple

    docker-compose.yml 里加入:

  • 資深大佬 : lyhapple

    networks:
    backend:
    name: backend_network
    driver: bridge

    然后容器里指定:
    nginx:
    // 其它配置
    networks:
    – backend

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具