How to use docker service logs command to view service and task logs

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively use the docker service logs command to view and manage logs generated by Docker services and their individual tasks. We will begin by creating a simple Docker service using the alpine image and the ping command.

Following the service creation, you will explore how to view logs for the entire service, providing a consolidated view of output from all running tasks. You will then learn how to narrow down your focus to view logs for a specific task within the service. Finally, the lab will demonstrate how to leverage various options with the docker service logs command to filter and format the log output according to your needs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/ContainerOperationsGroup -.-> docker/logs("View Container Logs") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") subgraph Lab Skills docker/run -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} docker/ls -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} docker/ps -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} docker/rm -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} docker/logs -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} docker/pull -.-> lab-555226{{"How to use docker service logs command to view service and task logs"}} end

Create a simple Docker service

In this step, we will create a simple Docker service using the docker service create command. A Docker service is a group of containers of the same image. When you create a service, you define which container image to use and which commands to execute inside the containers.

First, let's pull the alpine image, which is a lightweight Linux distribution commonly used in Docker containers.

docker pull alpine

You should see output indicating that the image is being pulled and downloaded.

Using default tag: latest
latest: Pulling from library/alpine
...
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

Now, we will create a Docker service named my-service using the alpine image. We will also specify the command ping labex.io to run inside each container of the service. The --name flag assigns a name to the service, and the alpine ping labex.io part specifies the image and the command to run.

docker service create --name my-service alpine ping labex.io

After executing the command, you will see output similar to this, indicating that the service has been created. The long string of characters is the service ID.

p1234567890abcdefghijklmnopqrstu
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged

To verify that the service is running, you can use the docker service ls command. This command lists all the services running on your Docker swarm.

docker service ls

You should see output similar to this, showing your my-service listed with 1 replica running.

ID             NAME         MODE         REPLICAS   IMAGE          PORTS
p1234567890   my-service   replicated   1/1        alpine:latest

This confirms that your Docker service my-service has been successfully created and is running with one task (container) executing the ping labex.io command.

View logs for the entire service

In the previous step, we created a Docker service named my-service that runs a container executing the ping labex.io command. This command generates output (logs) as it runs. In this step, we will learn how to view the logs for the entire service.

To view the logs for a Docker service, you use the docker service logs command followed by the service name.

docker service logs my-service

Executing this command will display the logs generated by all the tasks (containers) belonging to the my-service service. Since our service currently has only one task, you will see the output from the ping labex.io command running in that container.

my-service.1.abcdefghijkl  PING labex.io (1.2.3.4): 56 data bytes
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=0 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=1 ttl=50 time=123.456 ms
...

The output shows the log entries, prefixed with the service name, task number, and a unique task ID. This helps you identify which task generated which log entry, especially when you have multiple tasks running for a service.

You can also use the -f or --follow flag to stream the logs in real-time. This is useful for monitoring the service's activity as it happens.

docker service logs -f my-service

This command will continuously display new log entries as they are generated by the service's tasks. To stop following the logs, press Ctrl+C.

Viewing the logs for the entire service is helpful for getting an overall picture of the service's health and activity.

View logs for a specific task within the service

In the previous step, we viewed the logs for the entire my-service. When a service has multiple tasks (replicas), viewing the combined logs can be overwhelming. In this step, we will learn how to view logs for a specific task within a service.

First, we need to identify the task ID for which we want to view the logs. We can get this information using the docker service ps command, which lists the tasks associated with a service.

docker service ps my-service

This command will show you the tasks running for my-service. The output will look similar to this:

ID             NAME           IMAGE          NODE         DESIRED STATE   CURRENT STATE           ERROR   PORTS
abcdefghijkl   my-service.1   alpine:latest   labex-vm   Running         Running 2 minutes ago

In this output, abcdefghijkl is the task ID, and my-service.1 is the task name. The task name is composed of the service name and the task number.

To view the logs for a specific task, you use the docker service logs command followed by the task ID.

docker service logs abcdefghijkl

Note: Replace abcdefghijkl with the actual task ID you obtained from the docker service ps command output.

Executing this command will display only the logs generated by the container running for that specific task. This is useful for debugging issues with individual tasks.

my-service.1.abcdefghijkl  PING labex.io (1.2.3.4): 56 data bytes
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=0 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=1 ttl=50 time=123.456 ms
...

You can also use the -f flag here to follow the logs of the specific task in real-time.

docker service logs -f abcdefghijkl

Viewing logs for individual tasks allows you to isolate issues and understand the behavior of specific instances of your service.

Use options to filter and format service logs

In this step, we will explore some useful options for filtering and formatting the output of the docker service logs command. These options can help you find specific information in the logs and make the output more readable.

One common requirement is to view only the most recent log entries. You can use the --tail option to specify the number of lines to display from the end of the logs. For example, to view the last 5 lines of logs for my-service:

docker service logs --tail 5 my-service

This will output the last 5 log entries from the service.

my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=10 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=11 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=12 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=13 ttl=50 time=123.456 ms
my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=14 ttl=50 time=123.456 ms

Another useful option is --since, which allows you to view logs generated after a specific timestamp or duration. For example, to view logs generated in the last 5 minutes:

docker service logs --since 5m my-service

You can also specify a specific timestamp in RFC3339Nano format, date (YYYY-MM-DD), or date and time (YYYY-MM-DDTHH:MM:SS).

To include timestamps in the output, use the --timestamps or -t flag:

docker service logs -t my-service

This will add a timestamp to the beginning of each log entry.

2023-10-27T10:30:00.123456789Z my-service.1.abcdefghijkl  PING labex.io (1.2.3.4): 56 data bytes
2023-10-27T10:30:01.123456789Z my-service.1.abcdefghijkl  64 bytes from 1.2.3.4: seq=0 ttl=50 time=123.456 ms
...

Finally, you can combine these options to refine your log viewing. For instance, to view the last 10 log entries with timestamps:

docker service logs --tail 10 -t my-service

These options provide flexibility in how you view and analyze your Docker service logs, making it easier to monitor and troubleshoot your applications.

Before concluding the lab, let's clean up the service we created. We can remove the service using the docker service rm command.

docker service rm my-service

You will see output confirming the removal of the service.

my-service

This command stops and removes all tasks associated with the service.

Summary

In this lab, we learned how to create a simple Docker service using the docker service create command, specifying the image and the command to run within the service's tasks. We verified the service creation and status using docker service ls.

We then explored how to view logs for the entire service using docker service logs <service_name> and how to view logs for a specific task within the service by first identifying the task ID using docker service ps <service_name> and then using docker service logs <task_id>. Finally, we learned how to use various options with docker service logs to filter and format the log output, such as --follow to stream logs, --since to view logs from a specific time, and --tail to view the last N lines of logs.