Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions in .dockerignore

Tags:

docker

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.

like image 766
tidwall Avatar asked Oct 29 '14 17:10

tidwall


People also ask

What is .dockerignore used for?

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.

Where is Docker ignore?

dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.

Does Docker Compose use Dockerignore?

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.

What is dockerignore and how to use it?

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.

How to add comments to dockerfile and dockerignore?

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.

How does Docker ignore files and directories?

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.

Should I use dockerignore as a whitelist?

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?


2 Answers

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
like image 163
Mauricio Sánchez Avatar answered Sep 21 '22 14:09

Mauricio Sánchez


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

like image 36
Gergely Toth Avatar answered Sep 20 '22 14:09

Gergely Toth