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.