Patterns like *.pyc are matched only at the beginning of the path, or for the files directly below the context directory, but not recursively. To make it work recursively the **/ syntax must be used:

# Ignore generated files
**/*.pyc

The reference at How to create a dockerignore file doesn't put that clear enough.

Finally, I understood that trick. For some reason, I wasn't able to find any mention of this and haven't found any example Dockerfile with such construct, so documenting it here. Was it trivial for everybody else?

Answer from Ilia Barahovsky on Stack Overflow
🌐
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 - This file allows you to specify patterns for files and directories that should not be copied into the Docker image, thereby reducing the size of the image and improving the build performance.
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
The .dockerignore file is a newline-separated list of patterns similar to the file globs of Unix shells. Leading and trailing slashes in ignore patterns are disregarded.
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
In a "glob" pattern, this matches everything that starts with a .. In a regular expression, this would match every character, essentially matching every file and directory. Since the .dockerignore file uses Unix style glob patterns, we can safely add .* and only dot-files will be excluded.
🌐
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
🌐
Peterspython
peterspython.com › en › blog › testing-the-docker-dockerignore-file-patterns
Testing the Docker .dockerignore file patterns | peterspython.com
September 22, 2020 - What this does is behaving like when you are building your Docker image but now it uses the busybox image. It outputs the list of directories and files that are included and quits. You can check this files and if your are not satisfied, you edit the .dockerignore file and run the script again.
🌐
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.
Find elsewhere
🌐
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 - You can use the ! pattern to include these files. # Ignore everything * # But don't ignore these files !.dockerignore !Dockerfile !src/
🌐
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 - If we start by saying that nothing will be sent to the Docker daemon when building an image, it will be almost impossible for us to accidentally include anything that’s secret or unecessary. The pattern that we need in the .dockerignore file to ignore everything is simply an asterisk:
🌐
GitHub
github.com › balena-io-modules › dockerignore
GitHub - balena-io-modules/dockerignore: dockerignore is a file filter library compatible with Docker and the node-ignore API · GitHub
Leading and trailing slashes (or backslashes on Windows) are removed from .dockerignore patterns, so '/a', 'a/' and '/a/' are all equivalent to 'a' in a .dockerignore file, and they all anchor to the "leftmost" directory when matching against ...
Author   balena-io-modules
🌐
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.
🌐
GitHub
github.com › moby › moby › issues › 30018
Exception patterns in .dockerignore do not support wildcard directories · Issue #30018 · moby/moby
January 10, 2017 - Description Exception patterns in .dockerignore do not support wildcard directories. I expect to be able to write exception rules like !*/*.txt. Steps to reproduce the issue: Assume the following f...
Author   taylortrimble
🌐
Support Tools
support.tools › effective-dockerignore-patterns
Effective .dockerignore Patterns: Optimizing Docker Build Context // Support Tools
June 15, 2025 - Learn how to effectively use .dockerignore files to reduce build context size, improve build performance, and maintain clean Docker images. Includes practical patterns and real-world examples.
🌐
Medium
medium.com › containers-101 › docker-tutorials-2e31a513de61
DOCKER TUTORIALS. Do not ignore .dockerignore (it’s… | by Codefresh | Container Hub | Medium
October 10, 2019 - 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.
🌐
CyberPanel
cyberpanel.net › blog › dockerignore-file
Mastering the .dockerignore file for Optimized Docker builds
July 17, 2025 - In practice, it works pretty much the same way as a .gitignore in a Git repository, using patterns that match unnecessary files to ensure you only copy over relevant code and assets into your Docker image, which can speed up your builds and ...
🌐
DeepWiki
deepwiki.com › tilt-dev › dockerignore › 4-pattern-syntax-guide
Pattern Syntax Guide | tilt-dev/dockerignore | DeepWiki
This document provides a comprehensive guide to the pattern syntax used by the dockerignore library for matching file paths. The library implements Docker-compatible pattern matching with enhancements
🌐
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 - It works much like .gitignore: • Each line defines a file or directory to ignore. ... • You can use ! to negate a pattern, which means including certain files even if their parent directory is excluded.
🌐
GitHub
github.com › codeskyblue › dockerignore › blob › master › README.md
dockerignore/README.md at master · codeskyblue/dockerignore
The Go lib interprets a .dockerignore like file as a newline-separated list of patterns similar to the file globs of Unix shells. For the purposes of matching, the root of the context is considered to be both the working and the root directory.
Author   codeskyblue
🌐
GitHub
github.com › containers › common › blob › main › docs › containerignore.5.md
common/docs/containerignore.5.md at main · containers/common
The CLI interprets the .containerignore or .dockerignore file as a newline-separated list of patterns similar to the file globs of Unix shells. For the purposes of matching, the root of the context is considered to be both the working and the ...
Author   containers