Add Debug Log switch (-ld) and Context Log switch (-lcd) to docker build, outputting what's actually executed

Running docker build for a Dockerfile, it’s incredibly hard to tell the result of variable replacement or path conversion, particularly when using Windows containers.

Please, add two logging options to docker build, supposed to be outputting the Docker daemon’s context content and the actual result of parsing an instruction:

  1. --log-context-depth {unsigned byte}, or -lcd {unsigned byte}
    (default value: 0)

  2. --log-debug, or -ld

--log-context-depth {unsigned byte}

Providing a --log-context-depth with a value greater than 0 to docker build should result in outputting the content of the Docker daemon’s context in a way that would allow to copy/paste individual entries of the log output to a COPY instruction, i.e., this option should output absolute paths in a format that’s accepted by the COPY instruction. The depth value is supposed to be used to limit the depth of sub directorys being output.

Examples

Given the following folder structure:

Dockerfile
My Folder
|-- My Sub Folder
|   |-- My Sub File 1.txt
|   |-- My Sub File 2.txt
|   |-- My Sub Sub Folder
|   |   |-- My Sub Sub File 1.txt
|   |   |-- My Sub Sub File 2.txt
|-- My File 1.txt
|-- My File 2.txt

This is the suggested output when using --log-context-depth:

> docker build -lcd 1 ...
Sending build context to Docker daemon
Context content:
-----------------------------------------
"\Dockerfile"
"\My Folder\"
-----------------------------------------
> docker build -lcd 2 ...
Sending build context to Docker daemon
Context content:
-----------------------------------------
"\Dockerfile"
"\My Folder\"
"\My Folder\My Sub Folder\"
"\My Folder\My File 1.txt"
"\My Folder\My File 2.txt"
-----------------------------------------

--log-debug

Providing this command line argument should result in logging the actual instruction that’s being executed in the writable layer of the container, after variables have been replaced and paths have been canonicalized.

Examples

Given a Dockerfile content like this:

FROM mcr.microsoft.com/windows/servercore:20H2
ENV myPath="C:\Program Files\"
COPY "My Sub Folder\..\MyProg\" "${myPath}My Prog\"

This is the suggested output when using --log-debug:

> docker build -ld ...
Sending build context to Docker daemon
Step 1/3 : FROM mcr.microsoft.com/windows/servercore:20H2
FROM https://hub.docker.com/mcr.microsoft.com/windows/servercore:20H2
---> d7c03b5bcc73
Step 2/3 : ENV myPath="C:\Program Files\"
 ---> c8c23a14f7f3
Step 3/3 : COPY "My Sub Folder\..\MyProg\" "${myPath}My Prog\"
COPY "\MyProg\" "C:\Program Files\My Prog\"

This sounds like a feature request that should should be created on the according moby Github.

Thanks for pointing me!

I create a feature request now here: