Introduction to Docker Logging

By | June 16, 2021

We can leverage the logging mechanisms provided by Docker to debug failures and issues when they occur. Docker logs provide insights into what is happening inside a Docker container. In case a Container crashes, you can use the Docker logs to check out what happened wrong. In fact, it should be a practice to monitor Docker logs on daily basis. In this article, we will discuss how to leverage the Docker logging and check out the processes and commands that are being executed or have already completed inside a Docker container.

What is Docker Loggging?

Docker containers, by default, emit the logs to the stderr and stdout streams. The logs are stored in JSON files on the Docker host machine itself. Docker logging drivers are plugins that can be activated or installed if you want to export the logs to tools such as syslog, logstash, etc. These external tools can be used to manage and monitor docker logs.

Let’s find out the default Docker logging driver using the Docker info command.

$ docker info | grep Logging

You can see that the default Logging driver is json-file.

Vamware

We can use the Docker logs command which retrieves the container logs in batches during the execution.

The general syntax is –

Vamware
$ docker logs [OPTIONS] CONTAINER

Along with it, we can use several options as mentioned below.

Options Description
–details Show extra details provided to logs
–follow , -f Follow log output
–since Show logs since timestamp or relative (e.g. 42m for 42 minutes)
–until Show logs before a timestamp or relative (e.g. 42m for 42 minutes)
–timestamps , -t Show timestamps

Let’s check out a few examples.

Let’s create a simple Ubuntu container and execute a few instructions inside it, like creating a file, listing directories, print the file, etc.

$ docker run -it --name=myubuntucont ubuntu
# ls

# echo “Welcome” > welcome.txt
# ls
# cat welcome.txt
# exit

Now, let’s use the Docker logs command on this container.

$ docker logs myubuntucont

As soon as you run the command, it will show the exact sequence of steps performed on the container.

Wrapping Up!

To conclude, in this article, we discussed how to leverage the Docker logs command to check out what’s happening inside Docker containers. We saw the different options that can be used along with a practical example.

We hope that you will now be able to get hands-on with Docker logs after going through this article.

Happy Learning!

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published. Required fields are marked *