Docker Swarm Commands

Docker Swarm Commands

Docker Swarm is controlled with the Docker CLI, which has different commands for doing tasks in the Swarm cluster. Here are some common Docker Swarm commands:

Initialize Swarm:

  • Initialize Swarm on the Manager Node:

      docker swarm init --advertise-addr <manager-node-ip>
    

    This initializes a new Docker Swarm on the manager node.

  • Join Worker Nodes to the Swarm:

      docker swarm join --token <worker-token> <manager-node-ip>:2377
    

    Use the join token generated during swarm initialization to join worker nodes to the swarm.

Services and Stacks:

  • Deploy a Service:

      docker stack deploy -c <compose-file> <stack-name>
    

    Deploy a service or stack using a Docker Compose file.

  • Scale a Service:

      docker service scale <service-name>=<replica-count>
    

    Scale the number of replicas for a service.

  • List Services:

      docker service ls
    

    List all services in the swarm.

  • Inspect Service:

      docker service inspect <service-name>
    

    Get detailed information about a specific service.

Nodes:

  • List Nodes:

      docker node ls
    

    List all nodes in the swarm.

  • Inspect Node:

      docker node inspect <node-id>
    

    Get detailed information about a specific node.

  • Update Node Availability:

      docker node update --availability drain <node-id>
    

    Mark a node as drained (do not schedule new tasks on the node).

Secrets:

  • Create a Secret:

      echo "my_secret" | docker secret create <secret-name> -
    

    Create a secret to be used by services.

  • List Secrets:

      docker secret ls
    

    List all secrets in the swarm.

Network:

  • List Networks:

      docker network ls
    

    List all networks in the swarm.

  • Inspect Network:

      docker network inspect <network-id>
    

    Get detailed information about a specific network.

Stack Removal:

  • Remove a Stack:

      docker stack rm <stack-name>
    

    Remove a deployed stack.

Leave Swarm:

  • Leave Swarm (Worker Node):

      docker swarm leave
    

    Instruct a worker node to leave the swarm.

  • Leave Swarm (Manager Node):

      docker swarm leave --force
    

    Forcefully remove a manager node from the swarm.

These commands help you manage a Docker Swarm in a simple way. Docker Swarm has more advanced features, and you can learn about more commands and options in the official Docker Swarm documentation.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!