The short answer
In Docker, retrieving the logs of containers during their execution allows to simplify the debugging and tracing issues for developers.
To output the logs of a specific Docker container, you can use the [.inline-code]docker logs[.inline-code] command as follows:
Where [.inline-code]container_id[.inline-code] is either the name or the identifier of the container, which can be obtained using the [.inline-code]docker ps[.inline-code] command.
For example, this command will output the logs of the container named [.inline-code]container_zero[.inline-code]:
And this command will output the logs of the container with the ID [.inline-code]7783cbb0deaa[.inline-code]:
Displaying container logs in real-time
To monitor the logs of a docker container in real-time, you can use the [.inline-code]docker logs[.inline-code] command with the [.inline-code]-f[.inline-code] flag (short for [.inline-code]--follow[.inline-code]):
For example:
Easily retrieve this command using Warp’s AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code]docker 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].
Filtering container logs
In Docker, container logs can be filtered in several ways.
Outputting the last N logs
To show the latest log entries, you can use the [.inline-code]--tail[.inline-code] option, followed by the number of logs to display:
For example, the following command shows the latest 80 logs of the container identified by the ID [.inline-code]5228d692b669[.inline-code]:
Filtering 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:
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 the logs generated in the last hour:
And this command will output the logs generated until January 2nd, 2024:
Filtering logs based on patterns using the [.inline-code]grep[.inline-code] command
To filter the container logs based on specific keywords or patterns, you can pipe the output of the [.inline-code]docker logs[.inline-code] command into the [.inline-code]grep[.inline-code] command:
For instance, if the container is throwing an error, you can find out more about it by searching for the [.inline-code]ERROR[.inline-code] keyword: