📖 Scenario: You are setting up a Docker Swarm cluster to run multiple services that need to communicate securely across different nodes. To enable this, you will create an overlay network that spans all nodes in the swarm.
🎯 Goal: Learn how to create an overlay network in Docker Swarm and attach services to it for cross-node communication.
📋 What You'll Learn
Create a Docker overlay network named my_overlay
Create a service named web attached to my_overlay
Create a service named db attached to my_overlay
Verify the services are running and connected to the overlay network
💡 Why This Matters
🌍 Real World
Overlay networks in Docker Swarm let multiple containers communicate securely across different physical or virtual machines, useful for scalable microservices.
💼 Career
Understanding overlay networks is essential for DevOps roles managing container orchestration and multi-host networking in production environments.
Progress0 / 4 steps
1
Initialize Docker Swarm
Run the command docker swarm init to initialize the current node as a swarm manager.
Docker
Hint
Use docker swarm init to start the swarm on your current machine.
2
Create an overlay network
Create an overlay network named my_overlay using the command docker network create --driver overlay my_overlay.
Docker
Hint
Use docker network create --driver overlay my_overlay to create the overlay network.
3
Deploy services attached to the overlay network
Create two services: web running the nginx image and db running the redis image. Attach both services to the my_overlay network using --network my_overlay.
Docker
Hint
Use docker service create --name web --network my_overlay nginx and similarly for the db service.
4
Verify services and network connectivity
Run docker service ls to list the running services and docker network inspect my_overlay to check that the services are connected to the overlay network.
Docker
Hint
Use docker service ls and docker network inspect my_overlay to verify.
Practice
(1/5)
1. What is the main purpose of an overlay network in Docker Swarm?
easy
A. To provide a graphical interface for managing containers
B. To isolate containers within the same host only
C. To store container logs centrally
D. To enable communication between containers on different Docker hosts
Solution
Step 1: Understand overlay network function
An overlay network connects containers running on different Docker hosts, allowing them to communicate as if on the same network.
Step 2: Compare options
Options A, B, and D describe other features unrelated to overlay networks. Only To enable communication between containers on different Docker hosts correctly describes the main purpose.
Final Answer:
To enable communication between containers on different Docker hosts -> Option D
Quick Check:
Overlay network = cross-host container communication [OK]
Hint: Overlay networks connect containers across hosts, not just one host [OK]
Common Mistakes:
Thinking overlay networks only work on one host
Confusing overlay networks with logging or UI features
Assuming overlay networks isolate containers instead of connecting them
2. Which command correctly creates an overlay network named mynetwork in Docker Swarm?
easy
A. docker network create --driver overlay mynetwork
B. docker network create --driver bridge mynetwork
C. docker swarm network create mynetwork
D. docker create network overlay mynetwork
Solution
Step 1: Identify correct command syntax
The correct syntax to create an overlay network is docker network create --driver overlay [name].
Step 2: Evaluate options
docker network create --driver bridge mynetwork uses the bridge driver, not overlay. docker swarm network create mynetwork is invalid syntax. docker create network overlay mynetwork is incorrect command order. Only docker network create --driver overlay mynetwork is correct.
Final Answer:
docker network create --driver overlay mynetwork -> Option A
C. Allows standalone containers to connect to the overlay network
D. Automatically attaches volumes to containers
Solution
Step 1: Understand the --attachable flag
The --attachable flag allows standalone containers (not part of swarm services) to connect to the overlay network.
Step 2: Analyze options
Prevents services from using the network is false because it does not prevent services. Limits the network to only swarm services is opposite of what attachable does. Automatically attaches volumes to containers is unrelated to networks.
Final Answer:
Allows standalone containers to connect to the overlay network -> Option C
Quick Check:
--attachable = standalone container access [OK]
Hint: Attachable lets normal containers join overlay networks [OK]
Common Mistakes:
Thinking --attachable restricts network to swarm services
Confusing attachable with volume or storage options
Assuming it disables service connections
4. You created an overlay network with:
docker network create -d overlay mynet
But when you try to connect a standalone container using docker run --net mynet, it fails. What is the likely cause?
medium
A. The overlay network was not created with the --attachable flag
B. The container image is incompatible with overlay networks
C. Docker Swarm mode is not initialized
D. The container must be part of a service to use overlay networks
Solution
Step 1: Identify overlay network usage for standalone containers
Standalone containers can only connect to overlay networks if the network was created with --attachable.
Step 2: Evaluate other options
The container image is incompatible with overlay networks is incorrect; container images do not affect network connection. Docker Swarm mode is not initialized is unrelated if network was created. The container must be part of a service to use overlay networks is false because attachable networks allow standalone containers.
Final Answer:
The overlay network was not created with the --attachable flag -> Option A
Hint: Add --attachable to overlay network for standalone containers [OK]
Common Mistakes:
Ignoring the need for --attachable flag
Blaming container image compatibility
Assuming all overlay networks allow standalone containers
5. You want to deploy a multi-host Docker Swarm service that uses an overlay network named swarmnet. You also want to allow some standalone containers to join this network for debugging. Which sequence of commands correctly sets this up?
hard
A. docker network create -d overlay swarmnet docker service create --network swarmnet myservice
B. docker network create -d overlay --attachable swarmnet docker service create --network swarmnet myservice
C. docker network create -d bridge --attachable swarmnet docker service create --network swarmnet myservice
D. docker network create --driver overlay swarmnet docker run --net swarmnet myservice
Solution
Step 1: Create overlay network with attachable flag
To allow standalone containers to join, the overlay network must be created with --attachable.
Step 2: Deploy service on the created network
Use docker service create --network swarmnet myservice to deploy the service on the overlay network.
Step 3: Analyze options
docker network create -d overlay swarmnet docker service create --network swarmnet myservice misses --attachable, so standalone containers cannot join. docker network create -d bridge --attachable swarmnet docker service create --network swarmnet myservice uses bridge driver, which is not multi-host. docker network create --driver overlay swarmnet docker run --net swarmnet myservice tries to run a service as a container, which is incorrect.
Final Answer:
docker network create -d overlay --attachable swarmnet and docker service create --network swarmnet myservice -> Option B
Quick Check:
Attachable overlay network + service create = correct setup [OK]
Hint: Use --attachable overlay network for standalone container access [OK]
Common Mistakes:
Omitting --attachable flag for standalone containers
Using bridge driver for multi-host networking
Confusing service create with container run commands
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.