Super useful Docker Swarm commands

in #dockerlast year (edited)

Greetings!

Today I've just decided to publish a few docker commands that will help you manage the swarm. Let's go right into the topic!

DockerCmds

The cheatsheet image above is more general, while the post below describes the commands that I use really often while dealing with docker swarm.

Note: sudo is required if the docker is not a member of a wheel/root group. You might remove it if it's not needed.

View docker info & version

  • sudo docker info

Show docker swarm worker token (again)

This will only run on a manager node of course

  • sudo docker swarm join-token worker

View alive services (and replicas of them) in the swarm

This allows you to check the status of your swarm

  • sudo docker service ls

View log of the whole service

Very useful for debugging; replace {} with your actual variable (ex., from the previous cmd). Warning: if you have a lot of instances, this log will be really huge, so you might check the other way (below).

  • sudo docker service logs {SERVICE_ID_OR_NAME}

View nodes list

Check individual node names and statuses

  • sudo docker node ls

View individual node log

To check the specific node's log, replace {CONTAINER_ID} with the container of your interest, for example, from previous command.

  • sudo docker logs {CONTAINER_ID}

Inspect object by ID

  • sudo docker inspect {ID}

Remove unneeded network

If you have a network that you manually created and want to remove, or maybe it was a product of docker-compose or some other tools

  • sudo docker network rm {NETWORK_NAME}

Remove unremovable network

If you have an error like: "Error response from daemon: rpc error: code = FailedPrecondition desc = network {NET_ID} is in use by task {TASK_ID}", fix it with the following command:

  1. pull the recovery container: sudo docker pull dperny/tasknuke
  2. kill the task: sudo docker run -v /var/run/docker/swarm/control.sock:/var/run/swarmd.sock dperny/tasknuke {ID_OF_TASK}
    If you have multiple tasks related to this network, here you can remove them all:
  • while true; do sudo docker run -v /var/run/docker/swarm/control.sock:/var/run/swarmd.sock dperny/tasknuke $(sudo docker network rm {YOUR_NETWORK_NAME_TO_REMOVE} 2>&1 | awk -F"task" '{ print $2 }'); done
    Press Ctrl+C after seeing help message and then return to previous step to finally remove the annoying network.

Don't forget to replace {THE_VARIABLES} if you copy-paste these commands to your needs.

These commands helped me a lot while working on my swarm. I will update this list if some other ideas arise or leave your comments if you like for some commands to be added here.

Thanks and good luck!
@sxiii from OoOSloO city