Docker Networking
Networking in Docker allows containers to talk to each other, the host machine, and external services. Understanding Docker networking is essential for building scalable applications.
Docker Networking – Bridge, Host, Overlay, and Container Communication
Networking in Docker allows containers to talk to each other, the host machine, and external services. Understanding Docker networking is essential for building scalable applications.
Default Networks in Docker
When you install Docker, three networks are created automatically:
- bridge – Default network for standalone containers.
- host – Shares the host machine's network stack.
- none – Completely isolated, no network access.
Bridge Network (Default)
- The default mode for containers if no network is specified.
- Containers get a private IP and communicate via a virtual bridge (docker0).
- Best for running multiple containers on a single host.
Example:
docker run -d --name container1 --network bridge nginxHost Network
- Removes network isolation between container and host.
- The container shares the host's IP address and ports.
- Useful for performance-critical apps (e.g., game servers).
Example:
docker run -d --network host nginxNone Network
- No networking.
- Container has only a loopback interface.
- Used for security or manual network setups.
Example:
docker run -d --network none nginxUser-Defined Bridge Network
Custom networks allow containers to resolve each other by name.
Example:
docker network create my_network
docker run -d --name app1 --network my_network nginx
docker run -d --name app2 --network my_network alpine sleep 1000
# Inside app2 container, you can ping app1 by name:
docker exec -it app2 ping app1Overlay Network (Multi-Host)
- Used in Docker Swarm or Kubernetes.
- Connects containers across multiple Docker hosts.
- Requires cluster setup.
Example (Docker Swarm):
docker swarm init
docker network create -d overlay my_overlay
docker service create --name my_service --network my_overlay nginxInspecting Networks
List all networks:
docker network lsInspect details of a specific network:
docker network inspect my_networkConnecting & Disconnecting Containers
Connect a running container to another network:
docker network connect my_network app1Disconnect a container from a network:
docker network disconnect my_network app1Real-World Use Case
- Bridge: Local development with multiple services (API, DB, frontend).
- Host: Performance-heavy apps like video streaming.
- Overlay: Distributed microservices in production clusters.
- None: Secure isolated workloads.
Example: In a typical dev setup, your backend API and database run on a user-defined bridge, so API can call DB using container name instead of IP.
Summary
- Docker provides multiple networking modes: bridge, host, none, and overlay.
- User-defined networks make container-to-container communication easier.
- Overlay networks enable multi-host clusters.
In the next article, we'll cover Docker Volumes & Persistent Storage – Managing Data in Containers.