# 📦 Docker Swarm Documentation Comprehensive guide to managing a **Docker Swarm** cluster. This document includes core commands, workflows, and best practices for maintaining a healthy and operational environment. --- ## 📚 Table of Contents 1. [🔧 Cluster Health & Manager Count](#-cluster-health--manager-count) 2. [🚀 Example Workflow: Setting Up Nginx](#-example-workflow-setting-up-nginx) 3. [⚙️ Cluster Initialization and Management](#️-cluster-initialization-and-management) 4. [🖥️ Node Management](#️-node-management) 5. [🛠️ Service Management](#️-service-management) 6. [🔑 Join Tokens & Node Configuration](#-join-tokens--node-configuration) --- ## 🔧 Cluster Health & Manager Count A Docker Swarm cluster requires a **majority of manager nodes** to be functional for quorum. > **Best Practice:** Always maintain **more than 50%** manager nodes online. Losing quorum will render the cluster non-operational. --- ## 🚀 Example Workflow: Setting Up Nginx Docker Swarm handles service deployment through several internal components: 1. **API** – Receives service requests. 2. **Allocator** – Determines resource allocation. 3. **Dispatcher** – Assigns tasks to nodes. 4. **Scheduler** – Places tasks on optimal nodes. This process ensures resilient and efficient service distribution. --- ## ⚙️ Cluster Initialization and Management ### 🔹 Initialize Cluster ```bash docker swarm init ``` ### 🔹 Initialize with Specific Interface ```bash docker swarm init --advertise-addr ``` ### 🔹 Join Existing Cluster ```bash docker swarm join ``` ### 🔹 Leave Cluster ```bash docker swarm leave ``` ### 🔹 Unlock a Manager Node ```bash docker swarm unlock ``` --- ## 🖥️ Node Management ### 🔸 List Nodes ```bash docker node ls ``` **Example Output:** ``` ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 8yw8jrjeqczaci0qkuy060g09 * docker-1 Ready Active Leader 24.0.5 v4gvf7xenw0izmxgvhr6hb2rj docker-2 Ready Active 24.0.5 kd3ujmt1ey3pw6v9189fouxfa docker-3 Ready Active Reachable 24.0.5 tm1msy58ztcltt36rs1lb76p7 docker-4 Down Active 24.0.5 ``` ### 🔸 Remove a Node ```bash docker node rm ``` ### 🔸 Promote to Manager ```bash docker node promote ``` ### 🔸 Inspect a Node ```bash docker node inspect ``` ### 🔸 Change Node Role ```bash docker node update --role manager docker node update --role worker ``` ### 🔸 Change Node Availability ```bash docker node update --availability active docker node update --availability pause docker node update --availability drain ``` ### 🔸 Add or Remove Labels **Add:** ```bash docker node update --label-add env=development docker node update --label-add env=testing ``` **Remove:** ```bash docker node update --label-rm env= docker node update --label-rm env ``` **Use label constraints in service deployment:** ```yaml deploy: placement: constraints: - node.labels.env == development ``` --- ## 🛠️ Service Management ### 🔹 Show Tasks on a Node ```bash docker node ps ``` ### 🔹 List All Services ```bash docker service ls ``` ### 🔹 Create a New Service ```bash docker service create --name ``` ### 🔹 Scale a Service ```bash docker service scale = ``` **Example:** ```bash docker service scale nginx=5 ``` ### 🔹 Inspect a Service ```bash docker service inspect ``` ### 🔹 Create Service with Replicas, Env Vars, and Port Mapping ```bash docker service create \ --name \ --replicas \ --env \ --publish : \ ``` **Example:** ```bash docker service create \ --name nginx \ --replicas 3 \ --env MY_ENV_VAR=value \ --publish 8080:80 \ nginx ``` --- ## 🔑 Join Tokens & Node Configuration Securely add nodes to your Swarm using join tokens. ### 🔹 Get Worker Token ```bash docker swarm join-token worker ``` ### 🔹 Get Manager Token ```bash docker swarm join-token manager ```