The .dockerignore file is similar to the .gitignore syntax. Here are some example rules:
# Ignore a file or directory in the context root named "modules"
modules
# Ignore any files or directories within the subdirectory named "modules"
# in the context root
modules/*
# Ignore any files or directories in the context root beginning with "modules"
modules*
# Ignore any files or directories one level down from the context root named
# "modules"
*/modules
# Ignore any files or directories at any level, including the context root,
# named modules
**/modules
# Ignore every file in the entire build context (see next rule for how this
# could be used)
*
# Re-include the file or directory named "src" that may have been previously
# excluded. Note that you cannot re-include files in subdirectories that have
# been previously excluded at a higher level
!src
Note that "build context" is the directory you pass at the end of your build command, typically a . to indicate the current directory. This directory is packaged from the docker client, excluding any files you have ignored with .dockerignore, and sent to the docker daemon to perform the build. Even when the daemon is on the same host as your client, the build only works from this context and not directly from the folders.
There is only a single .dockerignore for a build, and it must be in the root of the build context. It will not work if it is in your home directory (assuming you build from a subdirectory), and it will not work from a subdirectory of your build context.
To test what is in your current build context and verify your .dockerignore file is behaving correctly, you can copy/paste the following (this assumes you do not have an image named test-context, it will be overwritten and then deleted if you do):
# create an image that includes the entire build context
docker build -t test-context -f - . <<EOF
FROM busybox
COPY . /context
WORKDIR /context
CMD find .
EOF
# run the image which executes the find command
docker container run --rm test-context
# cleanup the built image
docker image rm test-context
Answer from BMitch on Stack OverflowVideos
The .dockerignore file is similar to the .gitignore syntax. Here are some example rules:
# Ignore a file or directory in the context root named "modules"
modules
# Ignore any files or directories within the subdirectory named "modules"
# in the context root
modules/*
# Ignore any files or directories in the context root beginning with "modules"
modules*
# Ignore any files or directories one level down from the context root named
# "modules"
*/modules
# Ignore any files or directories at any level, including the context root,
# named modules
**/modules
# Ignore every file in the entire build context (see next rule for how this
# could be used)
*
# Re-include the file or directory named "src" that may have been previously
# excluded. Note that you cannot re-include files in subdirectories that have
# been previously excluded at a higher level
!src
Note that "build context" is the directory you pass at the end of your build command, typically a . to indicate the current directory. This directory is packaged from the docker client, excluding any files you have ignored with .dockerignore, and sent to the docker daemon to perform the build. Even when the daemon is on the same host as your client, the build only works from this context and not directly from the folders.
There is only a single .dockerignore for a build, and it must be in the root of the build context. It will not work if it is in your home directory (assuming you build from a subdirectory), and it will not work from a subdirectory of your build context.
To test what is in your current build context and verify your .dockerignore file is behaving correctly, you can copy/paste the following (this assumes you do not have an image named test-context, it will be overwritten and then deleted if you do):
# create an image that includes the entire build context
docker build -t test-context -f - . <<EOF
FROM busybox
COPY . /context
WORKDIR /context
CMD find .
EOF
# run the image which executes the find command
docker container run --rm test-context
# cleanup the built image
docker image rm test-context
.dockerignore is to prevent files from being added to the initial build context that is sent to the docker daemon when you do docker build, it doesn't create a global rule for excluding files from being created in all images generated by a Dockerfile.
It's important to note that each RUN statement will generate a new image, with the parent of that image being the image generated by the Dockerfile statement above it. Try collapsing your RUN statements into a single one to reduce image size:
RUN librarian-puppet install &&\
puppet apply --modulepath=/modules -e "class { 'buildslave': jenkins_slave => true,}" &&\
librarian-puppet clean
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
Equivalent PowerShell:
@"
FROM busybox
WORKDIR /build-context
COPY . .
CMD find .
"@ | docker image build --no-cache -t build-context -f - .
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
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.
Edit 2: Warning for the unwary: This solution uses rsync to test the dockerignore file, there are some differences in the file globbing syntax of .dockerignore files and rsync ignore files, particularly around subdirectories. 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.
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.