How to view logs for a docker image?

DockerDockerfileDocker Image

Docker Problem Overview


In the docker world, one can easily see logs for docker container (that is, a running image). But during image creation, one usually issues multiple commands. For example npm install commands in node projects. It would be beneficial to see logs for those commands as well. I quickly searched from the documentation, but didn't find how one can obtain logs for docker image. Is it possible?

Docker Solutions


Solution 1 - Docker

Had the same problem, I solved it using

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

Solution 2 - Docker

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

Solution 3 - Docker

You can see the logs in powershell with this command

docker logs --details <containerId>

There other options with logs here.

Solution 4 - Docker

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVille Miekk-ojaView Question on Stackoverflow
Solution 1 - Dockerdiegocl02View Answer on Stackoverflow
Solution 2 - DockerBMitchView Answer on Stackoverflow
Solution 3 - DockerJohn KuriakoseView Answer on Stackoverflow
Solution 4 - DockerJavier CallesView Answer on Stackoverflow