0
0
DockerConceptBeginner · 3 min read

What Is Overlay Network in Docker: Simple Explanation and Example

An overlay network in Docker is a virtual network that connects multiple Docker hosts so containers on different machines can communicate securely. It works by creating a network layer on top of existing host networks, allowing containers across hosts to behave as if they are on the same local network.
⚙️

How It Works

Imagine you have several houses (Docker hosts) in different locations, and you want people (containers) inside these houses to talk to each other as if they were in the same building. An overlay network acts like a private tunnel or bridge connecting all these houses, so the people inside can chat freely without leaving their rooms.

Technically, Docker creates this overlay network by using a technology called VXLAN, which wraps container network traffic inside packets that travel over the existing physical network. This lets containers on different hosts communicate securely and directly, even if the hosts are far apart.

This network is managed by Docker’s built-in swarm mode or other orchestrators, which handle the setup and routing automatically, so you don’t have to configure complex network settings manually.

💻

Example

This example shows how to create an overlay network and run two containers on different Docker hosts that can communicate through it.

bash
docker network create -d overlay my_overlay_network

docker service create --name service1 --network my_overlay_network nginx

docker service create --name service2 --network my_overlay_network busybox sleep 3600
Output
bfq2k1x9z8q1 service1 service2
🎯

When to Use

Use an overlay network when you want containers running on different physical or virtual machines to communicate as if they were on the same local network. This is common in:

  • Docker Swarm clusters where services need to talk across hosts.
  • Multi-host container deployments in production environments.
  • Scaling applications horizontally across multiple servers.

Overlay networks simplify networking in distributed systems by hiding the complexity of the underlying infrastructure.

Key Points

  • Overlay networks connect containers across multiple Docker hosts.
  • They create a virtual network on top of existing physical networks.
  • Docker manages overlay networks automatically in swarm mode.
  • They enable secure and seamless container communication in distributed setups.

Key Takeaways

Overlay networks let containers on different hosts communicate as if on the same local network.
They work by creating a virtual network layer over existing host networks using VXLAN technology.
Docker swarm mode manages overlay networks automatically for easy multi-host container communication.
Use overlay networks for scalable, distributed container applications across multiple machines.