Create a Docker Swarm playground

Let's create Docker Swarm playground on your local machine
Saturday, June 22, 2024

Docker Swarm, a lightweight container orchestrator, provides an excellent way to manage containerized applications. In this blog post, we’ll explore how to set up and run Docker Swarm on your local machine. This guide will help you get started.

Why Run Docker Swarm Locally?

  • Easy Testing: With a local setup, you can experiment with Docker Swarm features without worrying about breaking anything in a production environment.
  • Fear-Free Environment: No need to fear accidental misconfigurations or service disruptions. Your local setup is isolated and safe.
  • Testing and Demos: Use Docker Swarm for testing your applications or showcasing demos during presentations.
  • Simplicity: Compared to Kubernetes, Docker Swarm offers a simpler and more straightforward orchestration experience.

Architecture

Loading graph...

Setup

Below is the Docker Compose file that sets up one manager and three worker nodes for your Docker Swarm:

docker-compose.yaml
version: '3.9'
services:
manager:
image: docker:dind
privileged: true
environment:
- DOCKER_TLS_CERTDIR=/certs
volumes:
- ./swarm:/swarm
working_dir: /swarm
ports:
- 8000-8080:8000-8080
healthcheck:
test: ["CMD", "docker", "ps"]
interval: 1s
timeout: 1s
retries: 30
start_period: 2s
worker1:
image: docker:dind
privileged: true
environment:
- DOCKER_TLS_CERTDIR=/certs
healthcheck:
test: ["CMD", "docker", "ps"]
interval: 1s
timeout: 1s
retries: 30
start_period: 2s
worker2:
image: docker:dind
privileged: true
environment:
- DOCKER_TLS_CERTDIR=/certs
healthcheck:
test: ["CMD", "docker", "ps"]
interval: 1s
timeout: 1s
retries: 30
start_period: 2s
worker3:
image: docker:dind
privileged: true
environment:
- DOCKER_TLS_CERTDIR=/certs
healthcheck:
test: ["CMD", "docker", "ps"]
interval: 1s
timeout: 1s
retries: 30
start_period: 2s

Next, you’ll find an initialization script that sets up the infrastructure, creates a Docker Swarm cluster, and adds the workers to this Swarm.

init.bash
#!/bin/bash
docker-compose down
docker-compose up -d
sleep 30
docker-compose exec manager docker swarm init
SWARM_TOKEN=$(docker-compose exec manager docker swarm join-token -q worker | tr -d '\r\n')
echo "-> GENERATED TOKEN IS |$SWARM_TOKEN|"
sleep 5
docker-compose exec worker1 docker swarm join --token $SWARM_TOKEN manager
docker-compose exec worker2 docker swarm join --token $SWARM_TOKEN manager
docker-compose exec worker3 docker swarm join --token $SWARM_TOKEN manager
docker-compose exec manager docker node ls
docker-compose exec manager sh

Lastly, you can incorporate a visualizer application to observe the cluster:

swarm/visualizer.yaml
version: "3.8"
services:
viz:
ports:
- 8000:8080
image: dockersamples/visualizer
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints:
- node.role == manager

To run the visualizer using Docker Compose, execute docker stack deploy --compose-file visualizer.yml visualize inside the manager, wait for completion, and then visit localhost:8000 to check your cluster.

Examples

Here are some docker-compose examples you can use to check if your containers are well deployed in the Swarm cluster.

Nginx application

swarm/nginx.yaml
version: '3.8'
services:
nginx:
image: nginx:1.23.0-alpine
ports:
- 8010:80
healthcheck:
test: ["CMD", "curl", "-i", "http://localhost"]
timeout: 1s
interval: 1s
retries: 30
start_period: 2s
deploy:
placement:
constraints:
- node.role == worker
resources:
limits:
cpus: '0.5'
memory: 128m
# reservations:
# cpus: '0.75'
# memory: 256m
replicas: 5
update_config:
order: start-first
failure_action: rollback
delay: 5s

NodeJS and Mongo application

version: '3.9'
volumes:
nana_data:
# secrets:
# USER_NAME:
# file: secrets/USER_NAME.secret
# USER_PWD:
# file: secrets/USER_PWD.secret
services:
front:
image: nanajanashia/k8s-demo-app:v1.0
ports:
- 8020:3000
# secrets:
# - USER_NAME
# - USER_PWD
environment:
USER_NAME: root
USER_PWD: example
DB_URL: db
healthcheck:
test: ["CMD", "curl", "-i", "http://localhost:3000"]
interval: 1s
timeout: 1s
retries: 30
start_period: 2s
deploy:
replicas: 3
placement:
constraints:
- node.role == worker
resources:
limits:
cpus: '1'
memory: 300m
# reservations:
# cpus: '0.75'
# memory: 256m
# update_config:
# order: start-first
# failure_action: rollback
# delay: 5s
db:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- nana_data:/data/db
deploy:
placement:
constraints:
- node.role == worker
express:
image: mongo-express
ports:
- 8021:8081
environment:
ME_CONFIG_MONGODB_SERVER: db
ME_CONFIG_MONGODB_AUTH_USERNAME: root
ME_CONFIG_MONGODB_AUTH_PASSWORD: example
deploy:
placement:
constraints:
- node.role == worker

Note: You may need to add these lines in to Dockerfile to make it work (original: https://hub.docker.com/layers/nanajanashia/k8s-demo-app/v1.0/images/sha256-6f554135da39ac00a1c2f43e44c2b0b54ca13d3d8044da969361e7781adb7f95?context=explore):

# End of the original dockerfile
RUN /bin/sh -c npm install
CMD ["node" "server.js"]
# Commands to add (for the healthcheck + replace links)
RUN /bin/sh -c apk add curl
RUN /bin/sh -c sed -i 's/http:\/\//\/\//g'

Traefik

traefik.yaml
version: "3.9"
networks:
traefik:
services:
traefik:
image: traefik:2.10.3
ports:
# - 80:80
- 8010:8080
command:
- --api.insecure=true
- --api.dashboard=true
- --api.debug=true
- --log.level=DEBUG
- --providers.docker.swarmMode=true
- --providers.docker.network=traefik_traefik
- --entrypoints.http.address=:80
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
- traefik
deploy:
placement:
constraints:
- node.role == manager
labels:
- "traefik.enable=true"
- "traefik.http.routers.monitor.rule=Host(`monitor-domain`)"
- "traefik.http.routers.monitor.entrypoints=http"
- "traefik.http.services.monitor.loadbalancer.server.port=80"
- "traefik.http.routers.monitor.service=api@internal"

Conclusion

By following this guide, you should now be able to set up and run Docker Swarm on your local machine. Happy containerizing! 😊


Recommended articles