且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查看 docker 镜像的日志?

更新时间:2023-11-24 17:39:52

最简单的方法是使用 tee 将所有命令输出的副本发送到日志文件.如果您希望将其附加到图像上,请将运行命令输出到图像内的日志文件中,例如:

Easiest method is to use tee to send a copy of all your command output to a logfile. If you want it attached to the image, output your run commands to a logfile inside of the image with something like:

RUN my-install-cmd | tee /logs/my-install-cmd.log

然后你可以运行一个快速的一次性容器来查看日志的内容:

Then you can run a quick one-off container to view the contents of the logs:

docker run --rm my-image cat /logs/my-install-cmd.log

如果您不需要附加到映像的日志,则可以完全按照 JHarris 所说的那样,对构建命令进行一次更改(而不是对运行命令进行大量更改)来记录每个构建的输出:

If you don't need the logs attached to the image, you can log the output of every build with a single change to your build command (instead of lots of changes to the run commands) exactly as JHarris says:

docker build -t my-image . | tee my-image.build.log

如果您在构建时不使用 --rm=true,那么您将拥有所有中间容器,并且每个中间容器都有一个您可以使用的日志来查看

If you build without using --rm=true, then you have all the intermediate containers, and each one of those has a log you can review with

docker logs $container_id

最后,不要忘记图像中图层的历史.它们不显示每个命令的输出,但对于不记录任何输出并知道每个层来自哪个构建的所有命令很有用,尤其是在使用大量缓存时.

And lastly, don't forget there's a history of the layers in the image. They don't show the output of each command, but it is useful for all of those commands that don't log any output and knowing which build each layer comes from particularly when there's lots of caching being used.

docker history my-image