What is a Docker network?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Docker topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
A Docker network provides connectivity between containers and controls how they communicate with each other and with the outside world. Docker automatically creates three default networks: bridge — the default for standalone containers; host — uses the host's network directly; none — no networking. User-defined bridge networks are recommended over the default bridge: they provide automatic DNS resolution (containers can find each other by service name/container name, not just IP), better isolation, and the ability to dynamically add/remove containers. Create: docker network create mynet. Connect at run time: docker run --network mynet myapp. Connect running container: docker network connect mynet mycontainer. With Docker Compose, all services in the same Compose project automatically share a user-defined network and can reach each other by service name. Container DNS: in a user-defined network, ping db resolves to the db container's IP. Inspect network: docker network inspect mynet. Port exposure: EXPOSE in Dockerfile documents the port; -p 8080:3000 in docker run actually publishes it to the host (bind to 0.0.0.0:8080 by default — bind to 127.0.0.1:8080 to restrict to localhost).
Common Mistake
Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Docker experience.