If you need to ignore everything except some directories or files and also ignore some unnecessary files inside those allowed directories you can use the following .dockerignore file:
# Ignore everything
*
# Allow files and directories
!/file.txt
!/src
# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
Answer from Mauricio Sánchez on Stack OverflowIf you need to ignore everything except some directories or files and also ignore some unnecessary files inside those allowed directories you can use the following .dockerignore file:
# Ignore everything
*
# Allow files and directories
!/file.txt
!/src
# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
From the dockerfile reference:
Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.
So a line containing simply ** will ignore everything in the same directory as the Dockerfile.
As expected the exclamation can then be used to reference any files you do wish to send to the docker daemon.
docker - Dockerignore: Ignore everything except a folder - Stack Overflow
dockerignore file to ignore everything except two folders and their contents is not working
docker - Exceptions in .dockerignore - Stack Overflow
.dockerignore does not ignore files
What should I include in a .dockerignore?
Node_modules/, vendor/
Log files, *.log
Build artifacts, dist/, target/
Sensitive files, .env This will make your Docker image smaller and more secure.
If my .dockerignore file is not working?
Can I use regular expressions in a dockerignore?
Videos
Try using wildcards, and both for exclusions and inclusions in this case.
Also, .dockerignore supports ** wildcards for any number of directories depth like go does.
So try this:
src/modules/**
!src/modules/mall/**
Reference
If this doesn't work, could you elaborate in what doesnt work?
Do it like this
src/modules/*
!src/modules/mall
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
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
so i run a golang app using this simple Dockerfile shown below, and i've .dockerignore with Dockerfile written in it but when i exec into the container , the Dockerfile still show up in the workdir, why is that?
FROM golang:alpineWORKDIR /usr/src/appEXPOSE 8080COPY go.* .RUN go mod downloadCOPY . .CMD ["go","run","main.go"]
.dockerignore files try to follow Go's filepath.Match rules with the addition of a glob like ** matching any directories in a path. So they aren't exactly the same as glob patterns, or even trying to be. In fact, Docker implemented its own parser so it's not even using filepath.Match any more.
Using dir/.* and dir/*/.* works as expected. But dir/**/.* doesn't seem to work all the time.
This specific issue should be resolved, but I think I've seen the same behaviour in 1.13.1 in certain circumstances.
So with a .dockerignore of dir/**/.* the file dir/file.ext will be excluded but dir/subdir/file.ext will be included.
This is probably worth opening a new issue with Docker if you have a specific reproducible case on 1.13.
The issue has been fixed but the commit has not been merged into a released branch yet.