Terminus
Tail Container Logs In Docker

Tail Container Logs In Docker

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:

 $ docker logs <container_id>

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]:

 $ docker logs container_zero

And this command will output the logs of the container with the ID [.inline-code]7783cbb0deaa[.inline-code]:

 $ docker logs 7783cbb0deaa

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]):

 $ docker logs -f <container_id>

For example:

 $ docker logs -f 5228d692b669

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:

 $ docker logs --tail <number> <container_id>

For example, the following command shows the latest 80 logs of the container identified by the ID [.inline-code]5228d692b669[.inline-code]:

 $ docker logs --tail 80 5228d692b669

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:

 $ docker logs --since <time> <container_id>

Or you can use the [.inline-code]--until[.inline-code] flag to fetch the logs that occurred before a specific:

 $ docker logs --until <time> <container_id>

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:

 $ docker logs --since="1h" 5228d692b669

And this command will output the logs generated until January 2nd, 2024:

 $ docker logs --until="2024-01-02"  5228d692b669

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:

 $ docker logs <container_id> | grep '<pattern>'

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:

 $ docker logs 5228d692b669 | grep 'ERROR'