如何查看 Docker 映像的日志?

在 docker 世界中,可以很容易地看到 docker 容器的日志(即正在运行的映像)。但是在图像创建过程中,通常会发出多个命令。例如,节点项目中的 npm 安装命令。查看这些命令的日志也是有益的。我迅速地从文档中搜索,但是没有找到如何获得 docker 映像的日志。有可能吗?

119541 次浏览

Update: Since this question has been asked, it seems everyone is finding it after seeing the output changes from buildkit. Buildkit includes the following options (docker build --help to see them all):

      --build-arg list          Set build-time variables
--cache-from strings      Images to consider as cache sources
-f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
--no-cache                Do not use cache when building the image
-o, --output stringArray      Output destination (format: type=local,dest=path)
--platform string         Set platform if server is multi-platform capable
--progress string         Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--pull                    Always attempt to pull a newer version of the image
-q, --quiet                   Suppress the build output and print image ID on success
-t, --tag list                Name and optionally a tag in the 'name:tag' format
--target string           Set the target build stage to build.

The option many want with buildkit is --progress=plain:

docker build -t my-image --progress=plain .

If you really want to see the previous build output, you can disable buildkit with an environment variable, but I tend to recommend against this since there are a lot of features from buildkit you'd lose (skipping unused build steps, concurrent build steps, multi-platform images, and new syntaxes for the Dockerfile for features like RUN --mount...):

DOCKER_BUILDKIT=0 docker build -t my-image .

The OP is asking to include the logs of their build within the image itself. Generally I would recommend against this, you'd want those logs outside of the image.

That said, the easiest method for that 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 those logs:

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

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

With the classic docker build command, 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

Use This: https://github.com/jcalles/docker-wtee
Read instructions and please give me feedback.
Or...
If you need to get logs from running container, and container has volumes exposed, run this:

docker run --rm -it --name testlogs --link <CONTAINERNAME/ID> --network CONTAINERNETWORK -p PORT:8080 --volumes-from CONTAINERNAME/ID  javiercalles/wtee sh

You can see the logs in powershell with this command

docker logs --details <containerId>

There other options with logs here.

Had the same problem, I solved it using

docker build --no-cache --progress=plain -t my-image .