【问题标题】:docker-compose fails to resolve service hostnamedocker-compose 无法解析服务主机名
【发布时间】:2017-05-22 13:53:56
【问题描述】:

Docker 文档说,同一个 compose 文件中的每个容器都可以通过使用它们的服务名称来相互访问。这似乎适用于我所有的容器,除了磅。

version: "3"
services:
  lb:
    image: nginx
    volumes:
      - ./conf/nginx:/etc/nginx/conf.d/default.conf
    ports:
      - "8080:80"
    environment:
      - NGINX_HOST=foobar.com
      - NGINX_PORT=80
    stdin_open: true
    tty: true

  worker1:
    build: ./rel_sync_worker/.
    stdin_open: true
    tty: true
    depends_on :
      - broker
      - lb

  worker2:
    build: ./rel_sync_worker/.
    stdin_open: true
    tty: true
    depends_on :
      - broker
      - lb

  broker:
    build: ./broker/.
    ports:
      - "4444:4444/udp"
    stdin_open: true
    tty: true
    depends_on:
      - lb

broker1.py

import socket
host = socket.gethostbyname("broker")
print "broker", host
host = socket.gethostbyname("worker1")
print "worker1", host

host = socket.gethostbyname("lb")
# host = "127.0.0.1"
print host
port = 5555

broker1 输出:

broker_1   | broker 172.18.0.4
broker_1   | worker1 172.18.0.2
broker_1   | Traceback (most recent call last):
broker_1   |   File "sendertest.py", line 11, in <module>
broker_1   |     host = socket.gethostbyname("lb")
broker_1   | socket.gaierror: [Errno -2] Name or service not known

【问题讨论】:

  • “lb” docker 容器的 docker 日志是什么?是不是因为某些原因放弃了你?

标签: python docker


【解决方案1】:

也许你应该给他们添加一个公共网络。

version: "3"
services:
  lb:
    image: nginx
    volumes:
      - ./conf/nginx:/etc/nginx/conf.d/default.conf
    ports:
      - "8080:80"
    environment:
      - NGINX_HOST=foobar.com
      - NGINX_PORT=80
    stdin_open: true
    tty: true
    networks:
      - common-network

  worker1:
    build: ./rel_sync_worker/.
    stdin_open: true
    tty: true
    depends_on :
      - broker
      - lb
    networks:
      - common-network   

  worker2:
    build: ./rel_sync_worker/.
    stdin_open: true
    tty: true
    depends_on :
      - broker
      - lb

  broker:
    build: ./broker/.
    ports:
      - "4444:4444/udp"
    stdin_open: true
    tty: true
    depends_on:
      - lb
    netwok:
      
networks:
  common-netwok:
    driver: overlay

如果您的 lb 容器正在运行 ofc,我看不出还有什么可能给您带来这种问题,也许您的 default.conf 文件不正确。

希望对你有帮助。

【讨论】:

  • 已解决。我猜是某种错误。感谢您的解决方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多