Top Docker Swarm Commands in 2025: A Complete Command Guide

Image
Top Docker Swarm Commands in 2025: A Complete Command Guide
Master Docker Swarm commands with this comprehensive guide. Learn swarm init, node management, and troubleshooting for production success.
Blog Author
Published on
Jul 3, 2025
Views
2990
Read Time
8 Mins
Table of Content

Working with container orchestration has taught me that mastering the right commands can make the difference between a smooth deployment and a production nightmare. If you're struggling to manage multi-container applications or wondering how to scale your Docker deployments efficiently, you're facing the same challenges I encountered when I first started with Docker Swarm commands. The good news? Once you know these basic commands, managing containers becomes much easier.

Knowing Docker Swarm’s Architecture

It is highlighted that understanding Docker Swarm architecture is beneficial for gaining keen knowledge of the management features, cluster scheduling, and fault resiliency. I have noticed that a lot of engineers tend to use commands without any consideration of the structure. This often leads to uncontrolled system deployments, unsecured systems, and unexpected vulnerabilities.

Manager vs Worker Nodes

In my experience managing production clusters, the distinction between manager and worker nodes forms the foundation of every swarm command you'll execute. Manager nodes handle the orchestration and cluster management tasks, maintaining the desired state of your swarm. They're responsible for:

Cluster state management and decision-making: Manager nodes keep everything related to the swarm as a whole, such as service definitions, network configurations, and secret management. They make scheduling decisions, aiming to keep your services healthy. When you execute administrative swarm command options, you're essentially instructing these managers to update the cluster state.

Scheduling and load distribution: Every time you deploy a service using Docker Swarm commands, manager nodes determine which worker nodes should run your containers based on resource availability, constraints, and placement preferences. This intelligent scheduling has saved me countless hours of manual container placement in large-scale deployments.

Serving the swarm mode HTTP API: All your Docker CLI commands communicate with manager nodes through this API. This centralised communication model ensures consistency across your cluster and provides a single point of control for automation tools and CI/CD pipelines when executing any swarm command.

Worker nodes, on the other hand, receive and execute tasks from manager nodes. They're the workhorses of your swarm cluster, running the actual containers that serve your applications.

The Raft Consensus Algorithm

Here's something that took me months to fully appreciate: Docker Swarm uses the Raft consensus algorithm to maintain cluster consistency. This isn't just technical trivia – understanding Raft helps you make better decisions about cluster sizing and fault tolerance.

The algorithm ensures that all manager nodes agree on the current state of the swarm. When you initialise Docker Swarm, you're essentially starting a Raft consensus group. This is why Docker recommends an odd number of manager nodes (3, 5, or 7) – it prevents split-brain scenarios during network partitions.

 
 
 
 
Get Beginner to Advance Level DevOps Training
Learn DevOps with Generative AI

Essential Docker Swarm Init Command and Cluster Setup

Let's dive into the command that starts it all. The Docker swarm init command creates a new swarm cluster and transforms your Docker host into a manager node. Understanding this fundamental command is crucial for anyone working with container orchestration.

Basic Swarm Initialisation

The simplest way to initialise Docker Swarm involves a single command that I've probably typed thousands of times:

docker swarm init

However, in production environments, you'll need more control. Here's the Docker Swarm init command I typically use when setting up a new cluster:

docker swarm init --advertise-addr 192.168.1.100 --listen-addr 192.168.1.100:2377

The --advertise-addr flag specifies which IP address other nodes should use to connect to this manager. I learned the hard way that omitting this on multi-homed systems can lead to connectivity issues. The --listen-addr determines where the node listens for swarm traffic, making it a critical part of the Docker Swarm init command syntax.

When you initialise Docker Swarm with this command, Docker generates two important tokens:

Manager token: Allows other nodes to join as managers, providing full cluster administration capabilities. Guard this token carefully – it's essentially the key to your kingdom. In my DevOps certification preparation, token security was emphasised repeatedly, and for good reason.

Worker token: Enables nodes to join as workers with limited permissions. This token can be shared more freely with team members who need to scale the worker pool but shouldn't have administrative access to critical Docker Swarm commands.

Advanced Init Options

For production deployments, I often use additional initialisation parameters that provide better control over cluster behaviour. The Docker Swarm init command supports numerous flags:

docker swarm init \

  --advertise-addr 192.168.1.100 \

  --data-path-port 4789 \

  --default-addr-pool 10.0.0.0/8 \

  --default-addr-pool-mask-length 24

The-data-path-port option specifies the port for overlay network traffic. This becomes crucial when dealing with firewalls or cloud security groups. The address pool options control how Docker assigns IP addresses to overlay networks, preventing conflicts with existing infrastructure. These advanced options demonstrate the flexibility of the Docker Swarm init command.

Node Management Commands Every DevOps Engineer Should Master

Node management is at the core of swarm cluster management. With Docker Swarm, you can monitor the cluster's fitness and scale it as needed. Learning these commands is important for efficient cluster management.  

Adding and Removing Nodes  

