在主机模式下运行 Docker 容器会导致依赖容器的网络问题。

huangapple go评论64阅读模式
英文:

Running Docker Container in Host Mode Causes Networking Problems For Dependent Containers

问题

我试图安装 这个服务 以在比特币的区块链上运行可视化程序。应用程序本身并不那么重要;关键是我正在尝试将其指向运行在本地主机而不是另一个 Docker 容器中的比特币 RPC 服务。因此,我需要以主机网络模式运行 "api" 服务。

问题是,当我这样做时,似乎在应用程序堆栈中的某处破坏了 nginx。以下错误来自我的 "web" 容器日志(docker-compose.yml 在下方供参考):

2023/03/03 22:22:54 [emerg] 1#1: 主机在 "/etc/nginx/conf.d/default.conf" 的上游未找到 "api:6000"
nginx: [emerg] 主机在 "/etc/nginx/conf.d/default.conf" 的上游未找到 "api:6000"

我无法弄清楚问题所在。当我将 "api" 服务从主机网络模式中移除时,突然 "web" 容器不再抛出上面的错误。但如果我这样做,显然 "api" 容器无法连接到本地主机上的比特币 RPC 服务。

如何使这个工作将不胜感激!

我的 docker-compose 文件如下:

version: "3"

services:
  web:
    image: ghcr.io/bitfeed-project/bitfeed-client:v2.3.4
    restart: on-failure
    stop_grace_period: 1m
    depends_on:
      - api
    environment:
      TARGET: docker
      BACKEND_HOST: api
      BACKEND_PORT: "6000" # 由于另一个服务已经使用端口 5000,因此进行了修改
    ports:
      - "3080:80" # 由于另一个服务已经使用端口 3000,因此进行了修改

  api:
    image: ghcr.io/bitfeed-project/bitfeed-server:v2.3.4
    user: "1000:1000"
    restart: on-failure
    stop_grace_period: 1m
    network_mode: host # 添加 network_mode: host 以便我可以访问本地机器上的比特币 RPC
    environment:
      PORT: "6000" # 由于另一个服务已经使用端口 5000,因此进行了修改
      BITCOIN_HOST: "127.0.0.1" # 修改为指向本地主机
      BITCOIN_ZMQ_RAWBLOCK_PORT: 28332
      BITCOIN_ZMQ_RAWTX_PORT: 28333
      BITCOIN_ZMQ_SEQUENCE_PORT: 28334
      BITCOIN_RPC_PORT: 8332
      BITCOIN_RPC_USER: raspibolt
      BITCOIN_RPC_PASS: [密码]

尝试过:

  1. 更改端口(无效果)
  2. 在桥接模式下运行 "api" 服务(无法以这种方式连接到本地主机上的比特币 RPC 服务)
  3. 尝试修改 "web" 服务容器中的 /etc/nginx/conf.d/default.conf(令人困惑的是,预期值 "api:6000" 已经出现在那里)

预期结果:API 连接到比特币 RPC 后端并同步内存池;然后 web 服务连接到端口 5000 上的 API,Web 应用程序在 localhost:3080 上变为可访问。

实际结果:API 连接到比特币 RPC 后端并同步内存池;但 web 服务一直崩溃,因为 /etc/nginx/conf.d/default.conf 中预期的上游值 "api:6000" 未找到。

英文:

I'm trying to install this service to run a visualizer on bitcoin's blockchain. The application itself isn't so important; the key point is that I'm trying to point it to my bitcoin RPC service that's running on the local host, not in another docker container. As such, I need to run the "api" service in host networking mode.

The problem is, when I do that, it seems to break nginx somewhere in the application stack. The following error is from the logs in my "web" container (docker-compose.yml is below for reference)

2023/03/03 22:22:54 [emerg] 1#1: host not found in upstream "api:6000" in /etc/nginx/conf.d/default.conf:42
nginx: [emerg] host not found in upstream "api:6000" in /etc/nginx/conf.d/default.conf:42

