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.
The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
ps -a. List all the docker containers running/exited/stopped with container details.
docker volume ls | Docker Documentation.
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.
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
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
.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With