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 Overflow
🌐
Kevinpollet
kevinpollet.dev › posts › how-to-use-your-dockerignore-as-a-whitelist
How to use your .dockerignore as a whitelist - kevinpollet.dev
August 10, 2019 - As an example, the following .dockerignore instructs the Docker CLI to exclude everything excepted the JS files within the lib folder from the build context: # Ignore everything * # Allow files and folders with a pattern starting with ! !lib/**/*.js · With a whitelist it’s also possible to publish unwanted or sensitive files from a whitelisted folder but, in my opinion, this issue is minor.
Discussions

.dockerignore whitelist rules not respected
Expected behavior Skaffold calls docker build directly so that .dockerignore file is applied as expected, including whitelist rules like ** !/.gitmodules !/CMakeLists.txt !/etc/** !/CPlusPlusCode/*... More on github.com
🌐 github.com
1
July 30, 2019
Consider whitelisting on .dockerignore instead of blacklisting
It's much easier to whitelist all the files in the .dockerignore file than always update the blacklist. E.g.: https://github.com/cyon/docker-acmetool/blob/master/.dockerignore More on github.com
🌐 github.com
1
August 5, 2016
Exception patterns in .dockerignore do not support wildcard directories
Description Exception patterns in .dockerignore do not support wildcard directories. I expect to be able to write exception rules like !*/*.txt. Steps to reproduce the issue: Assume the following f... More on github.com
🌐 github.com
18
January 10, 2017
Definetly avoiding unwanted files to the .dockerignore will help in minimize the image Size
Dear KodeKloud, there is question in Mock exam 2 , which is Question :- Which of the below can help minimize the image size? the below option is marked wrong, Avoid sending unwanted files to the build context using .dockerignore How is this conclusion, for sure .dockeignore can help in reducing ... More on kodekloud.com
🌐 kodekloud.com
0
0
December 14, 2021
🌐
Moshen
moshen.net › posts › use-a-whitelist
Use A Whitelist for Your Ignore
June 11, 2018 - By default .dockerignore is a blacklist.
🌐
GitHub
github.com › GoogleContainerTools › skaffold › issues › 2576
.dockerignore whitelist rules not respected · Issue #2576 · GoogleContainerTools/skaffold
July 30, 2019 - Expected behavior Skaffold calls docker build directly so that .dockerignore file is applied as expected, including whitelist rules like ** !/.gitmodules !/CMakeLists.txt !/etc/** !/CPlusPlusCode/** !/submodules/** which is the recommend...
Author   AndiDog
🌐
OneUptime
oneuptime.com › home › blog › how to use .dockerignore to speed up docker builds
How to Use .dockerignore to Speed Up Docker Builds
January 16, 2026 - # Using rsync to simulate rsync ... --exclude-from=.dockerignore . /dev/null · # BAD: ignores everything, then your COPY fails * # GOOD: be specific node_modules .git *.log · # Ignores "build" anywhere in the tree build # Ignores only root-level "build" /build · # Matches "logs" directory AND file named "logs" logs # Matches only "logs" directory logs/ For very selective builds, ignore everything then whitelist what you ...
🌐
DevPress
devpress.csdn.net › cicd › 62ec0b1189d9027116a1009d.html
How to use your .dockerignore as a whitelist_docker_weixin_0010034-CI/CD
August 5, 2022 - As an example, the following .dockerignore instructs the Docker CLI to exclude everything excepted the JS files within the lib folder from the build context: # Ignore everything * # Allow files and folders with a pattern starting with ! !lib/**/*.js · With a whitelist it's also possible to publish unwanted or sensitive files from a whitelisted folder but, in my opinion, this issue is minor.
🌐
Markbirbeck
markbirbeck.com › 2018 › 12 › 07 › getting-control-of-your-dockerignore-files
Getting Control Of Your .dockerignore Files | Mark Birbeck’s Blog
December 7, 2018 - This post shows how to control .dockerignore by indicating which files we want to include rather than those we want to exclude.
Find elsewhere
🌐
GitHub
github.com › CachetHQ › Docker › issues › 116
Consider whitelisting on .dockerignore instead of blacklisting · Issue #116 · cachethq/Docker
August 5, 2016 - It's much easier to whitelist all the files in the .dockerignore file than always update the blacklist. E.g.: https://github.com/cyon/docker-acmetool/blob/master/.dockerignore
Author   dol
🌐
GitHub
github.com › moby › moby › issues › 30018
Exception patterns in .dockerignore do not support wildcard directories · Issue #30018 · moby/moby
January 10, 2017 - Description Exception patterns in .dockerignore do not support wildcard directories. I expect to be able to write exception rules like !*/*.txt. Steps to reproduce the issue: Assume the following files in the Docker context: $ find . -ty...
Author   taylortrimble
🌐
KodeKloud
kodekloud.com › docker
Definetly avoiding unwanted files to the .dockerignore will help in minimize the image Size - Docker - KodeKloud - DevOps Learning Community
December 14, 2021 - Dear KodeKloud, there is question in Mock exam 2 , which is Question :- Which of the below can help minimize the image size? the below option is marked wrong, Avoid sending unwanted files to the build context using .dockerignore How is this conclusion, for sure .dockeignore can help in reducing ...
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
You can use a .dockerignore file to exclude files or directories from the build context.
🌐
balenaForums
forums.balena.io › product support
Ignoring .dockerignore - Product support - balenaForums
March 24, 2018 - My .dockerignore is being ignored and it’s driving me crazy. My docker file has the following COPY statement WORKDIR /gg-dash COPY . /gg-dash …and after I do a RUN ls -l just to see what got copied. My .dockerignore file (in root next to Dockerfile) has the following statements: **/.DS_Store dist node_modules splash .resin-sync.yml local.sh README.md …but I’m still getting everything copied (including .git) - Am I doing something wrong here?
🌐
Readthedocs
nemesyst.readthedocs.io › en › latest › docker
7. Dockerisation — Nemesyst 2.0.6.r6.68bebbc documentation
Special care should be paid to ... to unexpected results. We personally recommend a whitelist strategy .dockerignore where you specify only what you would like to be copied in....
🌐
GitHub
github.com › coredns › coredns › pull › 3347
Use whitelist in .dockerignore by hloeffler · Pull Request #3347 · coredns/coredns
January 10, 2017 - 1. Why is this pull request needed and what does it do? Simplify the .dockerignore by using a white list instead of trying to blacklist everything else. 2. Which issues (if any) are related? None. ...
Author   coredns
🌐
Hacker News
news.ycombinator.com › item
You should know there was a pretty big bug fixed in .dockerignore in just the la... | Hacker News
March 25, 2016 - And it's possible a similar issue still exists in docker-compose but it's still open · Are you saying that docker would include files that should have been excluded by .dockerignore? I'd be interested to learn more. Thanks in advance
🌐
GitHub
github.com › dani-garcia › vaultwarden › pull › 4856
Switch to Whitelisting in .dockerignore by Timshel · Pull Request #4856 · dani-garcia/vaultwarden
Recently mistakenly added .git to the .dockerignore in another PR because I thought it might be responsible for unnecessary rebuild (it was not anyway). But with so many blacklisted element I think switching to a whitelist style is simpler.
Author   dani-garcia
🌐
GitHub
github.com › aws › aws-cdk › issues › 10921
[core] natively support .dockerignore · Issue #10921 · aws/aws-cdk
October 17, 2020 - A not-uncommon pattern to use with Docker is to turn the .dockerignore file into a whitelist by making the first rule one that ignores everything and then having subsequent negated patterns explicitly exempting specific files and directories.
Author   blaenk
🌐
Codefresh
codefresh.io › home › blog › do not ignore .dockerignore (it’s expensive and potentially dangerous)
Do not ignore .dockerignore (it's expensive and potentially dangerous) | Codefresh
March 13, 2025 - Now that you know why you need to control the docker build context, we can see how this is done. The .dockerignore file is similar to gitignore file, used by the git tool. similarly to .gitignore file, it allows you to specify a pattern for files and folders that should be ignored by the Docker client when generating a build context.
🌐
JetBrains
youtrack.jetbrains.com › issue › IJPL-75990 › Docker-build-does-not-respect-.dockerignore-entries-for-paths-with-symlinks
Docker build does not respect .dockerignore entries for ...
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong