The .dockerignore file

Docker · lesson 5 of 31 · 2 min read

Stop shipping junk into the build context and stop it from silently destroying your build cache.

Open this lesson in the learning hub

Key points

  • docker build . sends that whole directory to the builder first. That bundle is the build context.
  • A stray target/ or .git can make the context hundreds of megabytes and slow every single build.
  • Worse, any changed file in the context can invalidate a COPY layer and throw away your cache.
  • .dockerignore sits next to the Dockerfile and uses the same glob rules as .gitignore.
  • Ignoring .env and *.pem also stops secrets sneaking in through a lazy COPY . ..

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.