简体   繁体   中英

Docker container in docker-compose can't find other containers by hostname

I am upgrading a docker-compose file from v1 to v3 and have a problem to link the containers together.

Before the docker-compose.yml was this:

database:
  working_dir: /mnt
  volumes:
    - .:/mnt
  links:
   - postgresql_db
  ports:
    - '3000:3000'
  build: ./database
  entrypoint: python database/server.py

postgresql_db:
  build: ./database
  ports:
    - '5432:5432'

test_database:
  working_dir: /mnt
  volumes:
    - .:/mnt
  links:
    - test_postgresql_db
  ports:
    - '5053:5053'
  build: ./database/test

test_postgresql_db:
  image: postgres:latest
  ports:
    - '5432:5432'

But links has been deprecated. Now containers should share a network (which they should do by default) and be able to find each other using hostnames (see: here ).

So I modified the file to:

version: '3'

networks:
  dbnet:
    driver: bridge

volumes:
  postgresql_data: {}
  postgresql_test_data: {}

services:
  database:
    build: database
    environment:
      APPLICATION_DB_CONTAINER: db
      APPLICATION_POSTGRES_HOST: db
    working_dir: /mnt
    volumes:
      - .:/mnt
    networks:
      - dbnet
    ports:
      - '3000:3000'
    command: python database/server.py

  db:
    image: postgres:latest
    volumes:
      - postgresql_data:/var/lib/postgresql/data
    networks:
      - dbnet
    depends_on:
      - database

  test_database:
    build: database/test
    environment:
      APPLICATION_DB_CONTAINER: testdb
    working_dir: /mnt
    volumes:
      - .:/mnt
    command: python -m pytest --cov=database --cov-report term --cov-report html:htmlcov database/test/

  testdb:
    image: postgres:latest
    volumes:
      - postgresql_test_data:/var/lib/postgresql/data

The problem I have is that the database container can't find the db container by hostname and I get (my software tries to connect every second for 10 seconds and then stops):

ERROR: Database postgresql://postgres:@db:5432/postgres NOT found (10 attempts)

I checked and the db container is up, if I use its IP address then it works but using the hostname does not.

How can I link the database and db container and make sure it finds it each time ?

Info: I run docker Version 18.06.0-ce-mac70 on MacOs.

Thank you !

EDIT: Tried to add this in the compose file for db, but did not change the problem.

ports: '5432:5432'

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Name or service not known database_1 | (Background on this error at: http://sqlalche.me/e/e3q8 )

I get this error from sqlalchemy that indicates that the database container does no know what db (or db-1) is and the file /etc/hosts does not contain any mention of another container

As @sachav indicated in the comments, I inverted the depends_on.

In the db service,

db:
  depends_on:
    - database
...

forced the db service to be started before database, thus preventing database to know that the host db existed !

Taking it out and adding

database:
  depends_on:
    - db
...

made the trick :)

Thank you @sachav, I'll go face palm myself now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM