Updated July 1, 2023
Introduction to Docker Logging
Docker Logging helps to troubleshoot errors related to Docker. It is a mechanism to debug the issues that occur in our Docker environment. For example, if the Docker daemon or any container running on Docker is not starting up properly, then we need to check the logs to know what is causing the issue. Docker has two levels of logging. One is at the Docker daemon level, and the one is at the container level.
How does Logging work in Docker?
Docker has multiple mechanisms to log information from running containers and services. We say these mechanism logging drivers. A default logging driver, already configured for each Docker daemon, is used by containers if we do not configure it to use any different driver. There are also logging driver plugins other than built-in logging drivers that we can implement to extend and customize Docker’s logging capabilities.
We need to update the ‘daemon.json’ file to explicitly configure the default logging driver to any other logging driver; the ‘daemon.json’ file is located in /etc/docker/ on Linux hosts or c:\programdata\docker\config\ on Windows server hosts. We can set options in the daemon.json file as json objects with the key ‘log-opts’ if the logging driver allows doing so. We use the ‘docker log’ command to check the logs generated by any container.
1. Docker logging at daemon level
Daemon-level docker logging is used to fix issues and errors that are generated by the Docker daemon itself.
There are four levels of logging available at the daemon level.
- Debug: It provides each and every possible information generated by the Docker daemon process.
- Info: It provides all types of information and errors generated by the Docker daemon process.
- Error: It provides all the errors generated by the daemon process.
- Fatal: It only details all the fatal errors generated by the daemon process.
We can enable Docker daemon logging by command or editing the ‘daemon.json’ file. We need to stop the Docker process in either case.
Let’s do it by the command first. So first, we need to stop the Docker daemon process using the below command:
sudo service docker stop
Or
sudo systemctl stop docker
Then we need to start the Docker daemon process with the option ‘-l’ and specify the logging level. For example, debug, info, etc.
Code:
dockerd -l debug &
Output:
Code:
dockerd -l info &
Output:
Explanation:
In the above two snapshots, we can see when we specified the ‘debug’ option, we got all the information, including debug information; however, when we specified the ‘info’ option, there was no ‘debug’ log in it, only an error, warning, and info.
We can also edit the daemon.json file located at /etc/docker to change the default daemon logging level.
We add “debug”: true in the daemon.json file as shown below and start the Docker daemon process.
Code:
cat /etc/docker/daemon.json
Output:
Below is the snapshot before enabling debug level daemon logging, and the default logging level is set to ‘info’ as there is debug log showing in the below snapshot.
Code:
dockerd
Output:
And below is the snapshot after enabling the debug level daemon logging by editing the ‘daemon.json’ file, and there is debug log in the snapshot; however, we are using the same command as above again.
Code:
dockerd
Output:
2. Docker logging at the container level
Container-level Docker logging is used for debugging errors and issues related to containers or services. For example, getting an error while starting a container or container continuously crashing or stuck in loopback, etc. The default logging driver for container-level logging is ‘json-file’. We can again configure the ‘daemon.json’ file to change the default setting. We can set the value of ‘log-driver’ to the name of the logging driver and specify the log options using the ‘log-opts’ key if different logging options are available.
We use the ‘docker info’ command to know the default logging driver for containers as below.
Code:
docker info
Output:
Explanation:
In the above snapshot, we see that ‘json-file’ is set as the default logging driver. It also gives details about plugins as well, such as awslogs, fuentd, syslog, etc.
Let’s change the default logging driver to ‘syslog’ and set some log options as well in the ‘daemon.json’ file.
Code:
sudo cat /etc/docker/daemon.json
Output:
Explanation:
In the above example, the default logging driver has been set to ‘syslog’ with options like tag, labels, and env, which is self-explanatory. We need to restart the docker to apply these changes. First stopped the docker daemon and then started it again. If we check now using the ‘docker info’ command, we can see the default logging driver has been changed to ‘syslog.’
Code:
sudo systemctl stop docker
sudo systemctl start docker
docker info
Output:
We can also configure the logging driver per container. We can do this by passing the ‘–log-driver’ flag while starting the container. It bypasses the default logging driver configured in the ‘daemon.json’ file.
Example:
Code:
docker run -d --log-driver syslog alpine
Output:
Explanation:
In the above example, I started an alpine container and configured the container to use ‘syslog’ as the logging driver. If we want to check which logging driver is used by a container, we inspect the container as shown in the snapshot and scroll down and find the “HostConfig” key and look for the “LogConfig” type, and here, it is ‘syslog.’
We can check the logs generated by the containers using ‘docker log’.
Syntax:
docker log <container_id or container_name>
Example:
Code:
docker run -d mysql
docker log 4b0ee
Output:
Explanation:
In the above snapshot, a container has been created with a default logging driver, and if we check the logs of the container, it shows us errors, and now we can understand why the container failed to start as it requires any one of the variables to be passed while running this container. So logging helps to troubleshoot the issues.
Conclusion
Docker logging is useful and necessary to understand what is happening behind the scenes. If we use a different logging driver, we need tools to read those logs, as Docker logs cannot read logs generated by all other logging drivers. There are other plugins available however, we need to configure those properly in the ‘daemon.json’ file, and we can get the available options from the official website of those plugin providers.
Recommended Articles
This is a guide to Docker Logging. Here we discuss the introduction to Docker Logging and how logging work with a detailed explanation. You may also have a look at the following articles to learn more –