To increase the number of worker nodes in the swarm, you should get the join token from one of the manager nodes using the swarm command shown below.

docker swarm join-token worker

This command outputs the exact swarm command needed to join the swarm. On the new node, execute:

docker swarm join --token SWMTKN-1-xxx... 192.168.1.100:2377

For manager nodes, the process is similar but requires the manager token:

docker swarm join-token manager

I've developed a practice of rotating these tokens periodically for security using these Docker Swarm commands:

docker swarm join-token --rotate worker

docker swarm join-token --rotate manager

Removing nodes requires a two-step process that I've seen many engineers overlook. First, drain the node to prevent service disruption:

docker node update --availability drain node-name

This swarm command gracefully migrates all running containers to other nodes. Once drained, remove the node:

docker node rm node-name

Node Status and Maintenance

Monitoring node health is crucial for maintaining cluster stability. The following Docker Swarm commands have become part of my daily routine:

docker node ls

This displays all nodes with their status, availability, and manager status. For detailed information about a specific node:

docker node inspect node-name --pretty

When performing maintenance, I always use the drain command mentioned earlier. But here's a pro tip: you can also use labels to control service placement:

docker node update --label-add type=gpu node-gpu-01

These labels help with the constraints of services to guarantee that workloads are executed on suitable hardware, showcasing the precision with which Docker Swarm commands can be wielded.

Service Deployment and Management Commands

Docker services are pivotal for the orchestration features of Docker Swarm. Services come with additional features not found in stand-alone containers, like automatic scaling, rolling updates, and self-healing, which can be deployed through different Docker Swarm commands.

Creating and Updating Services

Creating a service requires defining the application state you wish to achieve. Let’s see how this works with an example from one of my projects:

docker service create \

  --name api-service \

  --replicas 3 \

  --publish 80:8080 \

  --env NODE_ENV=production \

  --mount type=volume,source=api-data,target=/data \

  --constraint 'node.labels.type==app' \

  nginx:latest

This powerful swarm command creates a service with three replicas, publishes port 80, sets environment variables, mounts a volume, and constrains deployment to specific nodes. Each flag serves a purpose:

--replicas: Determines how many container instances to run. Docker Swarm automatically distributes these across available nodes, ensuring high availability. I typically start with three replicas for production services and scale based on metrics using additional Docker Swarm commands.

--publish: Creates an ingress network that load-balances requests across all service replicas. This built-in load balancing has eliminated the need for external load balancers in many of my deployments when using Docker Swarm commands.

--constraint: Provides fine-grained control over placement. I use constraints to separate database workloads from application servers, ensure GPU workloads run on appropriate hardware, and maintain compliance by keeping sensitive services on specific nodes.

Updating services follows a similar pattern but uses the update command:

docker service update \

  --image nginx:1.21 \

  --update-parallelism 1 \

  --update-delay 10s \

  api-service

The update flags control the rollout process, preventing service disruption during deployments.

Service Scaling and Rollbacks

Scaling services in Docker Swarm is remarkably simple. When traffic spikes, I can quickly adjust replica counts:

docker service scale api-service=10

For multiple services:

docker service scale api-service=10 worker-service=5 cache-service=3

Docker Swarm's automatic rollback feature has saved me from numerous failed deployments. When updating a service, add rollback configuration:

docker service update \

  --image myapp:v2 \

  --update-failure-action rollback \

  --update-max-failure-ratio 0.25 \

  api-service

If more than 25% of tasks fail during the update, Swarm automatically reverts to the previous version.

Network and Stack Commands for Production Environments

Networking in Docker Swarm provides secure, encrypted communication between services across multiple hosts. Understanding these Docker Swarm commands is essential for building microservices architectures.

Overlay Network Management

Creating an overlay network enables multi-host container communication:

docker network create \

  --driver overlay \

  --subnet 10.0.0.0/24 \

  --gateway 10.0.0.1 \

  --attachable \

  my-overlay-network

The --attachable flag allows standalone containers to connect to the network, useful for debugging and development. In production, I often create multiple overlay networks to segment traffic:

docker network create --driver overlay frontend

docker network create --driver overlay backend

docker network create --driver overlay --encrypted secure-backend

The --encrypted flag enables IPSec encryption for sensitive traffic, though it does impact performance.

Docker Stack Deployment

Stack deployment represents the pinnacle of Docker Swarm orchestration. Instead of managing individual services, you define your entire application in a Compose file and deploy it as a cohesive unit:

docker stack deploy --compose-file docker-stack.yml myapp

Here's a sample stack file I use as a template:

version: '3.8'

services:

  web:

    image: nginx:latest

    deploy:

      replicas: 3

      update_config:

        parallelism: 1

        delay: 10s

      restart_policy:

        condition: on-failure

    networks:

      - frontend

  api:

    image: myapi:latest

    deploy:

      replicas: 5

    networks:

      - frontend

      - backend

networks:

  frontend:

    driver: overlay

  backend:

    driver: overlay

    encrypted: true

Managing stacks involves these key Docker Swarm commands:

docker stack ls                    # List all stacks

docker stack ps myapp             # Show all tasks in a stack

docker stack services myapp       # List services in a stack

docker stack rm myapp             # Remove a stack

Monitoring and Troubleshooting Swarm Commands

Effective monitoring separates successful swarm deployments from problematic ones. These Docker Swarm commands form my troubleshooting toolkit.

Health Checks and Logs

Service logs provide invaluable debugging information:

docker service logs api-service

docker service logs -f --tail 50 api-service  # Follow logs, last 50 lines

For distributed logging across multiple replicas:

docker service logs --timestamps --details api-service

Health status monitoring helps identify failing containers before they impact users:

docker service ps api-service --no-trunc

This shows the complete error message for failed tasks, crucial for debugging startup issues.

Debugging Common Issues

When services fail to start, I follow this debugging sequence:

# Check service status

docker service ps api-service --no-trunc

# Inspect service configuration

docker service inspect api-service --pretty

# Check node availability

docker node ls

# Verify network connectivity

docker network inspect frontend

For persistent issues, I examine the swarm cluster state:

docker system events --filter type=service

This real-time event stream reveals scheduling decisions, helping identify resource constraints or placement conflicts.

Best Practices and Command Combinations

Through years of managing production swarms, I've developed command combinations that streamline common tasks. These Docker Swarm commands have proven invaluable:

Rolling restart without downtime:

docker service update --force api-service

Backup swarm configuration:

docker service ls --format "table {{.Name}}" | \

  xargs -I {} docker service inspect {} > backup-{}.json

Monitor resource usage across nodes:

docker node ls -q | xargs docker node inspect \

  -f '{{ .Description.Hostname }} {{ .Status.State }} {{ .Spec.Availability }}'

Clean up unused resources:

docker system prune -af

docker volume prune -f

docker network prune -f

Recall that Docker Swarm commands are important tools that should be approached with caution. Always check changes in a staging environment, keep healthy backups, and evaluate cluster health regularly. If you need to initialise Docker Swarm for a new project or you are managing existing clusters, these commands support efficient container orchestration.

Conclusion

Learning Docker Swarm by acquiring DevOps certification commands makes the task of container orchestration much easier to handle. Every command, starting from initial Docker swarm init to more sophisticated stack deployment, has its purpose in building resilient, scalable applications. It is critical to know not only what each swarm command does but when and why to use it. Use basic initialisation like the Docker Swarm init command, slowly add on node control, and ultimately orchestrate complete application stacks. With these Docker swarm commands at your disposal, combined with a good grasp of swarm architecture, you can confidently tackle the challenges of production container orchestration.

FAQs

1. What is the command to initialise a Docker Swarm?

Use docker swarm init on the manager node. It sets up the cluster and provides a join token for worker nodes. This is the first step in creating a secure, orchestrated environment for your containers.

2. How do I check the status of the Swarm nodes?

Run docker node ls on the manager node to view all nodes and their roles in the cluster. It shows whether nodes are active, drained, or unreachable—helpful for troubleshooting.

3. How can I deploy a service in Docker Swarm?

Use docker service create followed by the service details to deploy a containerised application across the cluster. You can define replicas, publish ports, and set update strategies with flags.

4. How do I scale services in Docker Swarm?

Use docker service scale service_name=number_of_replicas to adjust the number of running instances. This allows you to handle increased traffic or reduce unused resources quickly.

5. What command shows service logs in real time?

Run docker service logs service_name to stream logs and monitor your service activity live. It’s useful for debugging issues or ensuring your service is running as expected.

Share Article
WhatsappFacebookXLinkedInTelegram
About Author
Narasimha Reddy Bommaka

CEO of StarAgile, CST

Certified Scrum Trainer (CST) with Scrum Alliance. Trained more than 10,000+ professionals on Scrum, Agile and helped hundreds of teams across many organisations like Microsoft, Capgemini, Thomson Reuters, KPMG, Sungard Availability Services, Knorr Bremse, Quinnox, PFS, Knorr Bremse, Honeywell, MicroFocus, SCB and SLK adopt/improve Agile mindset/implementation

Are you Confused? Let us assist you.
+1
Explore DevOps Certification Training!
Upon course completion, you'll earn a certification and expertise.
ImageImageImageImage

Popular Courses

Gain Knowledge from top MNC experts and earn globally recognised certificates.
50645 Enrolled
2 Days
From USD 699.00
USD
299.00
Next Schedule July 17, 2025
2362 Enrolled
2 Days
From USD 699.00
USD
299.00
Next Schedule July 16, 2025
25970 Enrolled
2 Days
From USD 1,199.00
USD
545.00
Next Schedule July 19, 2025
20980 Enrolled
2 Days
From USD 999.00
USD
499.00
Next Schedule July 21, 2025
12659 Enrolled
2 Days
From USD 1,199.00
USD
545.00
Next Schedule July 19, 2025
PreviousNext

Trending Articles

The most effective project-based immersive learning experience to educate that combines hands-on projects with deep, engaging learning.
WhatsApp