Reputation: 649
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.
Upvotes: 1
Views: 6667
Reputation: 428
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
Upvotes: 3