xalusigadu
xalusigadu

Reputation: 13

docker-compose fails to resolve service hostname

Docker documentation says that every container in the same compose file can be accessed from each other by using their service names. This seems to be working for all my containers except the lb.

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 output:

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

Upvotes: 1

Views: 2764

Answers (1)

wilsonW
wilsonW

Reputation: 374

Maybe you should add them a common network.

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

I don't see what else could give you that kind of problem if your lb container is working ofc, maybe your default.conf file isn't correct.

Hope it helps.

Upvotes: 1

Related Questions