Docker Network Types: Overview and Usage Examples
network types to connect containers: bridge for isolated networks, host to share the host's network, overlay for multi-host communication, and none for no networking. Each type serves different use cases depending on container communication needs.How It Works
Think of Docker network types like different ways to connect devices in a home or office. The bridge network acts like a private Wi-Fi router that connects containers inside the same machine, letting them talk to each other but isolated from the outside.
The host network is like plugging a device directly into the main internet cable, sharing the host's network settings without isolation. The overlay network connects containers across multiple machines, similar to a virtual private network (VPN) linking offices in different locations. Lastly, the none network means the container has no network connection, like a device turned off from the network.
Example
docker network create my-bridge-network
docker run -dit --name container1 --network my-bridge-network alpine sh
docker run -dit --name container2 --network my-bridge-network alpine sh
docker exec container1 ping -c 2 container2When to Use
Use the bridge network for simple container communication on a single host, like running a web server and database together.
Choose host network when you want the container to use the host's network directly, useful for performance-sensitive apps or when ports must match exactly.
Overlay networks are best for multi-host Docker Swarm or Kubernetes clusters where containers on different machines need to communicate securely.
Use none when you want a container completely isolated from any network, for example, running a task that does not require internet or network access.
Key Points
- Bridge: Default, isolated network for containers on the same host.
- Host: Shares host network, no isolation.
- Overlay: Connects containers across multiple hosts.
- None: No network access for the container.