Create a Docker Swarm playground
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:
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.
#!/bin/bash
docker-compose downdocker-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 managerdocker-compose exec worker2 docker swarm join --token $SWARM_TOKEN managerdocker-compose exec worker3 docker swarm join --token $SWARM_TOKEN manager
docker-compose exec manager docker node lsdocker-compose exec manager sh
Lastly, you can incorporate a visualizer application to observe the cluster:
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
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 dockerfileRUN /bin/sh -c npm installCMD ["node" "server.js"]
# Commands to add (for the healthcheck + replace links)RUN /bin/sh -c apk add curlRUN /bin/sh -c sed -i 's/http:\/\//\/\//g'
Traefik
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! 😊
Practice code with the "Quick Sort" algorithm
Enhance your coding skills by learning how the Quick Sort algorithm works!
The SOLID/STUPID principles
Learn what are the SOLID and STUPID principles with examples
Create an Ansible playground with Docker
Let's create an Ansible playground with Docker
Setup a Kubernetes cluster with K3S, Traefik, CertManager and Kubernetes Dashboard
Let's setup step by step our own K3S cluster !
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
How to internationalize an AstroJS website while maintaining good SEO ?
We will see how to create an implementation of i18n with AstroJS
Database ACID/BASE - Understanding the CAP Theorem
Learn what is the CAP Theorem in less than 5 minutes !
LFTP - Deploy an application in command line
Here we will see how to automatically deploy an application with lftp in command line.