Skip to main content

Configuring Swarm Networks

1. Creating Overlay Networks

Option Values Description
docker network create --driver overlay Creates an overlay network using the Docker overlay driver
--name network-name Name of the network to be created
--subnet 10.0.0.0/24 Subnet for the network (optional)
--gateway 10.0.0.1 Gateway IP for the network (optional)
--attachable true Allows external non-Swarm containers to connect (optional)

Example: Creating an Overlay Network

docker network create --driver overlay --subnet=10.0.9.0/24 --gateway=10.0.9.1 my-overlay-network
Note: Overlay networks are only available in swarm mode. The --driver overlay option is required to create a network that spans multiple Docker daemons.

2. Encrypting Overlay Networks

Property Value Description
encrypt true Enables encryption for the overlay network
key string Encryption key (optional, auto-generated if not provided)

Example: Create encrypted overlay network

docker network create \
  --driver overlay \
  --opt encrypted=true \
  --opt com.docker.network.driver.mtu=1200 \
  my-encrypted-network
Note: Encryption is only available for overlay networks in swarm mode.
Warning: Enabling encryption adds performance overhead. Consider the trade-off between security and performance.

3. Attaching Services to Networks

Service Network Configuration
web-app frontend-net Attached to frontend-net with --network
db-service backend-net Attached to backend-net with --network

Example: Attaching a Service to a Network

docker service create \
  --name web-app \
  --network frontend-net \
  nginx

Example: Attaching Multiple Services to Same Network

docker service create \
  --name api-server \
  --network backend-net \
  node:18

docker service create \
  --name cache-server \
  --network backend-net \
  redis:latest
Note: Services can be attached to multiple networks using --network multiple times.
Warning: If a service is not attached to any network, it will be created on the default ingress network.

4. Publishing Service Ports

Option Description Values Notes
--publish Maps a container port to a host port -p <hostPort>:<containerPort> Can be repeated for multiple ports
--publish-all Publishes all exposed ports to random host ports true Use with caution in production
--publish-mode Defines how ports are published host or ingress ingress allows load balancing across nodes

Example: Publishing a Service Port

docker service create --name web \
  --publish 8080:80 \
  nginx

Example: Publishing with Ingress Mode

docker service create --name api \
  --publish published=8081,target=8080,mode=ingress \
  node:alpine

5. Configuring Ingress Network

Property Description Default Value Notes
ingress.network.name Name of the ingress network ingress Used for routing external traffic to services
ingress.network.driver Driver used for the ingress network overlay Must be overlay for swarm mode
ingress.network.subnet Subnet for the ingress network 10.255.0.0/16 Must not overlap with other networks

Example: Create Ingress Network with Custom Subnet

docker network create \
  --driver overlay \
  --subnet 10.255.1.0/24 \
  --ingress \
  ingress-custom
Note: The --ingress flag makes the network available for routing external traffic to services.
Warning: Modifying the default ingress network can disrupt service accessibility. Always test in a non-production environment.

6. Setting Up Service Discovery

Option Values Description
--attachable true | false Allows external containers to connect to the overlay network
--driver overlay Network driver for multi-host communication
--subnet 10.0.0.0/24 Subnet range for the network
--gateway 10.0.0.1 Gateway IP for the network

Example: Create an Overlay Network

docker network create --driver overlay --subnet=10.0.9.0/24 --gateway=10.0.9.1 swarm-network

Example: Attach Service to Network

docker service create --name webapp --network swarm-network nginx
Note: Service discovery uses DNS-based resolution within the overlay network.
Warning: Ensure all nodes in the swarm are configured to use the same network driver and subnet.

7. Using Network Aliases in Services

Network aliases allow services to be discovered by alternative names within a Docker Swarm network. They provide a way to map a service name to a different DNS name, which can be useful for organizing services or maintaining backward compatibility.

Property Description Example
--network-alias Defines an alias for a service within a network --network-alias web-server

