How to see docker build "RUN command" stdout? (docker for windows)
Asked Answered
L

3

49

In the past I could simply do something like this:

Dockerfile:

FROM ubuntu
RUN echo "test"

which would output test to my shell. I used this as a way of debugging my builds.

In the latest stable version of docker for windows the build output looks completely different and is not showing any stdout from any command.

How can I see the output of any command in the build process?

The current output looks like this for above Dockerfile example:

[+] Building 4.7s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                       0.0s 
 => => transferring dockerfile: 65B                                                                                        0.0s 
 => [internal] load .dockerignore                                                                                          0.0s 
 => => transferring context: 2B                                                                                            0.0s 
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                           2.0s 
 => [1/2] FROM docker.io/library/ubuntu@sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c            2.3s 
 => => resolve docker.io/library/ubuntu@sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c            0.0s 
 => => sha256:f643c72bc25212974c16f3348b3a898b1ec1eb13ec1539e10a103e6e217eb2f1 3.32kB / 3.32kB                             0.0s 
 => => sha256:da7391352a9bb76b292a568c066aa4c3cbae8d494e6a3c68e3c596d34f7c75f8 28.56MB / 28.56MB                           1.2s 
 => => sha256:14428a6d4bcdba49a64127900a0691fb00a3f329aced25eb77e3b65646638f8d 847B / 847B                                 0.5s 
 => => sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c 1.20kB / 1.20kB                             0.0s 
 => => sha256:4e4bc990609ed865e07afc8427c30ffdddca5153fd4e82c20d8f0783a291e241 943B / 943B                                 0.0s 
 => => extracting sha256:da7391352a9bb76b292a568c066aa4c3cbae8d494e6a3c68e3c596d34f7c75f8                                  0.8s 
 => => extracting sha256:14428a6d4bcdba49a64127900a0691fb00a3f329aced25eb77e3b65646638f8d                                  0.0s 
 => => extracting sha256:2c2d948710f21ad82dce71743b1654b45acb5c059cf5c19da491582cef6f2601                                  0.0s 
 => [2/2] RUN echo "test"                                                                                                  0.3s 
 => exporting to image                                                                                                     0.0s 
 => => exporting layers                                                                                                    0.0s 
 => => writing image sha256:8c61a015c1cc5af925e0db03bb56a627ce3624818c456fca11379c92c2e9d864                               0.0s 
Loireatlantique answered 5/1, 2021 at 13:43 Comment(2)
What is the output now?Asthenic
@Asthenic added the current outputLoireatlantique
L
60

Reading through When using BuildKit with Docker, how do I see the output of RUN commands? the reason for the changed output format is that instead of the "classic" docker build a new feature named "buildkit" is now being used instead.

Method 1 (taken from above questions answers)

Use docker build --progress=plain . to see the plain output.

To permanently set the progress to plain the ENV BUILDKIT_PROGRESS=plain can be set.

For a quick win (non permanent) I simply added the following line to the beginning of my scripts:

  • powershell: $env:BUILDKIT_PROGRESS="plain"
  • cmd: set BUILDKIT_PROGRESS=plain

Method 2 (Not recommended)

Disable buildkit in the settings docker engine features enter image description here

Loireatlantique answered 5/1, 2021 at 16:7 Comment(4)
Worth noting that with BuildKit it outputs to stderr and not to stdout: github.com/moby/buildkit/issues/1186Thuja
Nothing happens when I add --progress plain to my command. I do not see output of any RUN echo hello commands.Rabbinism
@Rabbinism Try redirecting your echo to stderr: RUN echo hello >&2Spalato
I've tried --progress=plain (using docker version 24.0.2 on mac), and it just work fine. no need to redirect stderr.Mackoff
B
3

Yanking up a comment from @lightster into an answer.

The accepted answer didn't work at all for me.

What worked was adding >&2 onto the end of the RUN command, eg:

RUN echo hello >&2

I'm guessing that docker is suppressing stdout as being just too noisy.

The --progress plain is still required. (Thx @Akaisteph7.)

Bucephalus answered 1/8, 2023 at 5:39 Comment(1)
This is helpful but --progress plain is still needed for this to workFeudist
C
0

The reason we don't see the results of the debug commands we added is due to how Docker handles the output of RUN commands. We can only see the debug command when there is an error and a command fails. You also need to redirect the standard error stream to the standard output with 2>&1.

Here is a command that displays the log result because the script (build.sh) of the RUN fails:

RUN ./build.sh > /var/log/build.log.txt 2>&1 || (cat /var/log/build.log.txt && false)

Or you use a technique that works by deliberately creating an error to display the result of a command:

RUN ERROR || (echo "Error detected. Here is the output of your command:" && ls -l /app && false)

The disadvantage is that you have to fail your build, but it is faster than the next solution.

Another solution is to run your docker compose with the --progress=plain and --no-cache options. The --progress=plain option displays the build logs in a detailed and unformatted way. However, its usefulness is limited when the cache is used because many steps are skipped. This is why you must not omit to include the --no-cache to force Docker to rebuild all the steps without using the cache, otherwise the command will not display anything.

docker compose build --progress=plain --no-cache

Exemple :

RUN npm list @angular/cli

=> And make sure not to forget to add 2>&1 when necessary!

Chuch answered 19/7 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.