What the Build Context Is
docker build -t myapp .
The dot at the end is the build context: the folder whose files the builder may read. COPY and ADD in the Dockerfile can only take files from it, and a line such as COPY . . copies all of it into the image. A .dockerignore file in the root of the context removes paths from it before any instruction runs.
Why It Matters
- Secrets: an
.envfile, a private key, or a credentials file copied into a layer stays in the image, even if a laterRUN rmdeletes it. Anyone who can pull the image can extract it. - Image size:
.git, test data, and local build output can add hundreds of megabytes. - Speed and cache: fewer files to transfer, and a change in an ignored file no longer invalidates the
COPY . .layer. - Correct builds: a
node_modulesor.venvfolder from your own computer can hold binaries built for another system. Ignore it and let the Dockerfile install dependencies.
Pattern Syntax
| Line | Matches |
|---|---|
.git | .git at the root |
*.log | Log files at the root |
**/*.log | Log files at any depth |
tmp? | tmp plus one character |
!keep.log | Exception: keep it |
# note | A comment |
- Paths start at the context root. Unlike
.gitignore,*.logdoes not match in subfolders; write**/*.logfor that. - The last matching line wins, so an exception with
!must come after the rule it overrides. - A leading slash is ignored:
/distanddistmean the same.
A File for a Node.js Project
# version control and secrets
.git
.env*
!.env.example
*.pem
*.key
# dependencies and output, rebuilt in the image
node_modules
dist
coverage
npm-debug.log*
A File for a Python Project
.git
.env
.venv
**/__pycache__
**/*.pyc
.pytest_cache
.mypy_cache
A File for a PHP Project
.git
.env
vendor
node_modules
storage/logs
tests
Ignore vendor only when the Dockerfile runs composer install itself; otherwise the image ends up without its dependencies.
One Ignore File per Dockerfile
A project with several Dockerfiles can give each its own rules. With BuildKit, a file named after the Dockerfile plus .dockerignore, placed next to it, takes precedence over the one in the context root: build/prod.Dockerfile uses build/prod.Dockerfile.dockerignore. Compose builds read the same files for each service's context; see docker compose build.
Check What Reaches the Builder
This throwaway build copies the context into a small image and lists it, with the same ignore rules applied:
printf 'FROM busybox\nCOPY . /ctx\nRUN find /ctx -maxdepth 2\n' | \
docker build --no-cache --progress=plain -f - .
Read the list in the output. If .git, .env, or a key file appears, extend the ignore file and run it again. Remove the unnamed image it leaves behind with docker image prune.
What It Does Not Affect
- Bind mounts and volumes: files mounted at run time are not filtered; see Docker volumes.
- docker cp: copying files into a container does not read
.dockerignore; see docker cp. - Images already built: a secret that went into an older image is still there. Rebuild, delete the old images in every registry, and replace the secret itself.