Skip to content
Log In Sign Up info@muvcloud.com

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 .env file, a private key, or a credentials file copied into a layer stays in the image, even if a later RUN rm deletes 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_modules or .venv folder from your own computer can hold binaries built for another system. Ignore it and let the Dockerfile install dependencies.

Pattern Syntax

LineMatches
.git.git at the root
*.logLog files at the root
**/*.logLog files at any depth
tmp?tmp plus one character
!keep.logException: keep it
# noteA comment
  • Paths start at the context root. Unlike .gitignore, *.log does not match in subfolders; write **/*.log for that.
  • The last matching line wins, so an exception with ! must come after the rule it overrides.
  • A leading slash is ignored: /dist and dist mean 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations