How to set up Docker Swarm?
· Category: Docker
Short answer
Initialize Docker Swarm with docker swarm init on the manager node, then join worker nodes with the provided token. Deploy services using docker service create or stack files.
Steps
- Initialize the swarm on the manager:
docker swarm init --advertise-addr <IP>
- Copy the join token.
- Run the join command on worker nodes.
- Deploy a service or stack.
Example
docker swarm init --advertise-addr 192.168.1.10
docker swarm join --token SWMTKN-1-... 192.168.1.10:2377
Deploy a stack:
docker stack deploy -c docker-compose.yml myapp
Tips
- Run multiple managers for high availability.
- Use
docker node promoteto upgrade workers to managers. - Overlay networks are automatically created for stack deployments.
Common issues
- Firewall rules must allow ports 2377, 7946, and 4789.
- Manager nodes should not run heavy workloads.
- Swarm mode disables standalone container networking features.