Docker · Containers in practice
How does Docker networking work?
Short answer
Docker creates virtual networks and attaches containers to them. User-defined bridge networks provide automatic DNS by container or service name, which is how containers find each other.
The default bridge network connects containers but does not provide automatic name resolution, so containers must use IP addresses or legacy links. A user-defined bridge network provides an embedded DNS server, so a container can reach another by its name or network alias.
docker network create app-net
docker run -d --name db --network app-net postgres:16
docker run --rm --network app-net alpine sh -c "getent hosts db"Network drivers
- bridge: the default for standalone containers on one host
- host: shares the host network namespace, removing port isolation
- none: no networking, useful for isolated batch jobs
- overlay: connects containers across multiple hosts in a cluster
- macvlan: gives a container a MAC address on the physical network
Only containers on the same network can reach each other directly. That is a useful segmentation tool: put the database and application on an internal network and expose only the proxy to the outside.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does the default bridge not resolve container names?
- 2When would you use host networking?
- 3How do you isolate a database from the public network?
- 4What is an overlay network used for?
Say it in the interview
Docker creates virtual networks and attaches containers to them. I use a user-defined bridge network because it gives embedded DNS, so the app reaches the database by service name. I also use separate networks for segmentation, keeping the database off the public-facing network and exposing only the proxy. Host and overlay modes solve different problems, but both change the isolation model.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Docker mock interview and get scored feedback on depth, clarity and confidence.