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
Dockerignore Example serves to show how .dockerignore files work and the benefits of using them, along with various examples.
Starred by 2 users
Forked by 4 users
Languages   Go 53.3% | Makefile 25.1% | Dockerfile 20.7% | Shell 0.9%
🌐
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.
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
🌐
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 - Like the .gitignore file, which tells Git which files to ignore, the .dockerignore file tells Docker which files to exclude from the build context.
🌐
GeeksforGeeks
geeksforgeeks.org › devops › how-to-use-a-dockerignore-file
How to Use a .dockerignore File? - GeeksforGeeks
April 8, 2024 - sudo docker run -it sample-image bash ls · Running the Container · 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 - 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.
🌐
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 ...
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
In this article, we covered how to leverage the .dockerignore file to exclude unnecessary files and directories from the container build. As we found out, the usage of the .dockerignore file is very simple. Do you have any .dockerignore tips or tricks?
Find elsewhere
🌐
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 - If a file isn’t needed for your app to run, put it in the .dockerignore. Now that this file is in the .dockerignore, it won’t be included in the Docker image, reducing the size of your image. The Docker CLI looks for .dockerignore in the root of your app.
🌐
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
🌐
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.
🌐
GitHub
gist.github.com › yizeng › eeeb48d6823801061791cc5581f7e1fc
An example .dockerignore file for Rails · GitHub
An example .dockerignore file for Rails · Raw · .dockerignore · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.
🌐
GitHub
gist.github.com › neckhair › ace5d1679dd896b71403fda4bc217b9e
Sample dockerignore for a Rails app · GitHub
September 21, 2019 - Sample dockerignore for a Rails app. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
github.com › gothinkster › aspnetcore-realworld-example-app › blob › master › .dockerignore
aspnetcore-realworld-example-app/.dockerignore at master · gothinkster/aspnetcore-realworld-example-app
ASP.NET Core backend implementation for RealWorld. Contribute to gothinkster/aspnetcore-realworld-example-app development by creating an account on GitHub.
Author   gothinkster
🌐
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.
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - Like Git’s .gitignore – the .dockerignore file is probably the most effective way to make your Docker-based build faster. Like Dockerfile, it is used for the most important thing: excluding unnecessary files from the build context. The key effect of this continues to grow building times and ensures lightweight and secure Docker images.
Top answer
1 of 11
190

To expand on VonC's suggestion, here's a sample build command you can use to create an image with the current folder's build context:

docker image build --no-cache -t build-context -f - . <<EOF
FROM busybox
WORKDIR /build-context
COPY . .
CMD find .
EOF

Equivalent PowerShell:

@"
FROM busybox
WORKDIR /build-context
COPY . .
CMD find .
"@ | docker image build --no-cache -t build-context -f - .

Once created, run the container and inspect the contents of the /build-context directory which includes everything not excluded by the .dockerignore file:

# run the default find command
docker container run --rm build-context

# or inspect it from a shell using
docker container run --rm -it build-context /bin/sh

You can then cleanup with:

docker image rm build-context
2 of 11
65

One thing that the other answers do not consider, is that this will potentially copy many gigabytes of data and be very slow, when all you want to do is find out which file(s) you need to exclude to reduce the image size.

Edit 2: Warning for the unwary: This solution uses rsync to test the dockerignore file, there are some differences in the file globbing syntax of .dockerignore files and rsync ignore files, particularly around subdirectories. For rsync the pattern *.bogus matches all files with that name regardless of the directory. .dockerignore however only matches *.bogus in the current directory. To get the same behavior you need to prefix the pattern with the path glob characters **/*.bogus This will still work with rsync.

So here is how you test your .dockerignore without actually copying data:

$ rsync -avn . /dev/shm --exclude-from .dockerignore

What this will do, is try to sync your current directory with the empty in-memory folder /dev/shm verbosely and dry-run (don't actually copy anything) the --exclude-from option reads glob patterns in the same format as .gitignore and .dockerignore

You will end up with a list of files copied and a summary with the total size at the end:

file.bogus
tests/
tests/conftest.py
tests/test_model.py

sent 1,954 bytes  received 207 bytes  4,322.00 bytes/sec
total size is 209,916,337  speedup is 97,138.52 (DRY RUN)

Add it to .dockerignore:

*.bogus

and test again:

tests/
tests/conftest.py
tests/test_model.py

sent 1,925 bytes  received 204 bytes  4,258.00 bytes/sec
total size is 201,145  speedup is 94.48 (DRY RUN)

This is extremely fast and doesn't fill your disk.