Frontend must reach the API but not the database. Segment the network.
IntermediateScenarioNetworking and Compose
Answer
Situation read → Least-privilege networking.
What I'd do →
services:
web: { networks: [front] }
api: { networks: [front, back] }
db: { networks: [back] }
networks:
front: {}
back: { internal: true }
Why → db shares no network with web; internal: true also blocks outbound internet for the back tier.
Interview tip → Network aliases give stable names per network.
More on Networking and Compose
Containers on the default bridge cannot resolve each other by name. Why, and what do you do?BeginnerHow do you expose a container only to the local host, not the internet?BeginnerDesign a Compose file for web + api + postgres + redis with correct startup ordering.IntermediateA container needs to call a service running on the Docker host.BeginnerWhen would you use `--network host`?IntermediateTwo Compose projects need to talk to each other.Intermediate