Connection to server at "127.0.0.1", port 5432 failed: Connection refused

FastAPI container fails to connect to PostgreSQL.
However, the PostgreSQL container is running well and accessible.
I don't know why this problem is happening.
Here is gist link of Dockerfile, docker-compose.yaml and log when docker-compose is run. https://gist.github.com/sunwoo1524/9f75b4d3fd295c9829705e23a3b8094d

4 points · 4 comments · view on lemmy.world

4 Comments

superpants@lemmy.world · 1 pts · 3y (3 replies)

The python container should connect on postgres:5432 not localhost. To the web container, localhost is itself, not the physical host running docker

sunwoo1524@lemmy.world · 1 pts · 3y (2 replies)

I changed host to postgres:5432, but python made an error.

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: Temporary failure in name resolution
superpants@lemmy.world · 2 pts · 3y (1 reply)

Sorry, I misread your compose file. It should be the name of the PG container, so db:5432

sunwoo1524@lemmy.world · 1 pts · 3y

Thanks!
I also added

depends_on:
      - db

to docker-compose.yaml and fixed the issue!

superpants@lemmy.world · 1 pts · 3y
[ removed ]