CodePark/ questions / Docker Compose
START PRACTICINGPRACTICE
Backend question · Docker › Docker Compose

In a Compose project with services api and db (Postgres, ports: ["5433:5432"]), what host and port should the API use to connect to the database?

db:5432

The API should use db:5432. Inside the Compose network, the API reaches Postgres by its service name and the container's own port.

"
db:5433" is wrong because the 5433 mapping only applies to connections from the host. "localhost:5433" and "localhost:5432" are wrong because localhost inside the API container means the API container itself, not the database.