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
🌐
OneUptime
oneuptime.com › home › blog › how to use docker ignore files
How to Use Docker Ignore Files
January 25, 2026 - # .dockerignore # Comments start with # # Blank lines are ignored # Ignore specific file secret.key # Ignore all files with extension *.log *.tmp # Ignore entire directories node_modules/ .git/ # Ignore files starting with pattern temp-* # Ignore files in any subdirectory **/*.md # But include specific file (exception) !README.md
Discussions

docker - Dockerignore: Ignore everything except a folder - Stack Overflow
I want to ignore all the folders in src/modules except mall folder when build images. I try like below, but it doesn't work. src/modules !src/modules/mall Any help will be appreciated. More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2018
dockerignore file to ignore everything except two folders and their contents is not working
Description of the issue .dockerignore file with ** wildcards is no longer (worked few days ago) white-listing directories with ! marks For instance: .dockerignore file ** !single-page-app/ !mysql/ should ignore everything on that folder... More on github.com
🌐 github.com
3
June 18, 2018
docker - Exceptions in .dockerignore - Stack Overflow
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 wo... More on stackoverflow.com
🌐 stackoverflow.com
.dockerignore does not ignore files
COPY go.* . and COPY . . are redundant. Do not use WORKDIR (see the latest runc issue with it). IMHO don’t use .dockerignore but simply have a roofs folder which you copy. Here is an example on how that looks like. More on reddit.com
🌐 r/docker
4
1
February 7, 2024
People also ask

What should I include in a .dockerignore?
In general, you should ignore:

Node_modules/, vendor/
Log files, *.log
Build artifacts, dist/, target/
Sensitive files, .env This will make your Docker image smaller and more secure.
🌐
cyberpanel.net
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
If my .dockerignore file is not working?
Review your pattern syntax ensure the file is at the root of your build context and look for any conflicting patterns. You can also try with --no-cache to avoid the cached issues.
🌐
cyberpanel.net
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
Can I use regular expressions in a dockerignore?
No, dockerignore doesn't support regex. It uses wildcard patterns such as * for multiple characters and ** for recursive directories.
🌐
cyberpanel.net
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
🌐
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 - # In .dockerignore # Exclude all ... the README.md file. So why don’t we use this feature to our advantage, and begin every .dockerignore file with a statement to exclude everything......
🌐
GitHub
github.com › docker › compose › issues › 6024
dockerignore file to ignore everything except two folders and their contents is not working · Issue #6024 · docker/compose
June 18, 2018 - .dockerignore file with ** wildcards is no longer (worked few days ago) white-listing directories with ! marks For instance: .dockerignore file ... should ignore everything on that folder, except the contents of those two folders.
Author   Frondor
🌐
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 - To achieve this, specify * as the first pattern, followed by one or more! exception patterns. Pretty easy! 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:
Find elsewhere
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - The dockerignore file has successfully excluded the node_modules/ directory and any .log files, reducing the build context to 2.5MB. ... Ignore everything except specific files.
🌐
Reddit
reddit.com › r/docker › .dockerignore does not ignore files
r/docker on Reddit: .dockerignore does not ignore files
February 7, 2024 -

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:alpine
WORKDIR /usr/src/app
EXPOSE 8080
COPY go.* .
RUN go mod download
COPY . .
CMD ["go","run","main.go"]

🌐
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 - The .dockerignore file is the tool, that can help you to define the Docker build context you really need. Using this file, you can specify ignore rules and exceptions from these rules for files and folder, that won’t be included in the build context and thus won’t be packed into an archive and uploaded to the Docker server.
🌐
Mrugesh Mohapatra
hn.mrugesh.dev › how-to-use-a-dockerignore-file-a-comprehensive-guide-with-examples
How to Use a .dockerignore File: A Comprehensive Guide with Examples • Mrugesh Mohapatra
May 25, 2023 - ! at the beginning of a pattern inverts the match. For example, !*important.txt would ignore all files except those ending with important.txt. Here’s an example .dockerignore file that uses these patterns:
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEA-200303 › dockerignore-not-respecting-exceptions-when-ignoring-all-files
dockerignore not respecting exceptions when ignoring all files
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Blogger
xuykyuu.blogspot.com › 2018 › 07 › dockerignore-ignore-everything-except.html
Dockerignore: Ignore everything except a file and the Dockerfile
October 9, 2018 - It's extremely important that the ** goes to the top of the file, otherwise the exclusions will be ignored. Docker uses the last rule that matches as the "final" rule, so if ** is last it'll match everything.
🌐
TutorialsPoint
tutorialspoint.com › how-to-ignore-files-in-docker
How to ignore files in Docker?
.dockerignore ??? file1.txt ??? file2.sh ??? file3.py ??? no_ex1 ??? No_ex2 5 directories, 10 files · Step 1 ? Create a Dockerfile and add the code. ... #for the base image we are using busybox FROM busybox:latest #set a working directory WORKDIR /my_context_directory #copy everything from the build context to the working directory of the #image COPY .
🌐
JetBrains
youtrack.jetbrains.com › issue › IJPL-73186 › dockerignore-not-respecting-exceptions-when-ignoring-all-files
dockerignore not respecting exceptions when ignoring all ...
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Medium
medium.com › @bounouh.fedi › mastering-the-dockerignore-file-boosting-docker-build-efficiency-398719f4a0e1
Mastering the .dockerignore File: Boosting Docker Build Efficiency | by Fedi Bounouh | Medium
November 9, 2024 - For example, let’s say you want to ignore all .env files except for .env.production. You can use negation for this: # Ignore all .env files *.env # But include .env.production !.env.production · This gives you the flexibility to fine-tune which files are excluded and which are included in your Docker build context. ... Using a .dockerignore file is one of the easiest ways to optimize your Docker builds.
🌐
Medium
medium.com › @LihauTan › took-me-hours-to-realise-why-docker-build-ignores-my-dockerignore-and-this-is-what-ive-learned-2f87c770ea9c
Took me hours to realise why docker build ignores my .dockerignore, and this is what I’ve learned | by Tan Li Hau | Medium
November 26, 2017 - Before the docker CLI sends the context to the docker daemon, it looks for a file named .dockerignore in the root directory of the context. If this file exists, the CLI modifies the context to exclude files and directories that match patterns in it.
🌐
LinkedIn
linkedin.com › pulse › securing-docker-builds-comprehensive-guide-usage-best-ilyas-ou-sbaa
Securing Docker Builds: A Comprehensive Guide to .dockerignore Usage and Best Practices
July 17, 2023 - If the context directory contains unnecessary files and directories, they will be included in the image, making it larger than necessary. By using .dockerignore, you can exclude files that are not required in the final image, resulting in smaller ...
🌐
GitHub
github.com › moby › moby › issues › 21433
dockerignore does not respect the "special wildcard **" · Issue #21433 · moby/moby
March 23, 2016 - As per documentation: 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 t...
Author   ebr