The .dockerignore file
Stop shipping junk into the build context and stop it from silently destroying your build cache.
Open this lesson in the learning hubKey points
docker build .sends that whole directory to the builder first. That bundle is the build context.- A stray
target/or.gitcan make the context hundreds of megabytes and slow every single build. - Worse, any changed file in the context can invalidate a
COPYlayer and throw away your cache. .dockerignoresits next to the Dockerfile and uses the same glob rules as.gitignore.- Ignoring
.envand*.pemalso stops secrets sneaking in through a lazyCOPY . ..
Example
# .dockerignore
.git
.gitignore
target/
build/
out/
*.iml
.idea/
.vscode/
**/node_modules
.env
*.pem
Dockerfile
compose.yaml
README.md
Two minutes of .dockerignore buys back minutes on every build.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Docker course, and every lesson in it is listed on the Docker contents page.