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

2020-08-19 16:19:55 +08:00
 css3

大佬们请教个问题:

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

遇到问题( 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
5251 次点击
所在节点    程序员
19 条回复
StarUDream
2020-08-19 16:32:14 +08:00
指定为 "host" 时,所有容器端口都对应属主机端口,在 PORTS 中看不到映射关系。
hackshen
2020-08-19 17:17:51 +08:00
看看这个 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
2020-08-19 17:28:14 +08:00
ports 部分配置了“80:80”,网络模式没必要了,建议取消配置。应该也不是网络模式导致的异常,建议从权限问题入手排查:
1. 端口号低于 1024 可能需要特殊权限,确认权限是否足够,可以试试高位端口映射,如“2080:80”
2. 看报错日志,首先考虑排查挂载的文件权限。如果是手动在外部创建了目录和文件,挂载后可能会有文件权限问题
aaa5838769
2020-08-19 17:33:05 +08:00
你本机有没有 80 端口开放?不包括 nginx
Reficul
2020-08-19 18:51:40 +08:00
```
10-listen-on-ipv6-by-default.sh: error: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
```

你这个配置又是 Volume 挂进去的,权限问题基本上是。
Xusually
2020-08-19 19:13:07 +08:00
can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
wfd0807
2020-08-19 19:36:53 +08:00
docker for mac 运行容器的宿主实际上是 qemu 虚拟机,所以 host 模式在 mac 上不支持
css3
2020-08-19 20:54:14 +08:00
@StarUDream 哦哦,就是说 network 是 host 时,端口不用映射了,容器外访问任何端口都会指向容器内对应的端口,是这个意思吧?但我看楼下有人说 mac 的 dodocker 不支持 hohost 网络模式😂
css3
2020-08-19 21:06:49 +08:00
@css3 @StarUDream @hackshen @anubu @aaa5838769 @Reficul @Xusually @wfd0807
老哥们再看看,我说 append 原始需求和最新遇到的问题了
wfd0807
2020-08-19 21:30:42 +08:00
@css3 host.docker.internal 是从容器内访问 qemu 虚拟机的宿主机用的

proxy_pass http://127.0.0.1:8080; ==> proxy_pass http://host.docker.internal:8080;
css3
2020-08-19 21:44:53 +08:00
@wfd0807 我的需求是:本地请求=>nginx 容器转发=>返回本地,如何实现呢
no1xsyzy
2020-08-20 09:36:01 +08:00
@css3 #9 俺寻思你看看 #2 和 #7 吧
network_mode: host 不可用
no1xsyzy
2020-08-20 09:37:40 +08:00
docker 还是 ports: ["80:80"]
nginx 的代理上游不应该是 127.0.0.1 而应该是 host.docker.internal
anubu
2020-08-20 09:56:45 +08:00
看上去是要利用容器中的 nginx 代理本地宿主机上的一个程序,目前的 proxy_pass 配置肯定是错误的。没用过 Mac 版,按照 Windows 版推测,当前环境至少有三层网络,宿主机》虚拟机》容器,三层分别有各自的 localhost,当前配置挂载到容器中的 nginx 就是反代容器的 localhost:8080,应该是不符合预期的,实际需求应该是希望反代宿主机的 localhost:8080 。

如果宿主机程序监听 0.0.0.0:8080,可简单修改 prox_pass 为宿主机 IP:8080,应该能达到预期。
hackshen
2020-08-20 11:06:37 +08:00
同意 14 楼 nginx 里面配置 proxy_pass http://宿主机 IP:端口号;
如果你电脑 IP 每天更换可以尝试在宿主机修改 host 绑定 ,比如 xxx.xxx.xxx.xx test.baidu.com, nginx 里面配置 proxy_pass http://test.baidu.com;
css3
2020-08-20 11:11:51 +08:00
@css3 @StarUDream @hackshen @anubu @aaa5838769 @Reficul @Xusually @wfd0807 @no1xsyzy

老哥们,解决了,就是 nginx 容器内配置本地的 ip 要写成像 13 楼老哥的那种,多谢各位了!
css3
2020-08-20 11:12:37 +08:00
@wfd0807 对的,这样解决了问题,多谢老哥指点
lyhapple
2020-08-20 11:18:03 +08:00
docker-compose.yml 里加入:
lyhapple
2020-08-20 11:19:51 +08:00
networks:
backend:
name: backend_network
driver: bridge

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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/699660

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX