After reading the .dockerignore
documentation, I'm wondering if there is a way to test it?
Examples
**/node_modules/
How do I check my dockerfile ignore the correct files and directories?
After reading the .dockerignore
documentation, I'm wondering if there is a way to test it?
**/node_modules/
How do I check my dockerfile ignore the correct files and directories?
To expand on VonC's suggestion, here's a sample build command you can use to create an image with the current folder's build context:
docker image build --no-cache -t build-context -f - . <<EOF
FROM busybox
WORKDIR /build-context
COPY . .
CMD find .
EOF
Once created, run the container and inspect the contents of the /build-context
directory which includes everything not excluded by the .dockerignore
file:
# run the default find command
docker container run --rm build-context
# or inspect it from a shell using
docker container run --rm -it build-context /bin/sh
You can then cleanup with:
docker image rm build-context
To get a detailed analysis of the build context you could use pwaller/docker-show-context.
$ go get -v -u github.com/pwaller/docker-show-context
$ cd ~/path/to/project/using/docker
$ docker-show-context
It outputs statistics about the build such as file sizes and upload times.
One thing that the other answers do not consider, is that this will potentially copy many gigabytes of data and be very slow, when all you want to do is find out which file(s) you need to exclude to reduce the image size.
So here is how you test your .dockerignore
without actually copying data:
$ rsync -avn . /dev/shm --exclude-from .dockerignore
What this will do, is try to sync your current directory with the empty in-memory folder /dev/shm
verbosely and dry-run (don't actually copy anything) the --exclude-from
option reads glob patterns in the same format as .gitignore
and .dockerignore
You will end up with a list of files copied and a summary with the total size at the end:
file.bogus
tests/
tests/conftest.py
tests/test_model.py
sent 1,954 bytes received 207 bytes 4,322.00 bytes/sec
total size is 209,916,337 speedup is 97,138.52 (DRY RUN)
Add it to .dockerignore
:
*.bogus
and test again:
tests/
tests/conftest.py
tests/test_model.py
sent 1,925 bytes received 204 bytes 4,258.00 bytes/sec
total size is 201,145 speedup is 94.48 (DRY RUN)
This is extremely fast and doesn't fill your disk.
Edit: There is one difference that I have found. For rsync the pattern *.bogus
matches all files with that name regardless of the directory. .dockerignore
however only matches *.bogus
in the current directory. To get the same behavior you need to prefix the pattern with the path glob characters **/*.bogus
This will still work with rsync.
h
flag to rsync (like rsync -avnh
) so that the output is more human readable.
Oct 18, 2022 at 14:10
To expand on BMitch's answer (which implements VonC's answer), you can do it with one simple command:
# no cache to ensure output
# progress plain in case you are using buildkit
docker build --no-cache --progress plain --file - . <<EOF
FROM busybox
COPY . /build-context
WORKDIR /build-context
RUN find .
EOF
Switching from CMD
to RUN
means you don't have to ever run the container as the build will output the result of the find .
.
One way is to make a small Dockerfile with an ADD or COPY directive in it.
Try to add or copy a file in a node_modules
folder: it is does not succeed, that would be because of the .dockerignore
.
ADD testfile .
, create file, add to .dockerignore, rebuild, it should error.
To expand on Lucas' answer which expands on BMitch's answer (which implements VonC's answer), if you are using buildx you will also need to set the type of progress output by adding --progress plain
and of course to not forget the --no-cache
:
docker build --no-cache --progress plain -f - . <<EOF
FROM busybox
COPY . /build_context
WORKDIR /build_context
RUN find .
EOF
To expand on Lucas's answer (which implements BMitch's and VonC's answer), you get an easier to understand tree structure with:
$ docker build --no-cache --progress plain --file - . <<EOF
FROM debian
RUN apt update && apt install tree -y
COPY . /build-context
RUN tree /build-context
EOF
which outputs:
#8 0.342 /build-context
#8 0.342 |-- dirA
#8 0.342 | |-- a
#8 0.342 | |-- b
#8 0.342 | | `-- lib
#8 0.342 | | |-- common
#8 0.342 | | | |-- __init__.py
#8 0.342 | | | |-- other.py
...
Great solutions,, to add to @Sekenre's answer.
As he mentioned the rsync
matches all files regardless of directory position on the *.bogus
which means if you want to negate the .dockerignore file and ignore everything except for what you want to allow, which results in putting *
at the top of the .dockerignore file, then the rsync method won't transfer any files at all,, even if you have a negated statement for a file just right below it, but a tool to check this would be amazing.
Even having a vscode extension for seeing the files that are ignored and allowed in the file browser would be amazing, just like the git and gitignore thing in vscode file explorer.