如何阻止 Docker 清除死容器的日志?
我使用 Dokku 来运行我的应用程序,由于某种原因,容器每隔几个小时就会死亡并重新创建自己。
为了调查这个问题,我愿意读取此容器的错误日志并了解它崩溃的原因。由于 Docker 会清除死容器的日志,因此这是不可能的。
我打开了docker events
,它显示了许多事件(例如container update
、container Kill
、container 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除容器时,日志也会被删除。如果您希望日志持久存在,那么您需要避免删除容器。确保您没有使用诸如
--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.您可以采取多种措施来调查该问题:
您可以在前台运行容器并允许其登录到您的控制台。
如果您之前使用
docker run -d
(或docker-compose up -d
)在后台启动容器,只需删除-d
从命令行并允许容器登录到您的终端。当它崩溃时,您将能够看到最新的日志并滚动回终端历史缓冲区的限制。您甚至可以使用
script
工具将此输出捕获到文件中:这会将所有输出转储到名为
typescript
的文件中,尽管您当然可以在命令行上提供不同的输出名称。您可以更改日志驱动程序。
您可以将容器配置为使用不同的日志记录驱动程序。如果您选择
syslog
或journald
之类的内容,您的容器日志将被发送到相应的服务,并且即使在容器被删除后也将继续可用。我喜欢使用
journald
日志记录驱动程序,因为这允许通过容器 ID 搜索输出。例如,如果我启动一个这样的容器:我可以通过运行以下命令查看该容器的日志:
即使在容器退出后,这些日志也会保留。
(您还可以使用
CONTAINER_ID_FULL
(完整 ID)或CONTAINER_ID
(短 ID)按容器 ID 而不是名称进行搜索,甚至可以使用按图像名称进行搜索IMAGE_NAME
。)There are several things you can do to investigate the issue:
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
(ordocker-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: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.You can change the log driver.
You can configure your container to use a different logging driver. If you select something like
syslog
orjournald
, 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:I can see logs from that container by running:
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) orCONTAINER_ID
(the short id), or even by image name withIMAGE_NAME
.)