Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker command/option to display or list the build context

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.

like image 907
Steve Avatar asked May 05 '17 15:05

Steve


People also ask

Where is the build context for Docker?

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.

What command is use to list all the Docker?

ps -a. List all the docker containers running/exited/stopped with container details.

Which command displays a list of volumes created by Docker?

docker volume ls | Docker Documentation.


4 Answers

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:

enter image description here

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.

like image 92
RawCode Avatar answered Oct 03 '22 14:10

RawCode


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
like image 38
Ricardo Branco Avatar answered Oct 03 '22 12:10

Ricardo Branco


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.

like image 22
whitone Avatar answered Oct 03 '22 13:10

whitone


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/

like image 41
Jesper Rønn-Jensen Avatar answered Oct 03 '22 13:10

Jesper Rønn-Jensen