Skip to content

Instantly share code, notes, and snippets.

@sgdan
Created May 11, 2019 23:58
Show Gist options
  • Save sgdan/1cc6a8becc023d3b9b2c7b9dd379f102 to your computer and use it in GitHub Desktop.
Save sgdan/1cc6a8becc023d3b9b2c7b9dd379f102 to your computer and use it in GitHub Desktop.
Test the ".dockerignore" file to ensure the build context doesn't contain unwanted files
#!/bin/sh
# Based on BMitch's answer from:
# https://stackoverflow.com/questions/38946683/how-to-test-dockerignore-file
# Note: will create and delete temporary file "Dockerfile.build-context"
# 1. Copy to project folder where image is being built
# 2. Run script
# 3. You should see list of files in build context
# 4. If unwanted files in context, adjust .dockerignore file and go back to step 2
cat <<EOF > Dockerfile.build-context
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .
EOF
docker build -f Dockerfile.build-context -t build-context .
docker run --rm -it build-context
rm Dockerfile.build-context
@xandross389
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment