3

I'm unable to get my Phoenix app connecting to the Postgres container when using docker-compose up.

My docker-compose.yml:

version: '3.5'

services:
  web:
    image: "solaris_cards:latest"
    ports:
      - "80:4000"
    env_file:
      - config/docker.env
    depends_on:
      - db

  db:
    image: postgres:10-alpine
    volumes:
      - "/var/lib/postgresql/data/pgdata/var/lib/postgresql/data"
    ports:
      - "5432:5432"
    env_file:
      - config/docker.env

The application running in web container complains that a connection to the Postgres container is non-existing:

[error] Postgrex.Protocol (#PID<0.2134.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (db:5432): non-existing domain - :nxdomain

My env variables:

DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASS=postgres

I have tried running the Postgres container first separately and then running the web container but still have the same problem.

If I change the database host to 0.0.0.0 (which is what Postgres shows when running), then it seems to connect but the connection is refused rather than not found.

However docker should be able to translate the host name with out me manually inputing the ip.

Adam Bishti
  • 525
  • 2
  • 6
  • 16
  • Try command `getent hosts db` from your web container (docker exec -it web sh), it should show the IP of your db container. If it does not, then there is an issue with name resolution, if it does, there may be an issue with the db container – Pierre B. Mar 08 '19 at 09:31
  • @PierreB. I tried the command getent hosts db on my web container and it returned nothing. – Adam Bishti Mar 08 '19 at 10:01
  • Strange, I tried a similar version of your compose and it works: `getent hosts db 172.23.0.2 db db` Did you try down then up your compose? – Pierre B. Mar 08 '19 at 10:34
  • 1
    @PierreB. After further digging I found the problem. Postgres container was exiting due to the volume already containing data. Was able to clear the volume with docker-compose down -v and that solved the problem. Thank you for your help. – Adam Bishti Mar 11 '19 at 01:46

1 Answers1

1

Postgres was exiting due to its volume already containing data.

This was solved by cleaning the directory with:

docker-compose down -v
Adam Bishti
  • 525
  • 2
  • 6
  • 16