Like any other modern software, logging events and messages like warnings and errors is an inherent part of the Docker platform, which allows you to debug your applications and production issues. We’ll be covering some simple ways in which you can manage and monitor logs for your containers. So let’s get started.

Docker Logs Command

The basic syntax to fetch logs of a container is: OR Both of the syntaxes are essentially the same, so we’ll focus on the rest of the commands in this article as docker logs. Though do note here that the above command is only functional for containers that are started with the json-file  or journald logging driver. Here OPTIONS refers to the available supported flags with docker logs command, which are listed below: Example:

Docker Logs Location

Docker, by default, captures the standard output (and standard error) of all your containers and writes them in files using the JSON format. This is achieved using JSON File logging driver or json-file. These logs are by default stored at container-specific locations under /var/lib/docker filesystem. As an example, for my redis container listed below, I can check its json logfile as shown in the snippet below:

Show Extra Details

To show extra details provided to logs, use –details flag. Example:

Follow Log Output

You can use –follow or -f flag to follow the log output. This allows you to monitor new updates in the log stream from continuously STDOUT and STDERR. Example:

Tail Logs

Container logs can be tailed to limit the number of output shown on the screen with –tail or -n flag. By default, this flag assumes all as an argument that shows the complete log stream. To show a fixed number of lines from the end of the logs, specify a positive integer number following –tail or -n flag. Example:

Show Logs Since

We can limit log output by using –since flag and giving a timestamp like an absolute value with syntax 2021-08-28T15:23:37Z or a relative one like 56m for 56 minutes. The –since option shows only the container logs generated after a given date. You can specify the date as an RFC 3339 date, a UNIX timestamp, or a Go duration string (e.g. 1m30s, 3h). The local time zone on the client will be used if you do not provide either a Z or a +-00:00 time zone offset at the end of the timestamp. You can combine the –since option with either or both of the –follow or –tail options. Example: In the above example, logs since only 2 minutes are shown where nostalgic_wescoff is the auto-generated name assigned for the nginx container.

Show Logs Until

Like –since flag, docker logs also support –until flag, which shows logs before the given timestamp. Similarly, the timestamp follows a similar convention as earlier and can be specified as an absolute value with syntax 2021-08-28T15:23:37Z or a relative one like 56m for 56 minutes. Example: In the above example, all logs before 1 hour 30 minutes are shown.

Show Timestamps

Many container applications offer timestamps built in their log output, so Docker also shows them with docker logs command. If you need Docker to explicitly prefix its timestamps in the output, use –timestamps or -t flag. Example:

Merge Flags

Docker offers to combine certain flags to get more filtered output rather than print all of the log contents on the screen. As a simple example, we can combine –tail flag with –since to get more restricted output. Example: This can work with other flags as well.

Filter With Shell Utilities

Linux shell utilities can also be used for more dexterity in the log output. Utilities like grep, head, tail etc. can be piped to docker logs output for more advanced operations. Example: Do note here that we need to redirect log streams to provide single piped input for grep using 2>&1.

Summary 👩‍💻

Docker is a versatile platform that offers numerous features to administer its environment. Managing logs for a system is one of the essential skills which every system administrator should know. Managing logs in Docker is easy once you know the available command and possible flags as per your requirements. For further read on Docker and its functionalities, refer to Docker’s documentation.

How to Check Docker Logs  - 39How to Check Docker Logs  - 66How to Check Docker Logs  - 29How to Check Docker Logs  - 32How to Check Docker Logs  - 93How to Check Docker Logs  - 53How to Check Docker Logs  - 30How to Check Docker Logs  - 67How to Check Docker Logs  - 9How to Check Docker Logs  - 87How to Check Docker Logs  - 4How to Check Docker Logs  - 97How to Check Docker Logs  - 28How to Check Docker Logs  - 20How to Check Docker Logs  - 46How to Check Docker Logs  - 25How to Check Docker Logs  - 55How to Check Docker Logs  - 8How to Check Docker Logs  - 81How to Check Docker Logs  - 13How to Check Docker Logs  - 26How to Check Docker Logs  - 23How to Check Docker Logs  - 64