docker-compose无法解析服务主机名

时间:2017-05-22 13:53:57

标签: python docker

Docker文档说,可以使用服务名称相互访问同一个撰写文件中的每个容器。除了lb。

之外,这似乎适用于我的所有容器
$ awk 'BEGIN   {FS=OFS=";"} 
       NR==FNR {a[$1]=$2; next} 
       $1 in a {print $0,a[$1]}' file2 file1

A;1;30
B;2;20
A;3;30

broker1.py

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输出:

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

1 个答案:

答案 0 :(得分:0)

也许你应该为他们添加一个共同的网络。

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
    netwok:
      - common-network

  worker1:
    build: ./rel_sync_worker/.
    stdin_open: true
    tty: true
    depends_on :
      - broker
      - lb
    netwok:
      - 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容器正在工作,我不知道还有什么能给你带来这样的问题,也许你的default.conf文件不正确。

希望它有所帮助。