Example: Defining a Network Alias for a Service

docker service create \
  --name web-app \
  --network-alias api-server \
  --network frontend-network \
  nginx
Note: Network aliases are only effective within the scope of a specific network. A service with an alias in one network will not be discoverable under that alias in another network.

8. Configuring Load Balancing

Option Values Description
--load-balancer round-robin, least-connections Specifies the load balancing algorithm to use for distributing traffic among service tasks.
--publish port Exposes a port on the host to make the service accessible externally.

Example: Load Balancing Configuration

docker service create \
  --name my-service \
  --publish 80:80 \
  --load-balancer round-robin \
  nginx
Note: Load balancing is handled automatically by Docker Swarm when using the docker service create command with exposed ports.
Warning: Ensure that the service tasks are running on different nodes to effectively distribute load.

9. Managing Routing Mesh

Property Description Default Value Notes
routing.mesh Enables or disables the routing mesh for service discovery true Routing mesh allows services to communicate across nodes in a swarm
routing.mesh.subnet Subnet used for the routing mesh overlay network 10.255.0.0/16 Must not overlap with existing networks

Example: Configure Routing Mesh Subnet

docker network create \
  --driver overlay \
  --subnet=10.255.1.0/24 \
  --opt com.docker.network.overlay.rx_bandwidth=100mb \
  --opt com.docker.network.overlay.tx_bandwidth=100mb \
  routing-mesh-net
Note: The routing mesh is automatically managed by Docker Swarm, but can be customized for specific network configurations.

10. Configuring External Load Balancers

Configuration Description Example
--external-load-balancer Flag to enable external load balancing in swarm mode docker service create --external-load-balancer <lb-name> <image>
--publish Expose ports to the host network docker service create --publish 80:80 <image>
--network Attach service to a specific network docker service create --network my-network <image>

Example: External Load Balancer Setup

docker service create \
  --name my-app \
  --publish 80:80 \
  --external-load-balancer lb-123 \
  nginx
Note: External load balancers must be configured outside of Docker Swarm, typically via cloud providers or dedicated hardware.

11. Using Attachable Overlay Networks

Attachable overlay networks in Docker Swarm allow containers to connect to a network manually, rather than being automatically attached when starting a service. This is useful for debugging or connecting standalone containers to Swarm services.

Property Value Description
driver overlay The network driver to use for the overlay network
attachable true Enables manual attachment of containers to the network
name my-attachable-network Name of the network to be created

Example: Creating an Attachable Overlay Network

docker network create --driver overlay --attachable my-attachable-network

Example: Attaching a Container to the Network

docker run --network my-attachable-network alpine ping -c 3 google.com
Note: The attachable property must be set to true when creating the network for it to be manually attached.
Warning: Attachable networks are not suitable for production services due to potential security and performance implications.

12. Troubleshooting Swarm Networking

Common Issues and Solutions

  1. Containers cannot communicate across services: Check if the services are using the same network and that the network is attached to the correct scope (swarm or local).
  2. Port mapping issues: Ensure that the published ports are correctly mapped and that there is no port conflict on the host.
  3. Service discovery fails: Verify that DNS resolution is enabled in the network configuration and that services are using the correct service names.
  4. Network performance degradation: Check for network bottlenecks or misconfigurations in the overlay network.
Issue Possible Cause Solution
Service not reachable Incorrect network configuration or service name typo Verify the network and service names in docker service inspect
Port conflicts Host port already in use or misconfigured Use docker service update --publish-add to change port mapping
Overlay network not functioning Network misconfiguration or Docker daemon issues Recreate the overlay network with docker network rm and docker network create

Example: Inspecting Service Network Configuration

docker service inspect my-service
Note: Use docker network ls to list all networks and verify that the swarm network is active.
Warning: When troubleshooting, avoid modifying network configurations while services are running to prevent service disruption.

Was this article helpful?