How does .dockerignore
handle exceptions?
For example, I would like to ignore everything from the src/
directory except for src/web/public
.
I've tried...
.git
src
!src/web/public
Seems to not work.
docker build .
shows Sending build context to Docker daemon 20.63 MB
either way.
The . dockerignore file is very similar to the . gitignore file in that it allows you to specify a list of files or directories that Docker is to ignore during the build process. This can come in really handy in certain instances.
dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.
Why is this? Yes, the Dockerfile , docker-compose. yml , and . dockerignore are all used to build the image and spin up a container, however, that's where the purpose of these files stop.
Due to all these reasons, you might want to exclude some files and folders from your Docker Build Context. Now, similar to a .gitignore file that is commonly used when you build Git repositories, a .dockerignore file is used to ignore files and folders when you try to build a Docker Image.
When adding above file to the same level that of Dockerfile, the docker CLI before sending the context to docker daemon will look for above file .dockerignore and it will ignore the file (s) or directory that match the pattern (s) specified in the above file. You can add comments to .dockerignore same way as in Dockerfile.
Before sending the files over to the docker server, the docker client (Command Line Interface) starts searching for a file called .dockerignore in the Build Context’s root directory. If it finds such a file, the CLI (Client) will modify the build context to exclude those files and directories which are mentioned inside the .dockerignore file.
Using your .dockerignore as a whitelist can be an elegant option to avoid these mistakes and to reduce the effort to keep things up to date. What’s the .dockerignore for?
Using my response to another question https://stackoverflow.com/a/51429852/2419069 you can use the following .dockerignore
file:
# Ignore everything
**
# Allow /src/web/public directory
!/src/web/public/**
# You can also ignore unnecessary files inside the /src/web/public to make your image even smaller.
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
Just wanted to add in case others run into this question, that
from docker 1.7.0 exclusions are supported in the .dockerignore file. So the example !src/web/public
in the question actually works
https://docs.docker.com/engine/reference/builder/#dockerignore-file
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