Docker Swarm commands
Docker Swarm provides a range of commands to manage and operate your Swarm cluster. Here are some of the most important Docker Swarm commands and their usage:
Initialization and Configuration Commands
1. Initialize a Swarm
docker swarm init
This command initializes the current Docker engine as a manager node in a Swarm.
2. Join a Node to the Swarm
docker swarm join --token <token> <manager-ip>:<manager-port>
This command joins a new node to the Swarm as either a manager or a worker, depending on the token used.
3. Leave the Swarm
docker swarm leave
This command makes the current node leave the Swarm.
docker swarm leave --force
Forces the node to leave the Swarm, useful for manager nodes.
Node Management Commands
4. List Nodes in the Swarm
docker node ls
Displays a list of all nodes in the Swarm, showing their status, availability, and roles.
5. Inspect a Node
docker node inspect <node-id>
Provides detailed information about a specific node in the Swarm.
6. Promote a Node to Manager
docker node promote <node-id>
Promotes a worker node to a manager node.
7. Demote a Manager Node to Worker
docker node demote <node-id>
Demotes a manager node to a worker node.
8. Update Node Availability
docker node update --availability <active|pause|drain> <node-id>
Updates the availability of a node.
Service Management Commands
9. Create a Service
docker service create --name <service-name> --replicas <number> <image>
Creates a new service with the specified number of replicas.
10. List Services
docker service ls
Lists all services running in the Swarm.
11. Inspect a Service
docker service inspect <service-id>
Provides detailed information about a specific service.
12. Update a Service
docker service update --replicas <number> <service-id>
Updates the configuration of a service, such as scaling the number of replicas.
13. Remove a Service
docker service rm <service-id>
Removes a service from the Swarm.
Task Management Commands
14. List Tasks
docker service ps <service-id>
Lists the tasks (containers) for a specific service.
15. Inspect a Task
docker inspect <task-id>
Provides detailed information about a specific task.
Stack Management Commands
16. Deploy a Stack
docker stack deploy -c <compose-file> <stack-name>
Deploys a new stack from a Docker Compose file.
17. List Stacks
docker stack ls
Lists all stacks deployed in the Swarm.
18. Remove a Stack
docker stack rm <stack-name>
Removes a stack from the Swarm.
19. Inspect a Stack
docker stack ps <stack-name>
Provides information about the tasks in a stack.
Overlay Network Commands
20. Create an Overlay Network
docker network create --driver overlay <network-name>
Creates a new overlay network for use in the Swarm.
21. List Networks
docker network ls
Lists all networks, including overlay networks.
22. Remove a Network
docker network rm <network-name>
Removes an overlay network from the Swarm.
General Commands
23. Display Swarm Information
docker info
Displays detailed information about the Docker installation, including Swarm status and node details.
By using these commands, you can effectively manage and operate a Docker Swarm cluster, ensuring that services are deployed, scaled, and maintained across your nodes.