Master Docker Swarm: From Init to Portainer UI Management
This guide walks you through Docker Swarm fundamentals, core concepts like init, nodes, managers, workers, services, configs, and secrets, shows how to define multi‑service applications with Docker Compose, deploy them via Docker stack, and manage the cluster visually using Portainer.
Docker Swarm Overview
Docker Swarm is Docker's native clustering platform written in Go, which greatly simplifies management of Docker hosts, networks, and storage across physical machines, virtual machines, or cloud instances.
Core Concepts
Init
Swarm mode is built into Docker; enable it with a single command:
docker swarm initNode
A node represents a scheduling unit in a Swarm cluster. List nodes with:
docker node lsManager
Managers are responsible for resource allocation and task scheduling; a cluster must have at least one manager.
Worker
Workers are the execution nodes that run tasks assigned by managers.
Service
A service is the smallest deployable unit in Swarm, supporting resource limits, scaling, rolling updates, and simple health checks.
Config
Configs store configuration data for services. Create a config with:
echo "application.name=demo" | docker application.properties -Secret
Secrets manage sensitive information similarly to configs.
echo "123456" | docker mysql.password -Docker Compose Example
The following compose file defines multiple services (Redis, Nacos, MySQL, order‑center, user‑center, API gateway, Nginx) with global deployment mode, node constraints, environment variables, logging options, and health checks.
version: "3.2"
services:
redis:
image: redis
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
command: --requirepass 123456
deploy:
mode: global
placement:
constraints: [node.labels.node == manager]
nacos:
image: nacos/nacos-server
depends_on:
- mysql
environment:
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=mysql
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_DB_NAME=nacos_config
- MYSQL_SERVICE_USER=root
- MYSQL_SERVICE_PASSWORD=123456
deploy:
mode: global
placement:
constraints: [node.labels.node == manager]
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
mysql:
image: registry.cn-hangzhou.aliyuncs.com/yaochengzhu/mysql:2021-04-10
environment:
- MYSQL_ROOT_PASSWORD=123456
volumes:
- mysql:/var/lib/mysql
command:
--default-time_zone='+8:00'
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
deploy:
mode: global
placement:
constraints: [node.labels.node == manager]
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
order-center:
image: registry.cn-hangzhou.aliyuncs.com/yaochengzhu/order-center:2020-12-04-18-09-19
restart: always
depends_on:
- mysql
- nacos
- redis
deploy:
replicas: 2
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
environment:
- SERVER_PORT=80
- MYSQL_SERVER=jdbc:mysql://mysql:3306/order_center?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
- MYSQL_USER_NAME=root
- MYSQL_ROOT_PASSWORD=123456
- REDIS_HOST=redis
- REDIS_PORT=6379
- REDIS_PASSWORD=123456
- NACOS_SERVER=nacos:8848
- LOG_LEVEL=INFO
healthcheck:
test: ["CMD","curl","-f","http://127.0.0.1/doc.html"]
interval: 5s
timeout: 5s
retries: 100
user-center:
image: registry.cn-hangzhou.aliyuncs.com/yaochengzhu/user-center:2020-12-04-18-09-19
restart: always
environment:
- SERVER_PORT=80
- MYSQL_SERVER=jdbc:mysql://mysql:3306/user_center?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
- MYSQL_USER_NAME=root
- MYSQL_ROOT_PASSWORD=123456
- REDIS_HOST=redis
- REDIS_PORT=6379
- REDIS_PASSWORD=123456
- NACOS_SERVER=nacos:8848
- LOG_LEVEL=INFO
depends_on:
- mysql
- nacos
- redis
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
healthcheck:
test: ["CMD","curl","-f","http://127.0.0.1/doc.html"]
interval: 5s
timeout: 5s
retries: 100
api-gateway:
image: registry.cn-hangzhou.aliyuncs.com/yaochengzhu/api-gateway:2020-12-04-18-09-19
restart: always
environment:
- SERVER_PORT=80
- USER_CENTER_SERVER=lb://user-center
- ORDER_CENTER_SERVER=lb://order-center
- NACOS_SERVER=nacos:8848
- SENTINEL_SERVER=sentinel:8858
- LOG_LEVEL=INFO
depends_on:
- user-center
- order-center
- nacos
- sentinel
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
healthcheck:
test: ["CMD","curl","-f","http://127.0.0.1/doc.html"]
interval: 5s
timeout: 5s
retries: 100
nginx:
image: registry.cn-hangzhou.aliyuncs.com/yaochengzhu/nginx:api-gateway
restart: always
ports:
- "80:80"
- "443:443"
depends_on:
- api-gateway
logging:
driver: "json-file"
options:
max-size: "100m"
max-file: "3"
healthcheck:
test: ["CMD","curl","-f","http://127.0.0.1/doc.html"]
interval: 5s
timeout: 5s
retries: 100
volumes:
mysql:Save the file as docker-compose.yaml and start the stack with docker-compose up. For multi‑environment deployments, use Docker stack:
docker stack deploy --compose-file=demo-compose.yaml demoPortainer Visual Management
Portainer provides a UI for easier Swarm management.
Install Portainer
Deploy Portainer with the following stack definition:
version: '3.2'
services:
agent:
image: portainer/agent:2.11.1
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /var/lib/docker/volumes:/var/lib/docker/volumes
networks:
- agent_network
deploy:
mode: global
placement:
constraints: [node.platform.os == linux]
portainer:
image: portainer/portainer-ce:2.11.1
command: -H tcp://tasks.agent:9001 --tlsskipverify
ports:
- "9443:9443"
- "9000:9000"
- "8000:8000"
volumes:
- portainer_data:/data
networks:
- agent_network
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.role == manager]
networks:
agent_network:
driver: overlay
attachable: true
volumes:
portainer_data:Run the deployment:
docker stack deploy --compose-file=portainer.yaml portainer_demoAfter installation, open http://<your-ip>:9000 in a browser, set the admin password, and explore the UI.
Conclusion
Docker Swarm is suitable for small‑scale, less complex applications, though Alibaba Cloud discontinued its service in 2019, favoring Kubernetes. When choosing technology, consider both capabilities and suitability; you might try Swarm with Portainer or explore alternatives like Rancher.
References
docker: https://www.docker.com/
swarm: https://docs.docker.com/engine/reference/commandline/swarm/
portainer: https://www.portainer.io/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
BaiPing Technology
Official account of the BaiPing app technology team. Dedicated to enhancing human productivity through technology. | DRINK FOR FUN!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
