C.W.K.
Stream
Lesson 02 of 06 · published

Build Context & .dockerignore — Don't Send Your .git

~10 min · dockerfile, performance

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The dot at the end of docker build . is your build context

When you run docker build ., the entire current directory is sent to the Docker daemon. Every file. Every byte. Your .git, your node_modules, your .venv — all of it.

A 50KB Python project can have a 1GB build context if node_modules happens to be in there. The build will feel slow because before any layer is built, the entire context has to ship to dockerd over a unix socket.

The fix is .dockerignore

Same idea as .gitignore. Anything matching is excluded from the build context.

Code

A solid baseline .dockerignore·text
# Version control
.git
.gitignore

# Python caches
__pycache__
*.pyc
*.pyo
.pytest_cache
.mypy_cache
.venv
venv

# Node
node_modules
npm-debug.log*

# IDE / OS noise
.vscode
.idea
.DS_Store

# Local secrets — never bake into images
.env
.env.local
*.pem
*.key

# Docker itself
Dockerfile
docker-compose*.yml
.dockerignore

# Build artifacts you don't need at runtime
dist
build
*.log
See the difference·bash
# Without .dockerignore, watch the build context size:
docker build -t myapp:1.0 .
# Sending build context to Docker daemon: 1.42GB

# Add .dockerignore, rebuild:
docker build -t myapp:1.0 .
# Sending build context to Docker daemon: 47.3kB

External links

Exercise

Run docker build . on a real project of yours and note the 'Sending build context to Docker daemon' line. Add a thorough .dockerignore, rebuild, and note the new size. How much did you save? Commit the .dockerignore.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.