Docker logs Command
In this article, we will explain the Docker logs Command.
Every time a container is run, it generates logs of its activities. The docker logs
command shows information logged by a running container. Logs are the best way to understand the inner workings of a container.
Docker logs are important for developers and DevOps engineers. Logs are most useful when debugging or analyzing a problem because they offer insights into what went wrong. Thus, developers and engineers can solve issues faster.
By default, docker logs
or docker service logs
shows the command’s output just as it would appear if you run the command interactively in a terminal.
Let’s look at this command a bit more closely.
1. Docker Logs Location
By default, Docker containers emit logs to the stdout
and stderr
output streams. Containers are stateless, and the logs are stored on the Docker host in JSON files by default.
These logs are emitted from output streams, annotated with the log origin, either stdout
or stderr
, and a timestamp. Each log file contains information about only one container and is in JSON format. There is one log file per container.
You find these JSON log files in the /var/lib/docker/containers/
directory on a Linux Docker host. <container_id>
here is the id of the running container.
If you’re not sure which id is related to which container, you can run the docker ps
command to list all running containers.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS fcadf2fa4144 def/hello-world-image:latest "/opt/bin/entrypoint-" 3 hours ago Up 1 minutes 155ds6af2f03 lmn/my-docker-image:latest "/entrypoint.sh " 1 hours ago Up 3 minutes
/var/lib/docker/containers/
2. Reading Docker Logs
Now that we’re sure our container is running, let’s use the container ID to see all its logs. Enter the following command in your command-line interface (CLI), replacing <container ID> with your container’s ID:
docker logs <container ID>
The above command will show us the logs. But it won’t allow us to view continuous log output. In Docker jargon, we refer to creating a continuous stream of log output as tailing logs.
2.1 Tailing Logs: –follow option
The command to tail the logs for our containers is as follows
docker logs --follow <container ID>
The above command will continue streaming the new output from the container’s STDOUT
and STDERR
.
But what if I am interested not only in the latest logs but also a few days prior to the current date. In other words what if I want the logs created after a specific date?
2.2 Logs since: –since option
docker logs --since <container ID>
The above command shows only the container logs generated after a given date. The accepted format here is YYYY-MM-DDTHH:MM:SS. This means you can specify a very accurate timestamp from which you want to display logs, or a less specific timestamp, as shown in the example below.
docker logs --since 2019-03-02 <container ID>
Now, let’s say we do not want latest logs but instead want the logs until a specific point in time. Well Docker has a command for that too.
2.3 Logs until : –until option
Docker provides the option to only stream logs from a specific time. For this, you can use the until option with the follow option. The until option allows you to specify a time span for which the container should print logs to your CLI.
docker logs --follow --until = <timeperiod> <container_id>
You can use different notations to designate the timescale. For example, to see the logs for the last 30 minutes, use the following command
docker logs --follow --until=30m fcadf2fa4144
OR
docker logs --follow --until=2021-01-02T13:23:37 fcadf2fa4144
2.4 Tail Docker Logs: –tail option
In some cases, you want to restrict the number of lines printed on your screen from the Docker logs.
In order to achieve this result, you will have to use the “–tail” option in the following way.
docker logs --tail <number> <container_id>
For example, in order to show 100 lines from your Grafana for Docker logs, you will have to write
docker logs --tail 100 grafana
2.5 Logs Timestamp : –timestamps option
The below command shows timestamps for every message in the logs. If we round the timestamp generated by Docker, we can see that it differs by one nanosecond
docker logs --timestamps <container_id>
docker logs --timestamps python_example
3. Summary
In this article, we tried to understand the importance of docker logs. We also looked at where to locate the logs and view the logs
Then we looked at various viewing options such as
- Viewing logs after a specific timestamp
- Viewing logs until a specific timestamp (absolute or relative)
- Viewing the last n lines of the logs and
- finally how to add timestamps to every entry in the log file.