DEV Community

Yash Sonawane
Yash Sonawane

Posted on

Docker Series: Episode 22 โ€” Docker Networking Advanced: Multi-Host & Overlay Networks ๐ŸŒ

Welcome back! After covering basic networking, Docker Compose, Swarm, and logging, itโ€™s time to tackle advanced Docker networking for multi-host setups. This is essential when deploying scalable applications across multiple machines.


๐Ÿ”น Why Advanced Networking?

  • Containers need to communicate across different hosts.
  • Overlay networks allow seamless communication between containers on different machines.
  • Useful for Swarm clusters or distributed applications.

๐Ÿ”น Overlay Network

  • Overlay networks connect multiple Docker daemons (hosts) together.
  • Automatically encrypts traffic between nodes in a Swarm.

Creating an Overlay Network

docker network create -d overlay my_overlay
Enter fullscreen mode Exit fullscreen mode
  • -d overlay specifies the overlay driver.

Connecting Services

docker service create --name web --replicas 3 --network my_overlay nginx
Enter fullscreen mode Exit fullscreen mode
  • Services can now communicate across nodes transparently.

๐Ÿ”น Multi-Host Networking Example

  1. Initialize Swarm on the first host:
docker swarm init --advertise-addr <MANAGER-IP>
Enter fullscreen mode Exit fullscreen mode
  1. Join worker nodes:
docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377
Enter fullscreen mode Exit fullscreen mode
  1. Deploy a service on the overlay network.
  2. Check communication between replicas across nodes:
docker service ps web
docker exec -it <container_id> ping <other_container_name>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Macvlan Networks

  • Allows containers to appear as physical devices on the network.
  • Useful for legacy apps or apps that need direct LAN access.

Example:

docker network create -d macvlan \
  --subnet=192.168.1.0/24 \
  --gateway=192.168.1.1 \
  -o parent=eth0 my_macvlan
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Best Practices

  • Use overlay networks for multi-host apps.
  • Use bridge networks for isolated services on a single host.
  • Avoid exposing all ports publicly.
  • Use VLAN or Macvlan only when necessary for network integration.

๐Ÿ”น Hands-On Challenge

  1. Create an overlay network.
  2. Deploy a 3-replica Nginx service across Swarm nodes.
  3. Test inter-container communication.
  4. Experiment with Macvlan for a container needing LAN access.

โœ… Next Episode: Episode 23 โ€” Docker Swarm Advanced: Services, Secrets & Configs โ€” mastering orchestration features for production-ready deployments.

Top comments (0)