The .dockerignore file is similar to the .gitignore syntax. Here are some example rules:

# Ignore a file or directory in the context root named "modules"
modules

# Ignore any files or directories within the subdirectory named "modules" 
# in the context root
modules/*

# Ignore any files or directories in the context root beginning with "modules"
modules*

# Ignore any files or directories one level down from the context root named
# "modules"
*/modules

# Ignore any files or directories at any level, including the context root, 
# named modules
**/modules

# Ignore every file in the entire build context (see next rule for how this 
# could be used)
*

# Re-include the file or directory named "src" that may have been previously
# excluded. Note that you cannot re-include files in subdirectories that have 
# been previously excluded at a higher level
!src

Note that "build context" is the directory you pass at the end of your build command, typically a . to indicate the current directory. This directory is packaged from the docker client, excluding any files you have ignored with .dockerignore, and sent to the docker daemon to perform the build. Even when the daemon is on the same host as your client, the build only works from this context and not directly from the folders.

There is only a single .dockerignore for a build, and it must be in the root of the build context. It will not work if it is in your home directory (assuming you build from a subdirectory), and it will not work from a subdirectory of your build context.

To test what is in your current build context and verify your .dockerignore file is behaving correctly, you can copy/paste the following (this assumes you do not have an image named test-context, it will be overwritten and then deleted if you do):

# create an image that includes the entire build context
docker build -t test-context -f - . <<EOF
FROM busybox
COPY . /context
WORKDIR /context
CMD find .
EOF

# run the image which executes the find command
docker container run --rm test-context

# cleanup the built image
docker image rm test-context
Answer from BMitch on Stack Overflow
🌐
GitHub
github.com › starlightromero › dockerignore-example
GitHub - starlightromero/dockerignore-example: How to use .dockerignore to the fullest potential · GitHub
How to use .dockerignore to the fullest potential. Contribute to starlightromero/dockerignore-example development by creating an account on GitHub.
Starred by 2 users
Forked by 4 users
Languages   Go 53.3% | Makefile 25.1% | Dockerfile 20.7% | Shell 0.9%
Top answer
1 of 8
110

The .dockerignore file is similar to the .gitignore syntax. Here are some example rules:

# Ignore a file or directory in the context root named "modules"
modules

# Ignore any files or directories within the subdirectory named "modules" 
# in the context root
modules/*

# Ignore any files or directories in the context root beginning with "modules"
modules*

# Ignore any files or directories one level down from the context root named
# "modules"
*/modules

# Ignore any files or directories at any level, including the context root, 
# named modules
**/modules

# Ignore every file in the entire build context (see next rule for how this 
# could be used)
*

# Re-include the file or directory named "src" that may have been previously
# excluded. Note that you cannot re-include files in subdirectories that have 
# been previously excluded at a higher level
!src

Note that "build context" is the directory you pass at the end of your build command, typically a . to indicate the current directory. This directory is packaged from the docker client, excluding any files you have ignored with .dockerignore, and sent to the docker daemon to perform the build. Even when the daemon is on the same host as your client, the build only works from this context and not directly from the folders.

There is only a single .dockerignore for a build, and it must be in the root of the build context. It will not work if it is in your home directory (assuming you build from a subdirectory), and it will not work from a subdirectory of your build context.

To test what is in your current build context and verify your .dockerignore file is behaving correctly, you can copy/paste the following (this assumes you do not have an image named test-context, it will be overwritten and then deleted if you do):

# create an image that includes the entire build context
docker build -t test-context -f - . <<EOF
FROM busybox
COPY . /context
WORKDIR /context
CMD find .
EOF

# run the image which executes the find command
docker container run --rm test-context

# cleanup the built image
docker image rm test-context
2 of 8
57

.dockerignore is to prevent files from being added to the initial build context that is sent to the docker daemon when you do docker build, it doesn't create a global rule for excluding files from being created in all images generated by a Dockerfile.

It's important to note that each RUN statement will generate a new image, with the parent of that image being the image generated by the Dockerfile statement above it. Try collapsing your RUN statements into a single one to reduce image size:

RUN librarian-puppet install &&\
 puppet apply --modulepath=/modules -e "class { 'buildslave': jenkins_slave => true,}" &&\
 librarian-puppet clean
