1

I'm trying to use a following config:

docker-compose.yml

version: "3"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: onjin/alpine-postgres
    environment:
      POSTGRES_PASSWORD: password

The other file is Dockerfile:

FROM alpine

RUN apk update && apk add --no-cache postgresql-client
COPY Bot/ /Bot

ENV PGHOST=db PGPASSWORD=password

RUN psql -h "$PGHOST" -f /Bot/test/database_schema.sql

I have no idea why I always get this error while running "docker-compose up":

psql: could not translate host name "db" to address: Name does not resolve

Can anyone help me with debugging this? Seems like the "db" hostname is not being propagated inside the docker environment, but don't know the reason for that.

3
  • depends_on guarantees that you container is up, but it does not guarantee that the database is ready to receive connections. It takes some time for Postgres to be ready after the container starts. Try to see the logs and try to exec into your image and run the command manually after you give some time to Postrgres to get up. Commented Jan 23, 2019 at 17:13
  • @Mark But the hostname should resolve without the database being fully started, shouldn't it? Commented Jan 23, 2019 at 19:37
  • What is strange for me is that different compositions (those examples from Docker website) works even though they got DB+Python. Just my own project does not. Commented Jan 23, 2019 at 20:07

1 Answer 1

3

The issue you are seeing is related to the fact that docker-composer runs services in the same order as those are defined in the yaml file. So basically the moment when you run your web service db service does not exists yet so it's hostname is not resolvable.

If you change the order in the docker-compose.yaml:

version: "2"

services:
   db:
    image: onjin/alpine-postgres
    environment:
      POSTGRES_PASSWORD: password 
   web:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - "db"
    tty: true

and run docker-compose up -d you won't see the error anymore, service will be up:

sudo docker-compose ps
    Name                   Command               State           Ports          
-------------------------------------------------------------------------------
db_1    /docker-entrypoint.sh postgres   Up      5432/tcp               
web_1   /bin/sh                          Up      0.0.0.0:3000->3000/tcp 

and hostname is correctly resolvable:

sudo docker-compose run web "ping" "db"
PING db (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.096 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.101 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.097 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.106 ms
Sign up to request clarification or add additional context in comments.

2 Comments

depends_on already fixes the order issue.
The order of the service doesn't matter, when using depends_on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.