Docker command/option to display or list the build context
DockerDockerfileDocker Problem Overview
Is there a command/option to display or list the context which is sent to the Docker daemon for building an image?
$ docker build -t "image-name"
Sending build context to Docker daemon 8.499 MB
...
Files and directories can be excluded from the build context by specifying patterns in a .dockerignore
file. I guess what I'm looking for amounts to testing the .dockerignore
in addition to any other niche rules Docker uses when determined the context.
Docker Solutions
Solution 1 - Docker
Answers above are great, but there is a low-tech solution for most cases - ncdu
. This utility will show pretty and interactive tree structure with sizes. It has an option that will take patterns from a file and exclude them from scan. So you can just do ncdu -X .dockerignore
. You will get something like this:
This is pretty close to what you will get in your docker image. One caveat is thou if you add a dot directory (like .yarn
) into an image, it will not show in ncdu
output.
Solution 2 - Docker
The only way would be to add the current directory to an specific directory and list it.
Try building with this Dockerfile:
FROM busybox
RUN mkdir /tmp/build/
# Add context to /tmp/build/
COPY . /tmp/build/
Build it with:
docker build -t test .
List all the files and directories in /tmp/build:
docker run --rm -it test find /tmp/build
Solution 3 - Docker
Starting with version 18.09, Docker has an option to export context data using BuildKit backend.
It's not enabled by default, so you need to set an environment variable DOCKER_BUILDKIT=1
before invoking docker build
command.
The following command can work also if you don't have any Dockerfile in current directory.
printf 'FROM scratch\nCOPY . /' | DOCKER_BUILDKIT=1 docker build -f- -o context .
When you run multiple times remember to delete previous export with rm -r context
.
You can also get context data as archive and then mount with archivemount
command:
printf 'FROM scratch\nCOPY . /' | DOCKER_BUILDKIT=1 docker build -f- -o- . > context.tar
mkdir context
archivemount context.tar context
With both methods, then you can explore the result with ncdu context
.
Solution 4 - Docker
Updated answer: Since 2017, Docker has recommended to use COPY
instead of ADD
and with the comment from @tlrobinson, the simpler Dockerfile looks like so:
# debug and list the docker build context so that you can minimmize it
#
# usage:
# docker build -f docker/context.Dockerfile -t test/buildcontext .
#
######################
FROM busybox
RUN mkdir /tmp/build/
# Add context to /tmp/build/
COPY . /tmp/build
# this last command outputs the list of files added to the build context:
RUN find /tmp/build/
Solution 5 - Docker
What worked for me is to do the following (based on this article).
-
Tell Docker to use the old build kit. In PowerShell that is:
$ENV:DOCKER_BUILDKIT=0
-
Run Docker build so that it reports ALL the progress it's making:
docker build --progress=plain BLAH
Given those two things you can then do something as simple as this in your Docker file:
RUN ls /app
And that will give you a list out of everything in the /app folder.