Cloud Native 11 min read

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.

BaiPing Technology
BaiPing Technology
BaiPing Technology
Master Docker Swarm: From Init to Portainer UI Management

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 init

Node

A node represents a scheduling unit in a Swarm cluster. List nodes with:

docker node ls

Manager

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 demo

Portainer 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_demo

After 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/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud Nativecontainer orchestrationDocker ComposeDocker SwarmportainerDocker Stack
BaiPing Technology
Written by

BaiPing Technology

Official account of the BaiPing app technology team. Dedicated to enhancing human productivity through technology. | DRINK FOR FUN!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.