0
0
Dockerdevops~5 mins

Overlay networks in Swarm in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple Docker containers on different machines, they need a way to talk to each other securely and easily. Overlay networks in Docker Swarm create a private network that connects containers across these machines as if they were on the same local network.
When you want containers on different servers to communicate without exposing ports to the outside world.
When you deploy a multi-service app using Docker Swarm and need services to find each other by name.
When you want to isolate your app's network traffic from other apps running on the same servers.
When you want to scale your app across multiple machines and keep the network seamless.
When you want to use Docker's built-in service discovery and load balancing features.
Commands
This command creates a new overlay network named 'my-overlay-network' that Docker Swarm services and containers can use to communicate across multiple hosts.
Terminal
docker network create --driver overlay my-overlay-network
Expected OutputExpected
3v7xqz7k8kq7k8kq7k8kq7k8
--driver overlay - Specifies that the network type is overlay, which works across multiple Docker hosts.
This command creates a new Docker service named 'my-service' that runs the nginx container and connects it to the 'my-overlay-network' so it can communicate with other services on the same network.
Terminal
docker service create --name my-service --network my-overlay-network nginx
Expected OutputExpected
q1w2e3r4t5y6u7i8o9p0
--name - Names the service for easy reference.
--network - Connects the service to the specified overlay network.
This command lists all running Docker services so you can verify that 'my-service' is up and running.
Terminal
docker service ls
Expected OutputExpected
ID NAME MODE REPLICAS IMAGE PORTS q1w2e3r4t5y6u7i8o9p0 my-service replicated 1/1 nginx:latest
This command shows detailed information about the 'my-overlay-network', including which containers and services are connected to it.
Terminal
docker network inspect my-overlay-network
Expected OutputExpected
[ { "Name": "my-overlay-network", "Id": "3v7xqz7k8kq7k8kq7k8kq7k8", "Scope": "swarm", "Driver": "overlay", "Containers": { "q1w2e3r4t5y6u7i8o9p0": { "Name": "my-service.1.xxxxx", "EndpointID": "abcdef123456", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "10.0.0.2/24", "IPv6Address": "" } } } ]
Key Concept

If you remember nothing else from this pattern, remember: overlay networks let containers on different Docker hosts communicate securely as if they were on the same local network.

Common Mistakes
Creating an overlay network without initializing Docker Swarm mode.
Overlay networks require Docker Swarm mode to be active; otherwise, the network creation will fail.
Run 'docker swarm init' on your manager node before creating overlay networks.
Not attaching services or containers to the overlay network.
If services are not connected to the overlay network, they cannot communicate across hosts using that network.
Use the '--network' flag when creating or updating services to connect them to the overlay network.
Trying to connect standalone containers (not part of a service) to an overlay network on different hosts without extra setup.
Standalone containers cannot join overlay networks across hosts unless using swarm services or special configurations.
Use Docker services for multi-host networking or use other networking solutions for standalone containers.
Summary
Create an overlay network with 'docker network create --driver overlay' to enable multi-host container communication.
Deploy services connected to the overlay network using 'docker service create --network'.
Verify services and network connections with 'docker service ls' and 'docker network inspect'.