You are reading the article Guide To How Does Logging Work In Docker? updated in October 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Guide To How Does Logging Work In Docker?
Introduction to Docker LoggingDocker 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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:programdatadockerconfig on Windows server hosts. We can set options in the chúng tôi 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 levelDaemon-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
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 chúng tôi file located at /etc/docker to change the default daemon logging level.
We add “debug”: true in the chúng tôi 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 levelContainer-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:
docker info
Output:
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:
Example:
Code:
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.
ConclusionDocker 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 ArticlesThis 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 –
You're reading Guide To How Does Logging Work In Docker?
Update the detailed information about Guide To How Does Logging Work In Docker? on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!