A Guide to Docker Logs – Docker日志指南

最后修改: 2022年 5月 25日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Docker is an operating system-level virtualization platform that allows us to host applications in containers. Furthermore, it facilitates the separation of applications and infrastructure for fast software delivery.

Docker是一个操作系统级的虚拟化平台,它允许我们在容器中托管应用程序。此外,它促进了应用程序和基础设施的分离,以便快速交付软件。

Log files generated by Docker containers contain a variety of useful information. Whenever an event occurs, the Docker container creates log files.

Docker 容器生成的日志文件包含各种有用信息。每当有事件发生,Docker容器就会创建日志文件。

Docker generates the logs to the STDOUT or STDERR, including log origin, output stream data, and timestamp. Debugging and finding the root cause of problems can be done using log files.

Docker会生成日志到STDOUT或STDERR,包括日志来源、输出流数据和时间戳。调试和寻找问题的根本原因可以通过日志文件来完成。

In this tutorial, we’ll look into accessing the Docker logs in different ways.

在本教程中,我们将研究以不同方式访问Docker日志。

2. Understanding Docker Logs

2.了解Docker日志

In Docker, primarily, there are two types of log files. The Docker daemon logs provide insight into the Docker service’s overall status. The Docker container logs cover all the logs related to a particular container.

在Docker中,主要有两种类型的日志文件。Docker守护程序日志提供了对Docker服务整体状态的洞察力。Docker容器日志涵盖了与特定容器相关的所有日志。

We’ll primarily explore the different commands to access the Docker container logs. We’ll examine the container logs using the docker logs command and by directly accessing the logs file on the system.

我们将主要探讨访问Docker容器日志的不同命令。我们将使用docker logs命令和直接访问系统中的日志文件来检查容器日志。

The log files are useful to debug problems because they provide details about what occurred. By analyzing Docker logs, we can diagnose and troubleshoot issues faster.

日志文件对于调试问题非常有用,因为它们提供了有关发生的细节。通过分析Docker日志,我们可以更快地诊断和排除问题。

3. Using the docker logs Command

3.使用docker logs命令

Before we move forward, lets’ first run a sample Postgres Docker container:

在我们继续前进之前,让我们先运行一个Postgres Docker容器样本。

$ docker run -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung -p 5432:5432 -v /data:/var/lib/postgresql/data --name postgresql-baedlung postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
214ca5fb9032: Pull complete 
...
95df4ec75c64: Pull complete 
Digest: sha256:2c954f8c5d03da58f8b82645b783b56c1135df17e650b186b296fa1bb71f9cfd
Status: Downloaded newer image for postgres:latest
bce34bb3c6175fe92c50d6e5c8d2045062c2b502b9593a258ceb6cafc9a2356a

To illustrate, let’s check out the containerId of the postgresql-baedlung container:

为了说明问题,让我们看看containerIdpostgresql-baedlung容器。

$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
bce34bb3c617        postgres            "docker-entrypoint.s…"   12 seconds ago      Up 10 seconds       0.0.0.0:5432->5432/tcp   postgresql-baedlung

As we can see from the output of the above command, the postgresql-baedlung is running with the containerId “bce34bb3c617”. Let’s now explore the docker logs command to monitor the logs:

我们可以从上述命令的输出中看到,postgresql-baedlung正在运行,容器Id为 “bce34bb3c617″。现在让我们来探讨一下docker logs命令来监控日志。

$ docker logs bce34bb3c617
2022-05-16 18:13:58.868 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1)
  on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 18:13:58.869 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 18:13:58.869 UTC [1] LOG:  listening on IPv6 address "::", port 5432

Here, the logs contain data of the output stream with the timestamp. The above command doesn’t contain the continuous log output. To view the continuous log output of a container, we need to use the “–follow” option in the docker logs command.

这里,日志包含带有时间戳的输出流数据。上面的命令并不包含连续的日志输出。要查看一个容器的连续日志输出,我们需要在docker logs命令中使用“-follow”选项。

The “–follow” option is one of the most useful Docker options as it allows us to monitor the live logs of a container:

“-follow”选项是最有用的Docker选项之一,因为它允许我们监控一个容器的实时日志。

$ docker logs --follow  bce34bb3c617
2022-05-16 18:13:58.868 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1)
  on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
...
2022-05-16 18:13:59.018 UTC [1] LOG:  database system is ready to accept connections

