133

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?

Ville Miekk-oja
  • 18,749
  • 32
  • 70
  • 106
  • 6
    All of that is piped to standard out on creation. Can you not redirect that to a log (or tee if you want both) - `docker build -t something . > build.log` ? – johnharris85 Jun 15 '16 at 11:35
  • for docker-compose, I use the following command to save it in a file: `docker-compose build --progress=plain my_image > build.log 2>&1` – ucczs May 18 '22 at 13:54

4 Answers4

240

Had the same problem, I solved it using

docker build --no-cache --progress=plain -t my-image .
diegocl02
  • 2,688
  • 2
  • 9
  • 10
78

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
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    Is that meant to be "logfile _outside_ of your image"? – Matt Jun 17 '16 at 13:55
  • I was thinking of including them inside of the image so the image and logs of it's creation were kept together. e.g. `RUN my-install-cmd | tee /logs/my-install-cmd.log` in the Dockerfile. Then you can `docker run --rm my-image cat /logs/my-install-cmd.log` to see the install logs for that image. – BMitch Jun 17 '16 at 16:41
  • Ah cool. Just checking as JHarris' comment was saving the entire build output outside. You might want to add that detail to the A. – Matt Jun 18 '16 at 04:28
2

Instead of using --progress=plain you can also use the environment variable BUILDKIT_PROGRESS

You can use it as follow:

export BUILDKIT_PROGRESS=plain
docker build ....

ref: https://docs.docker.com/engine/reference/commandline/buildx_build/#progress

Mizux
  • 8,222
  • 7
  • 32
  • 48
-7

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
orkenstein
  • 2,810
  • 3
  • 24
  • 45