In this article, we cover how to view logs of a Docker container. Most users would check logs if they realize that something unusual has happened in their machine. But, logs are a great way check how efficiently our machine has been working. They provide us valuable information about our machine as and when required.
As a System Administrator one must ensure to check log records regularly. So, even if something bad happens out of the blue. The System Administrator who has been going through log records regularly would be able to handle the situation better. Or, he/she can manage an evolving adverse situation well in advance.
Since, we would be utilizing Container ID or Container Name for viewing logs. Therefore, open a terminal and issue the following to get that information (Container ID or Name) –
docker container ls -a
where, -a is used to list all containers.
View logs of a Docker Container
Use the following syntax to view logs of a Docker container –
docker container logs [OPTIONS] <Container-ID/Name>
For instance, if the Container-ID is ba017416ac98 then –
docker container logs ba017416ac98
Besides, we can also use range of [OPTIONS] which we cover next.
I. At times, we only want to get the data of specific number of lines from the end of logs. To get such data, use –tail or -n option.
docker container logs --tail <number-of-lines> <Container-ID/Name> docker container logs -n <number-of-lines> <Container-ID/Name>
docker container logs --tail 6 ba017416ac98
docker container logs -n 6 ba017416ac98
This would show last 6 lines from the end of log.
II. To show the logs with timestamps –
docker container logs --timestamps <Container-ID/Name>
III. Then, there are –since and –until options. –since option is used to view logs from a point in time. Whereas, –until is for viewing logs till a point in time.
docker container logs --since 10m ba017416ac98
docker container logs --until 10m ba017416ac98
where, 10m is ten minutes. We can also use “h” for hours as well. So, for 2 hours we can use 2h in place of 10m.
IV. Lastly, we have –follow or -f for following log output. But, there is a condition for using this option. Since, we are following a log output therefore the container must be running beforehand. This ensures we can track the output real-time.
Let’s say we start a container by –
docker container start -i ba017416ac98
Now, in other terminal just issue the following –
docker container logs -f ba017416ac98
Test it by issuing a command inside the container and look for changes that occur in the logs following output.
In conclusion, we have covered how to view logs of a Docker container here.