One of the drawbacks of the above command is that it will contain all the logs from the start. Let’s check out the command to view the continuous log output with the recent records:

上述命令的一个缺点是,它将包含从一开始的所有日志。让我们看看这个命令,查看最近记录的连续日志输出。

$ docker logs --follow --tail 1 bce34bb3c617
2022-05-16 18:13:59.018 UTC [1] LOG:  database system is ready to accept connections 

We can also use the “since” option with the docker log command to view the file from a particular time:

我们还可以使用“since”选项和docker log命令来查看特定时间的文件。

$ docker logs --since 2022-05-16  bce34bb3c617
2022-05-16 18:13:58.868 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1)
  on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
...
2022-05-16 18:13:59.018 UTC [1] LOG:  database system is ready to accept connections

Alternatively, we can also use the docker container logs command instead of the docker logs command:

另外,我们也可以使用docker container logs命令来代替docker logs命令。

$ docker container logs --since 2022-05-16  bce34bb3c617
2022-05-16 18:13:58.868 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1)
  on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
...
2022-05-16 18:13:59.018 UTC [1] LOG:  database system is ready to accept connections

Here, we can see from the above output that both the command works exactly the same. The docker container logs command is deprecated in the newer versions.

在这里,我们可以从上面的输出中看到,两个命令的工作原理完全相同。docker容器日志命令在较新的版本中被弃用。

4. Using Default Log File

4.使用默认日志文件

The Docker stores all the STDOUT and STDERR output in JSON format. Additionally, it is possible to monitor all the live Docker logs from the host machine. By default, Docker stores log files in a dedicated directory on the host using the json-file log driver. The log file directory is /var/lib/docker/containers/<container_id> on the host where the container is running.

Docker以JSON格式存储所有的STDOUT和STDERR输出。此外,还可以从主机上监控所有实时的Docker日志。默认情况下,Docker使用json-file日志驱动将日志文件存储在主机上的一个专用目录中。日志文件目录是/var/lib/docker/containers/<container_id> 在容器运行的主机上。

To demonstrate, let’s check out the log file of our postgress-baeldung container:

为了演示,让我们看看我们的postgress-baeldung容器的日志文件。

$ cat /var/lib/docker/containers/bce34bb3c6175fe92c50d6e5c8d2045062c2b502b9593a258ceb6cafc9a2356a/
  bce34bb3c6175fe92c50d6e5c8d2045062c2b502b9593a258ceb6cafc9a2356a-json.log 
{"log":"\r\n","stream":"stdout","time":"2022-05-16T18:13:58.833312658Z"}
{"log":"PostgreSQL Database directory appears to contain a database; Skipping initialization\r\n","stream":"stdout","time":"2022-05-16T18:13:58.833360038Z"}
{"log":"\r\n","stream":"stdout","time":"2022-05-16T18:13:58.833368499Z"}

In the above output, we can see that the data is in JSON format.

在上面的输出中,我们可以看到,数据是JSON格式的。

5. Clearing the Log File

5.清除日志文件

Sometimes we run out of disk space on our system, and we notice that the Docker log files are taking up a lot of space. For that, we first need to locate the log files and then delete them. Also, make sure that clearing the log file does not impact the state of the running container.

有时候,我们的系统中的磁盘空间用完了,我们会发现Docker日志文件占用了大量的空间。为此,我们首先需要找到日志文件,然后删除它们。同时,要确保清除日志文件不会影响运行中的容器的状态。

Below is the command to clear all the log files stored on the host machine:

以下是清除存储在主机上的所有日志文件的命令。

