Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose: App can not connect to Postgres container

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.

like image 244
Adam Bishti Avatar asked May 29 '26 13:05

Adam Bishti


1 Answers

Postgres was exiting due to its volume already containing data.

This was solved by cleaning the directory with:

docker-compose down -v
like image 101
Adam Bishti Avatar answered May 31 '26 06:05

Adam Bishti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!