如何阻止 Docker 清除死容器的日志?

发布于 2025-01-10 06:29:23 字数 393 浏览 0 评论 0原文

我使用 Dokku 来运行我的应用程序,由于某种原因,容器每隔几个小时就会死亡并重新创建自己。

为了调查这个问题,我愿意读取此容器的错误日志并了解它崩溃的原因。由于 Docker 会清除死容器的日志,因此这是不可能的。

我打开了docker events,它显示了许多事件(例如container updatecontainer Killcontainer die等.)但没有迹象表明是什么触发了这次杀戮。

我该如何调查这个问题?


版本:

  • Docker 版本 19.03.13,构建 4484c46d9d
  • dokku 版本 0.25.1

I use Dokku to run my app, and for some reason, the container is dying every few hours and recreates itself.

In order to investigate the issue, I am willing to read the error logs to this container and understand why it's crashing. Since Docker clears logs of dead containers, this is impossible.

I turned on docker events and it shows many events (like container update, container kill, container die, etc.) But no sign of what triggered this kill.

How can I investigate the issue?


Versions:

  • Docker version 19.03.13, build 4484c46d9d
  • dokku version 0.25.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2025-01-17 06:29:23

删除容器时,日志也会被删除。如果您希望日志持久存在,那么您需要避免删除容器。确保您没有使用诸如 --rm 之类的选项来运行容器,该选项会在退出时自动将其删除。并检查明显的问题,例如磁盘空间不足。

Logs are deleted when the container is deleted. If you want the logs to persist, then you need to avoid deleting the container. Make sure you aren't running the container with an option like --rm that automatically deletes it on exit. And check for the obvious issues like running out of disk space.

等数载,海棠开 2025-01-17 06:29:23

您可以采取多种措施来调查该问题:

  1. 您可以在前台运行容器并允许其登录到您的控制台。

    如果您之前使用docker run -d(或docker-compose up -d)在后台启动容器,只需删除-d 从命令行并允许容器登录到您的终端。当它崩溃时,您将能够看到最新的日志并滚动回终端历史缓冲区的限制。

    您甚至可以使用script 工具将此输出捕获到文件中:

    script -c 'docker run ...`
    

    这会将所有输出转储到名为 typescript 的文件中,尽管您当然可以在命令行上提供不同的输出名称。

  2. 您可以更改日志驱动程序。

    您可以将容器配置为使用不同的日志记录驱动程序。如果您选择syslogjournald之类的内容,您的容器日志将被发送到相应的服务,并且即使在容器被删除后也将继续可用。

    我喜欢使用 journald 日志记录驱动程序,因为这允许通过容器 ID 搜索输出。例如,如果我启动一个这样的容器:

    docker run --log-driver Journald --name web -p 8080:8080 -d docker.io/alpinelinux/darkhttpd
    

    我可以通过运行以下命令查看该容器的日志:

    $journalctl CONTAINER_NAME=web
    2 月 25 日 20:50:04 docker 0bff1aec9b65[660]:darkhttpd/1.13,版权所有 (c) 2003-2021 Emil Mikulic。
    

即使在容器退出后,这些日志也会保留。

(您还可以使用 CONTAINER_ID_FULL (完整 ID)或 CONTAINER_ID (短 ID)按容器 ID 而不是名称进行搜索,甚至可以使用 按图像名称进行搜索IMAGE_NAME。)

There are several things you can do to investigate the issue:

  1. You can run the container in the foreground and allow it to log to your console.

    If you were previously starting the container in the background with docker run -d (or docker-compose up -d), just remove the -d from the command line and allow the container to log to your terminal. When it crashes, you'll be able to see the most recent logs and scroll back to the limits of your terminal's history buffer.

    You can even capture this output to a file using e.g. the script tool:

    script -c 'docker run ...`
    

    This will dump all the output to a file named typescript, although you can of course provide a different output name on the command line.

  2. You can change the log driver.

    You can configure your container to use a different logging driver. If you select something like syslog or journald, your container logs will be sent to the corrresponding service, and will continue to be available even after the container has been deleted.

    I like use the journald logging driver because this allows searching for output by container id. For example, if I start a container like this:

    docker run --log-driver journald --name web -p 8080:8080 -d docker.io/alpinelinux/darkhttpd
    

    I can see logs from that container by running:

    $ journalctl CONTAINER_NAME=web
    Feb 25 20:50:04 docker 0bff1aec9b65[660]: darkhttpd/1.13, copyright (c) 2003-2021 Emil Mikulic.
    

These logs will persist even after the container exits.

(You can also search by container id instead of name by using CONTAINER_ID_FULL (the full id) or CONTAINER_ID (the short id), or even by image name with IMAGE_NAME.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文