Docker has logging mechanisms in place which can be used to debug issues as and when they occur. There is logging at the daemon level and at the container level. Let’s look at the different levels of logging.
At the daemon logging level, there are four levels of logging available −
- Debug − It details all the possible information handled by the daemon process.
- Info − It details all the errors + Information handled by the daemon process.
- Errors − It details all the errors handled by the daemon process.
- Fatal − It only details all the fatal errors handled by the daemon process.
Go through the following steps to learn how to enable logging.
- First, we need to stop the docker daemon process, if it is already running. It can be done using the following command −
sudo service docker stop
- Now we need to start the docker daemon process. But this time, we need to append the –l parameter to specify the logging option. So let’s issue the following command when starting the docker daemon process.
sudo dockerd –l debug &
The following points need to be noted about the above command −
- dockerd is the executable for the docker daemon process.
- The –l option is used to specify the logging level. In our case, we are putting this as debug
- & is used to come back to the command prompt after the logging has been enabled.
Once you start the Docker process with logging, you will also now see the Debug Logs being sent to the console.
Now, if you execute any Docker command such as docker images, the Debug information will also be sent to the console.
Logging is also available at the container level. So in our example, let’s spin up an Ubuntu container first. We can do it by using the following command.
sudo docker run –it ubuntu /bin/bash
Now, we can use the docker log command to see the logs of the container.
Docker logs containerID
- containerID − This is the ID of the container for which you need to see the logs.
On our Docker Host, let’s issue the following command. Before that, you can issue some commands whilst in the container.
sudo docker logs 6bfb1271fcdd
From the output, you can see that the commands executed in the container are shown in the logs.