All jobs / DevOps / SRE / cloud / Containers
DevOps / SRE / cloud interview question · 1–3 years
The app container connects to `localhost:5432` and gets connection refused, though the database container is running. Why?
The strongest answer
Each container has its own network namespace, so localhost is the app container itself; on a Compose or user-defined network the containers resolve each other by service name — connect to `db:5432`
Why it works
Exactly — container networking gives each container its own loopback and embedded DNS by service name.
What a good answer covers
- localhost is this container.
- Own namespace per container.
- Use the service name.
- Do not host-network your way out.
Answers that fall short
- Close, but not the strongest: Use the database container’s IP address from `docker inspect` instead of localhost
It works until the IP changes on the next recreate; names are the stable interface. - Weak: Postgres has not finished starting; add a sleep before the app connects
Startup order can be a second problem, but refused-on-localhost is the namespace, not timing. - Red flag: Set `network_mode: host` on both containers so everything is on localhost again
Host networking throws away isolation and port mapping to avoid learning how the network works.
Who gets asked this
More Containers questions
- The Dockerfile has no USER line, so the app runs as root. Does that matter inside a container?
- The container registry has grown to terabytes, pulls from the cluster are slow, and a public registry started…
- What is the difference between an image and a container?
- A Dockerfile says `FROM node:latest`. What is wrong?
- A container can take all the memory on the node. What do you set?
- In the team’s Docker Compose stack, Postgres loses all its data every time the container is recreated. Why…
- CI rebuilds a 12-minute Docker image on every commit. What do you change first?
- A production image is 1.8 GB and includes a compiler. What do you do?