0
0
DockerConceptBeginner · 4 min read

Docker Network Types: Overview and Usage Examples

Docker provides several 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

This example shows how to create and use a bridge network and run two containers connected to it.
bash
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 container2
Output
PING container2 (172.18.0.3): 56 data bytes 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.123 ms 64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.110 ms --- container2 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss
🎯

When 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.

Key Takeaways

Docker network types control how containers communicate and connect to networks.
Use bridge for isolated single-host container communication.
Host network shares the host's network stack for performance or port needs.
Overlay networks enable multi-host container communication in clusters.
None disables networking for isolated container tasks.