For the life of me I can't figure it out. When I take the "api" service off of host networking mode, suddenly the error above stops being thrown by my "web" container. But if I do that, the "api" container obviously is not able to connect to the bitcoin RPC service on localhost.

Any pointers on how to make this work would be very much appreciated!

My docker-compose file is:

version: "3"

services:
  web:
    image: ghcr.io/bitfeed-project/bitfeed-client:v2.3.4
    restart: on-failure
    stop_grace_period: 1m
    depends_on:
      - api
    environment:
      TARGET: docker
      BACKEND_HOST: api
      BACKEND_PORT: "6000" #modified because port 5000 is already in use by another service
    ports:
      - "3080:80" #modified because port 3000 is already in use by another service

  api:
    image: ghcr.io/bitfeed-project/bitfeed-server:v2.3.4
    user: "1000:1000"
    restart: on-failure
    stop_grace_period: 1m
    network_mode: host #added network_mode: host so that I can access bitcoin RPC on local machine
    environment:
      PORT: "6000" #modified because port 5000 is already in use by another service
      BITCOIN_HOST: "127.0.0.1" #modified to point to localhost
      BITCOIN_ZMQ_RAWBLOCK_PORT: 28332
      BITCOIN_ZMQ_RAWTX_PORT: 28333
      BITCOIN_ZMQ_SEQUENCE_PORT: 28334
      BITCOIN_RPC_PORT: 8332
      BITCOIN_RPC_USER: raspibolt
      BITCOIN_RPC_PASS: 
		
输入密码查看隐藏内容

Tried:

  1. Changing ports (no effect)
  2. Running "api" service in bridge mode (can't connect to bitcoin RPC service on localhost this way)
  3. Attempting to modify /etc/nginx/conf.d/default.conf in the "web" service container (confusingly, the expected value of "api:6000" already appears to be there)

Expected result: the API connects to the bitcoin RPC back end and synchronizes mempool; the web service then connects to the API on port 5000 and the web app becomes accessible at localhost:3080

Actual result: the API connects to the bitcoin RPC back end and synchronizes mempool; the web service keeps dying because "api:6000" is expected as an upstream value in /etc/nginx/conf.d/default.conf but is not found.

答案1

得分: 0

  1. webapi服务都指定network_mode: host。请注意,端口映射与主机网络模式不兼容。这意味着web服务将尝试使用127.0.0.1:80。确保端口80可用,或者让web使用其他端口;
  2. web服务寻找API时使用127.0.0.1:6000,而不是api:6000。如果我理解正确,您可以通过修改docker-compose.ymlwebBACKEND_HOST环境变量来实现这一点。

Explanation: 为了使这三个部分能够良好通信,它们应该在同一个网络上。您有两个选项:主机网络模式或Docker桥接网络。如果您不想将RPC比特币服务纳入docker-compose并使其成为其中一部分,那么您只能将webapi服务部署在主机网络中。这样一做,api不再在api:6000可用,因为它是桥接网络的地址。相反,它在主机网络上可用,因此是127.0.0.1:6000

英文:

You need to:

  1. Specify network_mode: host for both web and api services. Note that port mapping is incompatible with network mode host. It means that web service will try to use your 127.0.0.1:80. Make sure port 80 is available or make web use some other port;
  2. Make the web service look for the API at 127.0.0.1:6000 instead of api:6000. You should be able to achieve this by modifying the web's BACKEND_HOST env variable in docker-compose.yml if I understand correctly.

Explanation: for all 3 pieces to communicate well they should be on the same network. You have 2 options: host or docker bridge. If you don't want to dockerize your RPC bitcoin service and make it a part of docker-compose you are left with the option to deploy web and api services in the host network. When you do it, api is no longer available at api:6000 because it's the bridge network address. Instead, it becomes available at the host network, hence 127.0.0.1:6000.

huangapple
  • 本文由 发表于 2023年3月4日 06:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定