$ truncate -s 0 /var/lib/docker/containers/*/*-json.log 

Notice that the above command will not delete the log file. Instead, it will remove all the content inside the log file. By executing the below command, we can delete the log files associated with a specific container:

注意,上述命令不会删除日志文件。相反,它将删除日志文件中的所有内容。通过执行下面的命令,我们可以删除与特定容器相关的日志文件。

$ truncate -s 0 /var/lib/docker/containers/dd207f11ebf083f97355be1ae18420427dd2e80b061a7bf6fb0afc326ad04b10/*-json.log 

At the start of the container, we can also limit the size of a log file externally using the “–log-opt max-size” and “–log-opt max-file” options of the docker run command:

在容器开始时,我们也可以使用“-log-opt max-size”和”-log-opt max-file”命令的选项从外部限制一个日志文件的大小。

$ docker run --log-opt max-size=1k --log-opt max-file=5 -itd -e POSTGRES_USER=baeldung -e POSTGRES_PASSWORD=baeldung -p 5432:5432
  -v /data:/var/lib/postgresql/data --name postgresql-baedlung postgres
3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32

Now, let’s checkout the number of log files and log file size in the /var/lib/docker/containers/3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32 directory:

现在,让我们检查一下/var/lib/docker/containers/3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32目录下的日志文件数量和日志文件大小。

$ ls -la
total 68
drwx------. 4 root root 4096 May 17 02:06 .
drwx------. 5 root root  222 May 17 02:07 ..
drwx------. 2 root root    6 May 17 02:02 checkpoints
-rw-------. 1 root root 3144 May 17 02:02 config.v2.json
-rw-r-----. 1 root root  587 May 17 02:06 3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32-json.log
-rw-r-----. 1 root root 1022 May 17 02:06 3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32-json.log.1
-rw-r-----. 1 root root 1061 May 17 02:06 3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32-json.log.2
-rw-r-----. 1 root root 1056 May 17 02:06 3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32-json.log.3
-rw-r-----. 1 root root 1058 May 17 02:06 3eec82654fe6c6ffa579752cc9d1fa034dc34b5533b8672ebe7778449726da32-json.log.4
-rw-r--r--. 1 root root 1501 May 17 02:02 hostconfig.json
-rw-r--r--. 1 root root   13 May 17 02:02 hostname
-rw-r--r--. 1 root root  174 May 17 02:02 hosts
drwx------. 2 root root    6 May 17 02:02 mounts
-rw-r--r--. 1 root root   69 May 17 02:02 resolv.conf
-rw-r--r--. 1 root root   71 May 17 02:02 resolv.conf.hash

Here, we can see that five log files are created, and the size of each log file is 1 kb max. If we delete some log files, in that case, we will generate a new log with the same log file name.

在这里,我们可以看到有五个日志文件被创建,每个日志文件的大小最大为1kb。如果我们删除一些日志文件,在这种情况下,我们将生成一个新的日志,其日志文件名称相同。

We can also provide the configuration of log max-size and max-file in the /etc/docker/daemon.json file. Let’s look into the configuration of the daemon.json file:

我们还可以在/etc/docker/daemon.json文件中提供日志max-sizemax-file的配置。让我们来看看daemon.json文件的配置。

{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "1k",
        "max-file": "5" 
    }
}

Here, we provided the same configuration in the daemon.json, and importantly, all the new containers will run with this configuration. After updating the daemon.json file, we’ll need to restart the Docker service.

在这里,我们在daemon.json,中提供了相同的配置,重要的是,所有新的容器将以这种配置运行。在更新daemon.json文件后,我们需要重新启动Docker服务。

6. Redirecting the Docker Container Logs to a Single File

6.将Docker容器的日志重定向到一个文件中

By default, the Docker containers log files are stored in /var/lib/docker/containers/<containerId> dir. In addition, we can also redirect the Docker containers logs to some other file.

默认情况下,Docker容器的日志文件存储在/var/lib/docker/containers/<containerId> dir。此外,我们也可以将Docker容器的日志重定向到其他文件。

To illustrate, let’s look into the command to redirect the container’s log:

为了说明这一点,我们来看看重定向容器的日志的命令。

$ docker logs -f containername &> baeldung-postgress.log &

Here, in the above command, we redirect all the live logs to the baeldung-postgress.log file. Also, we run this command in the background using the &, so it will keep running until it is explicitly stopped.

在这里,在上述命令中,我们将所有的实时日志重定向到baeldung-postgress.log文件。另外,我们使用&在后台运行这个命令,所以它将一直运行,直到明确停止。

7. Conclusion

7.结语

In this tutorial, we learned different ways to monitor the logs of a container. First, we looked at the docker logs, and the docker container logs command to monitor the live logs. Later, we monitored the logs using the default containers log file.

在本教程中,我们学习了监控容器日志的不同方法。首先,我们看了docker logs,docker container logs命令来监控实时日志。后来,我们使用默认的容器日志文件来监控日志。

Finally, we looked into the clearing and redirecting of the log file. In short, we looked into monitoring and truncating the log file.

最后,我们研究了日志文件的清除和重定向问题。简而言之,我们研究了监控和截断日志文件的问题。