Docker Build Contexts: What .dockerignore Excludes and COPY Does Not
CONTAINER BUILD INPUTS
The context decides which files a build can access. A copy instruction decides what one step puts into a stage. Review those boundaries separately.
By Hossam
Start with the input boundary, not the final image
A Dockerfile that copies only application files can still have a wider build context than its author intended. The context is the set of files available to the build, while COPY describes one operation using a source. A selective copy is useful, but it is not the same control as excluding an input from the context.
Docker documents .dockerignore as a way to remove matching files and directories from the context before it is sent to the builder. That makes context selection and ignore rules worth reviewing even when the final runtime image appears small. A final-image inspection answers a different question from a build-input review.
This guide analyzes Docker documentation. Its example repository layout and review steps are illustrative, not captured build output. No Docker daemon, remote builder, context-transfer capture, image scan or performance benchmark was run for this article. Linux-style paths are used for discussion.
Documentation: Build context and .dockerignore, Dockerfile COPY reference.
The context path is not the Dockerfile directory
The positional argument to docker build or docker buildx build selects the context. For a local directory, files beneath that directory form the filesystem input. Dockerfile source paths for ordinary COPY are interpreted relative to the context root, not relative to the directory containing the Dockerfile.
Consider a fictional repository with application code under app, a Dockerfile under docker, and private operational notes beside those directories. Choosing the repository root as context makes a different input set from choosing app. Moving the Dockerfile into docker does not by itself narrow the context to that directory.
Before changing an ignore pattern, record the actual CI invocation, working directory, context argument and selected Dockerfile. A locally successful command is not evidence that a CI job uses the same root. A path change can alter both which files are eligible and where COPY expects to find them.
Documentation: Build context and .dockerignore, Dockerfile COPY reference.
Find the ignore file that actually applies
Docker looks for .dockerignore at the root of the context. A file with that name placed elsewhere should not be assumed to govern the whole build. Review its location against the selected context rather than against the visual root of a repository browser.
Docker also supports a Dockerfile-specific ignore file placed beside that Dockerfile, with the Dockerfile name prefixed to .dockerignore. Its documentation gives build.Dockerfile.dockerignore as an example. When both that file and the root .dockerignore exist, the Dockerfile-specific file takes precedence.
That precedence matters for separate production, test and lint builds. A strict root file is not proof that another selected Dockerfile uses identical exclusions. Treat each effective configuration as a separate review target, and check it after changes to CI paths or Dockerfile naming.
Documentation: Build context and .dockerignore.
Pattern order can reopen an exclusion
The ignore file is a newline-separated set of patterns. Docker describes preprocessing, Go filepath.Match rules and an additional double-star wildcard that matches any number of directories, including zero. Do not substitute assumptions about a shell glob or another ignore-file format for that documented matching behavior.
An exclamation mark introduces an exception. The last matching line determines whether a particular file is included or excluded. A broad exception placed after a narrower exclusion can therefore restore a file the author thought remained excluded. Review the result for representative paths, not just whether a sensitive-looking name appears somewhere in the file.
Depth also matters. The documentation distinguishes patterns matching the context root from examples one or two directory levels down. A useful test fixture should include a root-level file and nested files with similar names. Matching one convenient path is not evidence that every nested version is excluded.
Docker permits excluding the Dockerfile and .dockerignore themselves, but still sends these build-control files because the builder needs them. The documentation says the excluded files cannot then be copied into the image through ADD, COPY or bind mounts. This special handling is another reason to avoid an unqualified claim that no bytes bearing an excluded name ever reach the builder.
Documentation: Build context and .dockerignore.
COPY exclusions are scoped to the instruction
COPY can read from the default context or, with --from, from a build stage, named context or image. The source location is therefore part of the instruction contract. Inspect it before treating two similar-looking COPY statements as consuming the same input set.
The COPY --exclude option omits matching paths from that copy operation. Docker lists a minimum Dockerfile version of 1.19 for the flag. That version requirement belongs to the Dockerfile feature, and should not be confused with a claim about an installed Docker Engine version.
Excluding a path from one COPY does not establish that the path was removed from the context or that another instruction cannot use it. Conversely, a file excluded from the context is unavailable to an ordinary COPY from that context. Docker documents an error when ADD or COPY tries to use such a missing source; the instruction does not recover it from the developer workspace.
Documentation: Dockerfile COPY reference, CopyIgnoredFile build check.
Named contexts add independent input routes
The --build-context flag adds named sources alongside the default positional context. Dockerfile instructions can reference these names like build stages. This is useful for separating application code, documentation or configuration, but it widens the list of inputs that a reviewer must identify.
A review of the default context alone does not establish the contents or provenance of a named source. Record each name and its value, then inspect the COPY or RUN mount that consumes it. A clean application context is not evidence about a separately supplied configuration directory, image or remote repository.
This is a scope distinction, not a claim that every context type processes ignore files identically. Validate the effective behavior for the source type and builder configuration in use. Do not silently extend a tested local-directory rule to a remote Git context or another named input.
Documentation: Build context and .dockerignore, Dockerfile COPY reference.
Remote contexts move the collection point
A Git URL used as context causes the builder to obtain the repository. Docker documents a shallow clone and recursive submodule cloning. Editing an uncommitted local ignore file does not show what a build using a remote repository URL will read. Check the selected remote reference and the files present there.
For a remote tarball context, Docker says the URL is sent to the builder and the download happens on the host running BuildKit. That host may not be the machine issuing the command. Debugging only local filesystem access or local network reachability can therefore examine the wrong environment.
The transfer model also differs by backend. Docker describes the legacy builder as transmitting the whole context, while BuildKit transmits files used by the build. The legacy CLI reference is explicitly scoped to that older backend and its differences. Do not quote its whole-context behavior as a universal description of every modern build.
Documentation: Build context and .dockerignore, Legacy builder and BuildKit differences.
A smaller final stage does not erase the earlier boundary
Multi-stage builds let a later stage copy selected artifacts while leaving build tools and other intermediate material out of the final image. That controls the output assembled into the chosen image. It does not establish that an earlier stage never had access to a file supplied as an input.
Similarly, an ignored credential file and a deliberately supplied build secret are different routes. Docker provides secret mounts and SSH mounts for credentials needed during a build. A correct exclusion policy should prevent accidental context inclusion without pretending that a build needing authentication can avoid all credential delivery.
The narrow conclusion is practical: list context inputs, deliberate secrets and final artifacts separately. For this article, the question is where a file can enter the build. Whether trusted code handles a supplied secret safely is a separate review, not a guarantee created by an ignore pattern or a small runtime stage.
Documentation: Build context and .dockerignore, Build secrets, Multi-stage builds.
Use a disposable fixture to test the effective boundary
An isolated acceptance plan can use harmless marker files representing unwanted operational notes, required source files and optional documentation. Include paths at several depths and a Dockerfile-specific ignore file. Run the actual intended build invocation against that fixture, not a shortened command that selects a different root.
Check both directions: required files remain available, and unwanted markers are absent from the relevant context-based operations. A build failure after excluding a source can be evidence that a dependency was removed; it is not automatically a reason to widen the context. First decide whether that file was supposed to be an input.
Exercise other input routes separately, including named contexts and remote sources. Inspect the resulting artifacts as a second check, rather than equating an absent final file with proven context exclusion. These are proposed tests, not results obtained in this article; no marker fixture was executed.
Keeping a context small can reduce unnecessary transfer and cache invalidation, as Docker explains. It does not provide a measured speedup for a particular repository. Measure the real build and record its backend, cache state and inputs before making a performance claim.
Documentation: Build context and .dockerignore, Dockerfile COPY reference, CopyIgnoredFile build check, Optimize cache usage.
Related reading
The Docker build secrets guide examines what can happen after code receives a temporary credential. This article instead focuses on context roots, ignore precedence and independent input routes.
Sources and scope
Documentation checked November 24, 2026. Sources are official Docker build-context, Dockerfile, build-check, backend, secrets, multi-stage and cache references. These documents establish documented mechanisms, not independent measurements. Repository examples and acceptance checks are illustrative. No container build, transfer audit, scan, remote-source experiment or production policy validation was executed.
Comments
Post a Comment