The build context and .dockerignore
What the build context is, why COPY . . can ship your .git and .env into an image, and how .dockerignore patterns, negation and ** really match.
The build context and .dockerignore
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The build context is the set of files a build can read. COPY and ADD can only reach files inside it, never ../ outside it.
- The last argument to docker build is the context: a directory, a Git URL, a tarball, or - for none at all.
- A .dockerignore at the root of the context removes matching files before the builder ever sees them, which speeds up builds and keeps secrets out of COPY . .
- Patterns use Go's filepath.Match rules plus **. Leading and trailing slashes are ignored, and a pattern without ** matches from the context root only.
- A line starting with ! re-includes files, and the last matching line wins, so the order of lines matters.
- A Dockerfile-specific ignore file named <name>.Dockerfile.dockerignore next to that Dockerfile takes precedence over the root .dockerignore.
What the builder can see
In docker build -t notes ., the final . is not decoration. It is the build context: the set of files the build is allowed to read. The builder may be a daemon on another machine or a BuildKit container on a CI runner, so it cannot rummage through your disk. COPY and ADD resolve paths inside the context and nowhere else. COPY ../secret . fails, by design.
That makes the context a security boundary as much as a convenience. A COPY . . copies the whole context into the image, including .git, a .env with real credentials, and the node_modules you built for macOS. An image is easy to share and hard to unshare, so anything that lands in a layer should be treated as published.
Kinds of context
The docs list several, all given as the last argument:
docker build . # local directory
docker build https://github.com/user/repo.git#main:docker # Git ref and subdirectory
docker build - < context.tar.gz # tarball on stdin
docker build - < Dockerfile # no context at all
docker build --build-context docs=./docs . # an extra named context
A Git context is shallow-cloned by the builder, so your local uncommitted changes are not in it. An empty context is handy for images that only download things. A named context from --build-context is referenced in the Dockerfile like a stage, with COPY --from=docs or RUN --mount=from=docs, which lets you pull in a sibling directory without widening the main context to the whole repository.
With BuildKit, the builder only fetches the files it needs and, between builds, only the ones that changed. That makes a big context cheaper than with the legacy builder, which sent everything up front. It is still worth trimming, especially for remote builders.
.dockerignore
A file named .dockerignore at the root of the context lists patterns to exclude before anything is sent. A typical one for a Node service:
# version control and local deps
.git
node_modules
# secrets, but keep the template
.env*
!.env.example
# docs, except the top-level README
**/*.md
!README.md
tests/fixtures
The rules, from the build context docs:
- Patterns follow Go’s
filepath.Match, afterfilepath.Clean.*matches within one path segment,?one character. **matches any number of directories, including none.**/*.mdcatches Markdown anywhere.*.mdonly catches it at the root.- Leading and trailing slashes are ignored, so
/foo/bar/andfoo/barmean the same thing. !re-includes. The last line that matches a file decides, so put exceptions after the exclusions they override.- Lines starting with
#are comments.
Checking it with the real matcher
I could not run docker build here, so I used the same matching code instead. Docker’s .dockerignore parsing and matching live in the moby/patternmatcher Go module. I wrote a 30-line Go program that reads the file above with its ignorefile package and walks a sample project, calling MatchesOrParentMatches on each path. Output, sorted:
send .dockerignore
SKIP .env
send .env.example
SKIP .git/HEAD
SKIP .git/objects/ab12
SKIP CHANGELOG.md
send Dockerfile
send README.md
SKIP docs/README.md
SKIP node_modules/express/index.js
send package-lock.json
send package.json
send src/api/server.js
SKIP tests/fixtures/big.sql
Two things worth noticing. !README.md rescued only the root README, while docs/README.md stays excluded, because the negation has no **. And .env.example survived because its ! line comes after .env*. Swap those two lines and it would be excluded.
One caution: the real build client also adds the Dockerfile and .dockerignore back for the builder’s own use, even if you exclude them. You just cannot COPY them into the image.
More than one Dockerfile
A repository with build.Dockerfile and test.Dockerfile can keep separate ignore files beside them, named build.Dockerfile.dockerignore and test.Dockerfile.dockerignore. The docs say a Dockerfile-specific file takes precedence over the root .dockerignore when both exist. The test image can then include fixtures the production image leaves out.
The build checks lesson shows a related check, CopyIgnoredFile, which warns when a COPY names a file your ignore rules excluded.
About the video
The embed is Shubham gour’s “Docker Build Context & .dockerignore Explained”, April 2026, 14 minutes, a hands-on walk-through of what docker build . sends to the builder and how a .dockerignore makes builds smaller and faster. The description gives the build context section as running from 0:36 to 2:16, with the rest on writing and testing an ignore file. It is from a small channel, so check it against the rules above. YouTube reports an English original audio track. Net Ninja’s shorter episode in deeper covers .dockerignore alone.
What was run: the matching output above is real, from a Go program using moby/patternmatcher v0.6.1, the module Docker uses for .dockerignore, on a sample directory tree on this Mac. Everything else is from the build context page and the Dockerfile reference in the docker/docs repository, checked 2026-11-28. No image was built.
Where this fits
The builder now has exactly the files it needs. Next, how it decides which of your steps it can skip: layer caching and instruction order.
Check yourself
Your Dockerfile says COPY ../shared/config.yaml /etc/app/. The build fails with not found. Why?
COPY can only read from the build context, which is the directory given to docker build. A path outside it does not exist for the builder. Move the file in, use a larger context, or pass a named context with --build-context.
Your .dockerignore contains *.md and then !README.md. Is docs/README.md sent to the builder?
No. *.md without ** only matches at the context root, so docs/README.md is not excluded by it in the first place. With **/*.md it would be excluded, and !README.md only re-includes the root README.
A .dockerignore has !.env.example on line 1 and .env* on line 2. The example file is missing from the build. What went wrong?
The last matching line wins. .env* on line 2 also matches .env.example, so it excluded the file again. Put the ! line after the exclusion it overrides.
Why does a .dockerignore matter even when your Dockerfile copies only specific files?
The context is still collected and offered to the builder, so a huge node_modules or .git slows every build, and with a remote builder it is transferred over the network. Excluding it also stops a later COPY . . from pulling in secrets.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.