List all tasks on a node
In this step, you will learn how to list all tasks running on a Docker Swarm node. Docker Swarm is a container orchestration tool that allows you to manage a cluster of Docker nodes. A task is a running instance of a service.
Before listing tasks, we need to have a running Docker Swarm service. For this lab, we will use a simple nginx
service.
First, let's initialize a Docker Swarm on this node.
docker swarm init
You should see output indicating that the swarm has been initialized and providing a join token.
Now, let's create a simple service named my-nginx-service
using the nginx
image. We will pull the nginx
image first to ensure it's available.
docker pull nginx:latest
docker service create --name my-nginx-service --replicas 1 nginx
The docker service create
command creates a new service with one replica (one task). Docker will automatically pull the nginx
image if it's not already present, but we explicitly pulled it beforehand.
After the service is created, Docker will start a task for this service on one of the nodes in the swarm (in this case, the current node).
To list all tasks running on the node, you can use the docker node ps
command followed by the node ID or hostname. Since we are on the manager node where the service was created, we can use the node ID of this node.
To find the node ID, you can use the docker node ls
command.
docker node ls
This command will list all nodes in the swarm. The output will show the node ID, hostname, status, availability, and manager status. Copy the node ID of the current node (the one with Leader
under MANAGER STATUS
).
Now, use the docker node ps
command with the node ID you copied to list the tasks on that node. Replace <node_id>
with the actual node ID.
docker node ps <node_id>
This command will display a list of tasks running on the specified node, including the task ID, service name, image, node, desired state, current state, and error. You should see the my-nginx-service.1
task listed with a state of Running
.
Alternatively, you can list all tasks in the swarm using the docker task ls
command. This command is useful for seeing tasks across all nodes in the swarm.
docker task ls
This command will show similar information to docker node ps
, but it lists tasks from all nodes in the swarm. Since we only have one node and one task, the output will be similar to docker node ps
.
In this step, you learned how to list tasks on a specific node using docker node ps
and list all tasks in the swarm using docker task ls
.