Output Logs in Docker Compose
The short answer
In Docker Compose, to output the logs of all the containers related to all the services defined in your [.inline-code] compose.yaml[.inline-code] file at once, you can use the [.inline-code] docker compose logs[.inline-code] command without arguments:
For example, considering the following [.inline-code] compose.yaml[.inline-code] file:
This command will output all the logs of both the [.inline-code] website[.inline-code] and [.inline-code] api[.inline-code] services:
[#view-the-logs-of-specific-services] Viewing the logs of a specific service [#view-the-logs-of-specific-services]
To output the logs of one or more specific services, you can use the [.inline-code]docker compose logs[.inline-code] command as follows:
Where [.inline-code] service_name …[.inline-code] is a list of service names separated by a space character.
For example, this command will only output the logs of the containers related to the [.inline-code] api[.inline-code] service:
You can learn more about container logs by reading our article on how to monitor the logs of single containers in Docker.
[#customize-the-logs-output] Customizing the logs output [#customize-the-logs-output]
To output the logs of containers without including the name of their related services, which helps making the output cleaner and easier to parse, you can use the [.inline-code] --no-log-prefix[.inline-code] flag:
For example, this command will only output the logs generated by the containers without the names of the related services:
[#include-the-logs-timestamps] Including the logs timestamp in the output [#include-the-logs-timestamps]
To output the logs of containers including the timestamp at which they were generated, you can use the [.inline-code] -t[.inline-code] flag (short for [.inline-code] --timestamps[.inline-code] ):
For example, this command will include the timestamp of every single log line:
[#view-the-logs-real-time] Displaying service logs in real-time [#view-the-logs-real-time]
To monitor the logs of the containers related to one or more services in real-time, you can use the [.inline-code] docker compose logs[.inline-code] command with the [.inline-code] -f[.inline-code] flag (short for [.inline-code] --follow)[.inline-code] :
Note that this command will not only output the new logs generated by the containers in real-time, but also the previous ones. You can learn more about filtering these logs in the following sections.
[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
For example, entering [.inline-code] docker compose logs real-time[.inline-code] in the AI Command Suggestions will prompt a [.inline-code] docker[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .
[#filter-logs] Filtering container logs [#filter-logs]
In Docker Compose, container logs can be filtered in several ways.
[#output-the-last-n-lines] Outputting the last N logs [#output-the-last-n-lines]
To show the latest log entries, you can use the [.inline-code] -n[.inline-code] flag (short for [.inline-code] --tail[.inline-code] ) followed by the number of logs to display:
For example, the following command shows the latest 80 logs of the containers related to the [.inline-code] api[.inline-code] service:
[#filter-logs-by-date-and-time] Filtering logs by date and time [#filter-logs-by-date-and-time]
To filter container logs based on timeframes, you can either use the [.inline-code] --since[.inline-code] flag to fetch the logs that occurred after a specific time:
Or you can use the [.inline-code] --until[.inline-code] flag to fetch the logs that occurred before a specific time:
Where [.inline-code] time[.inline-code] is either:
- A timestamp expressed in the following format: [.inline-code] YYYY-MM-DDTHH:MM:SS.NNNNNNZ[.inline-code] .
- A relative duration expressed in the following format: [.inline-code] <number>[h|m|s][.inline-code] where [.inline-code] h[.inline-code] , [.inline-code] m[.inline-code] , [.inline-code] s[.inline-code] stand for hours, minutes, and seconds.
For example, this command will output all the logs generated in the last hour:
And this command will output all the logs generated until January 2nd, 2024:
[#filter-logs-with-patterns] Filtering logs based on patterns using the [.inline-code] grep[.inline-code] command [#filter-logs-with-patterns]
To filter container logs based on specific keywords or patterns, you can pipe the output of the [.inline-code] docker compose logs[.inline-code] command into the [.inline-code] grep[.inline-code] command as follows:
For instance, this command will output all the logs of the containers related to the [.inline-code] api[.inline-code] service containing the [.inline-code] ERROR[.inline-code] keyword:
You can learn more filtering methods by reading our article on how to filter the output of commands with grep and awk.