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
🌐
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 - If you’re familiar with .gitignore, you’ll find this concept familiar: it’s a file that helps exclude unnecessary files and directories from being included in the Docker build context, saving time, space, and headaches. In this article, we will dive deep into what a .dockerignore file is, why it’s important, and how to use it effectively with real-life examples.
People also ask

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
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
Does the dockerignore file contain case sensitivity?
Yes, the .dockerignore file and dockerignore are case-sensitive. Therefore, make sure to match exactly the case of file and directory names.
🌐
cyberpanel.net
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
🌐
PyPI
pypi.org › project › generate-dockerignore-from-gitignore
generate-dockerignore-from-gitignore · PyPI
This also can promote reproducible builds by ensuring that the only files your Docker context has access to are those files that are tracked by your git repository. The following differences between .dockerignore vs .gitignore processing are handled by this tool: The code's handling of these behavioral differences is tested using actual git and Docker calls, followed by validation that the resultant git source trees are equal to the Docker build contexts. ... usage: generate-dockerignore [-h] [-C DOCKER_ROOT] [-v] [-o OUTPUT] [PATH ...] .dockerignore file generator based on .gitignore file(s) positional arguments: PATH gitignore file(s), or directories under git source control (which will be searched for **/.gitignore).
      » pip install generate-dockerignore-from-gitignore
    
Published   Aug 14, 2023
Version   0.1.3
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
The file itself is a simple text file that contains a list of glob patterns for files and directories to exclude from the final build image. By leveraging the .dockerignore file, we can exclude files and directories we do not need within our final image.
🌐
Configs
configs.sh › dockerignore
.dockerignore Generator – configs.sh
Generate optimized .dockerignore files for faster Docker builds. Supports Node.js, Python, Go, Rust, Java with best practice exclusions.
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
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - If you do not know where to begin, there are .dockerignore generators that you can use online. For example, Dockerignore Generator is a customizable option to create a .dockerignore for your project’s specific setup. Here are a few examples of practical .dockerignore files for different types of projects:
Find elsewhere
🌐
npm
npmjs.com › package › dockerfile-generator
dockerfile-generator - npm
December 21, 2025 - const generator = require('dockerfile-generator') const result = await generator.generateDockerFile(inputJson) // Result is a generated Dockerfile. const generateResult = generator.generateDockerFileFromArray(inputArray) // Result is a generated Dockerfile. const convertedJSON = generator.convertToJSON(inputDockerFile) // Result is a generated JSON from Dockerfile. const genereratedIgnore = await generator.generateIgnoreFile(ignoredElementsArray) // generatedIgnore is a generated dockerignore file ·
      » npm install dockerfile-generator
    
Published   Dec 21, 2025
Version   5.0.4
Author   Tibor Udvari
🌐
Datacraftsman
datacraftsman.com.au › tools › Dockerignore Generator
Dockerignore Generator - Data Craftsman
June 6, 2025 - Generate .dockerignore files for your Docker projects. Choose from common patterns for different project types (Node.js, Python, Java, etc.) or add custom ignore patterns.
🌐
GitHub
github.com › GoogleCloudPlatform › aspnet-docker › issues › 56
Generate an appropriate .dockerignore file next to the Dockerfile to ignore app.yaml and other files. · Issue #56 · GoogleCloudPlatform/aspnet-docker
June 23, 2017 - We should generate a .dockerignore file to ignore all unnecessary files from the final Docker image. This .dockerignore file should include: Dockerfile app.yaml .git .hg ... and other version contr...
Author   ivannaranjo
🌐
skelebot
carsdotcom.github.io › skelebot › api › dockerignore.html
skelebot | Machine Learning Project Development Tool
This function will utilize the ignores list from the Config object to construct a fully functioning .dockerignore file for the project in order to omit specific files and folders and reduce the amount of data inside the build context during the Docker build process.
🌐
GitHub
github.com › orisano › dignore
GitHub - orisano/dignore: dockerignore generator for monorepo
dockerignore generator for monorepo. go install github.com/orisano/dignore@latest ·
Starred by 9 users
Forked by 2 users
Languages   Go 87.8% | Dockerfile 12.2% | Go 87.8% | Dockerfile 12.2%
🌐
PyPI
pypi.org › project › dockerignore-generate
dockerignore-generate
August 14, 2018 - JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
GeeksforGeeks
geeksforgeeks.org › 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.
🌐
npm
npmjs.com › package › gitignore-to-dockerignore
gitignore-to-dockerignore - npm
December 6, 2023 - Generate an equivalent .dockerignore file from existing .gitignore files.
      » npm install gitignore-to-dockerignore
    
Published   Dec 06, 2023
Version   3.0.0
🌐
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 - It’s pretty common to use the COPY instruction to copy files and folders within a Docker build context. However, each statement inside your Dockerfile would result in building a new intermediate image layer. Because of this, when you make changes to the Dockerfile over and over, this can lead to multiple cache invalidations which can waste precious resources. With that said, let’s find out how to use the .dockerignore file and the types of items you might want to include in them.
🌐
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 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.