The .dockerignore is used to control what files are included in the build context. This impacts the COPY and ADD commands in the Dockerfile, and ultimately the resulting image. When you run that image with a volume mount, e.g.:

        {
            "Type": "bind",
            "Source": "/home/adam/Desktop/Dev/ec2-data-analysis/grimlock",
            "Destination": "/opt/project",
            "Mode": "rw",
            "RW": true,
            "Propagation": "rprivate"
        },

That volume mount overrides the contents of the image for that container. All access to the path will go to your desktop directory rather than the image contents, and Linux bind mounts do not have a concept of the .dockerignore file.

When you run this image without the volume mount, you should see different behavior.


For anyone coming across this question in the future, the .dockerignore needs to be in the root of your build context. The build context is the directory you pass at the end of the build command, often a . or the current directory. If you enable BuildKit, it will first check for a Dockerfile.dockerignore where your Dockerfile path/name could be changed. And to test your .dockerignore, see this answer.

Answer from BMitch on Stack Overflow
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
One of the most common locations to store a dockerfile is the top level of the application's code repository. My personal projects are no exception. For this article, we will go ahead and add a .dockerignore file to one of my personal open-source projects that leverages Docker.
Discussions

docker - What are the files that the .dockerignore works on? - Stack Overflow
I don't really understand how .dockerignore works. Is it intended to be used like the following: ... Then I put this .dockerignore into the container. After that I run and enter the container. I create a new file named test.md and commit this container to the new image. More on stackoverflow.com
🌐 stackoverflow.com
docker - Location for common .dockerignore file - Stack Overflow
I'd like a common .dockerignore file, "outside" a build context (which I assume is the directory in which the Dockerfile is located). So instead of this: project/ client/ src/ ... More on stackoverflow.com
🌐 stackoverflow.com
puppet - How do you add items to .dockerignore? - Stack Overflow
I believe the reason that the image grows so large, is because librarian-puppet clones a puppet module to /modules which breaks the build cache · I've tried the following .dockerignore files with no luck. More on stackoverflow.com
🌐 stackoverflow.com
[Feature Request] Look for .dockerignore in same location as -f flag
Then the paths in the .dockerignore file would be relative, so ./ would be /AddProject/API/ but it would always preserve the root, so / would mean root of the build context, where docker build . was executed. More on github.com
🌐 github.com
3
August 26, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › devops › how-to-use-a-dockerignore-file
How to Use a .dockerignore File? - GeeksforGeeks
April 8, 2024 - You will find that the Container only contains the dockerfile and not the "ignore-this" folder. Not that it also does not contain the .dockerignore file. It is true that you can also mention the dockerfile inside the .dockerignore file and exclude it from the Docker build context.
🌐
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 - This includes not only source code but also unnecessary files such as logs, temporary files, and build artifacts. The .dockerignore file is a simple text file you place in your project root to exclude files or directories from the build context.
🌐
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.
🌐
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 - Creating a .dockerignore file is as simple as creating any other file. In the root directory of your application, where your Dockerfile resides, create a file named .dockerignore.
Find elsewhere
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
For historical reasons, the pattern . is ignored. 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.
🌐
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 - Of course, if we add a .dockerignore file to our source directory then that will be available to the Docker daemon, so we now need to ignore that as well: ... Although this is a nice simple solution, there’s still nothing to say that we might not accidentally copy something into the image that we didn’t want to.
🌐
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.
🌐
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 - If your working directory contains files that are frequently updated (logs, test results, git history, temporary cache files and similar), you are going to regenerate this layer for every docker build run. If you don’t manage the build context correctly, your builds will be very slow as cache cannot be used correctly. 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.
🌐
GoLinuxCloud
golinuxcloud.com › home › docker › unlock the power of .dockerignore for efficient docker builds
Unlock the Power of .dockerignore for Efficient Docker Builds | GoLinuxCloud
October 20, 2023 - Ignoring this directory can help to ensure that your Docker image does not contain files related to testing that are not necessary for running your application, contributing to a smaller and more manageable image size. Using relative paths is generally the preferred approach when specifying paths in a .dockerignore file.
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - The dockerfile ignore refers to controlling what files from your project should be considered when the Dockerfile is processed. This is achieved by using both .dockerignore and COPY/ADD commands effectively in your Dockerfile.
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
🌐
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 - Introduction .dockerignore is a text file used by Docker to exclude files and directories from the context that is sent to the Docker daemon during the build process.
🌐
GitHub
github.com › docker › cli › issues › 1308
[Feature Request] Look for .dockerignore in same location as -f flag · Issue #1308 · docker/cli
August 26, 2018 - / |-- .paket |-- AddProject | |-- API | | |-- Dockerfile | | `-- .dockerignore | | `-- foo.txt | `-- Web | `-- foo.txt |-- SkipProject_1 | `-- foo.txt |-- SkipProject_2 | `-- foo.txt
Author   gaui