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. We can use the Docker logs command which retrieves the container logs in batches during the execution. The general syntax is -
$ docker logs [OPTIONS] CONTAINER
Along with it, we can use several options as mentioned below.
|Show extra details provided to logs
|--follow , -f
|Follow log output
|Show logs since timestamp or relative (e.g. 42m for 42 minutes)
|Show logs before a timestamp or relative (e.g. 42m for 42 minutes)
|--timestamps , -t
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.
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:
- What is Docker Networking?
- What is Docker Swarm?
- What is Docker Compose?
- What is Docker Container Linking?
- What is Docker Logging?
- Difference between Docker Images and Container
- Docker Interview Questions
- How to Use a Dockerfile to Build Docker Images?
- How to list Docker Images?
- How to Search Docker Images using CLI?