🌐
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.
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
Beyond Go's filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go excludes all files that end with .go found anywhere in the build context. You can use the .dockerignore file to exclude the Dockerfile and .dockerignore files.
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
The above is a simple example of using the .dockerignore file. At this point, we could simply add a similar entry for each file and directory we wish to omit and we could have a smaller resulting image.
🌐
GeeksforGeeks
geeksforgeeks.org › devops › how-to-use-a-dockerignore-file
How to Use a .dockerignore File? - GeeksforGeeks
April 8, 2024 - 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. You can specify the list of files and directories inside the .dockerignore file. Let's look at an example of ...
🌐
TestDriven.io
testdriven.io › tips › 6850ab62-9323-4dca-8ddf-8db1d479accc
Tips and Tricks - Use a .dockerignore File | TestDriven.io
A properly structured .dockerignore file can help: Decrease the size of the Docker image · Speed up the build process · Prevent unnecessary cache invalidation · Prevent leaking secrets · Example: **/.git **/.gitignore **/.vscode **/coverage **/.env **/.aws **/.ssh Dockerfile README.md docker-compose.yml **/.DS_Store **/venv **/env ·
🌐
GitHub
gist.github.com › KernelA › 04b4d7691f28e264f72e76cfd724d448
.dockerignore example for Python projects · GitHub
.dockerignore example for Python projects. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Mrugesh Mohapatra
mrugesh.dev › blog › how-to-use-a-dockerignore-file-a-comprehensive-guide-with-examples
How to Use a .dockerignore File: A Comprehensive Guide with Examples • Mrugesh Mohapatra
Here are some of the key rules to keep in mind when writing patterns in a .dockerignore file: * matches any sequence of characters but does not match directory separators. For example, *.log would match any file ending with .log, but would not match /logs/my.log.
🌐
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 - Securing development environments: ... in Docker images. For example, you can exclude configuration files with passwords, API keys, or other secrets that should not be distributed ......
🌐
GitHub
github.com › kubeflow › examples › blob › master › code_search › .dockerignore
examples/code_search/.dockerignore at master · kubeflow/examples
A repository to host extended examples and tutorials - examples/code_search/.dockerignore at master · kubeflow/examples
Author   kubeflow
🌐
TutorialsPoint
tutorialspoint.com › using-dockerignore-file
Using .Dockerignore file
October 1, 2020 - You might have some important source ... into the final docker image. For example, exposing your .git folder inside your docker image. Thus, it’s always recommended to ignore such files and folders by mentioning them into .dockerignore file....
🌐
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 - There are multiple Docker images currently available on DockerHub, that expose application source code, passwords, keys and credentials (for example Twitter Vine). Copying the .git folder in a Docker image by mistake is especially damaging. Tip: Always mention your .git folder in your .dockerignore file
🌐
Medium
medium.com › nerd-for-tech › bigger-dockerignore-smaller-docker-images-49fa22e51c7
Bigger .dockerignore, Smaller Docker Images | by Starlight Romero | Nerd For Tech | Medium
July 26, 2021 - By making a bind mount .:/app, we are completely overriding the .dockerignore. Since the bind mount it attached after the image is build, even files that were ignored are now mounted to the container. A better way to use bind mounts is to map specific files or folders. The following is an example of a development docker-compose.dev.yml where we set a bind mount only on the main.go file and map it to the file within the container at the WORKDIR location /app/main.go.
🌐
TechRepublic
techrepublic.com › home › what are .dockerignore files, and why you should use them?
What are .dockerignore files, and why you should use them? - TechRepublic
November 7, 2022 - 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. But more importantly, the .dockerignore can help you reduce the size of the image and dramatically speed up the build process.
🌐
GitHub
gist.github.com › yizeng › eeeb48d6823801061791cc5581f7e1fc
An example .dockerignore file for Rails · GitHub
An example .dockerignore file for Rails. GitHub Gist: instantly share code, notes, and snippets.
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - Dot files are usually hidden in Unix-based systems where files whose names start with a dot are not shown. Make sure that you are excluding all the dot files in your .dockerignore file: ... In this example, the .dockerignore file failed to exclude hidden files due to incorrect configuration, leading to a larger build context.
🌐
Cratecode
cratecode.com › info › docker ignore usage
Docker Ignore Usage | Cratecode
May 13, 2023 - To create a .dockerignore file, simply create a new file in the root directory of your project, where your Dockerfile is located, and name it .dockerignore. Then, you can start adding patterns to exclude specific files and directories from your build context. For example, let's say you have a Node.js project, and you want to exclude the node_modules directory and any .log files from your Docker build context.
🌐
Medium
medium.com › @vikramgyawali57 › understanding-the-dockerignore-file-why-its-essential-and-how-to-use-it-1df9b716d9e5
Understanding the .dockerignore File: Why It's Essential and How to Use It | by Bikram Gyawali | Medium
November 26, 2024 - # Ignore node_modules except for production dependencies node_modules !node_modules/.prisma # Example: Keep this if using Prisma for production builds # Logs logs *.log # Environment files .env .env.* # Build outputs build dist # OS generated files .DS_Store Thumbs.db # Editor directories and files .idea .vscode # Temporary and cache files tmp .cache *.bak · Start Small Begin by including only essential ignores and expand as needed. A minimal .dockerignore is better than none at all.