Sub topic 5
How the Context Reaches the Builder
This chapter assumes you can write a simple Dockerfile, run docker build, and know that the last argument of docker build is the build context. Here you will see what happens to that context between your command and the first COPY.
The context is sent before any instruction runs
The Docker CLI does not read your Dockerfile and then fetch files one at a time. The build runs in a builder, which is a separate process (the BuildKit daemon, or a remote builder). The CLI sends the context to the builder, and the builder executes the Dockerfile against it.
With BuildKit, the default builder in current Docker releases, the transfer is lazy and incremental. BuildKit asks the client only for the files a COPY or ADD instruction needs. It also skips files it already has from a previous build and that have not changed. The legacy builder behaved differently. It packed the whole directory into a tar archive and uploaded all of it before step 1.
What you see in the output
The first lines of a build show the transfer. With BuildKit you will see a step like this:
=> [internal] load build context
=> => transferring context: 2.41MBThe number is the amount of data sent. On the first build it may be large. On a rebuild with few changes it is small, because only changed files are sent. With the legacy builder, the line Sending build context to Docker daemon 187.4MB shows the full archive size every time.
Why this matters
The context size has three effects.
- Time. Every file in the context must be read and hashed. A large
node_modulesor.gitdirectory slows the start of each build, even before any instruction runs. - Remote builders. When the builder is on another machine, the data crosses the network. A 500 MB context can dominate the build time.
- Cache.
COPY . .depends on the content of every file it copies. A change to any file in the context, including logs or editor swap files, creates a new layer and invalidates everything after it.
The filter is applied by the client, before files are sent. Files excluded by .dockerignore are not transferred and the builder cannot see them. The next chapter covers how to write that filter.
Measuring the transfer
You can compare the effect of a large directory yourself. Run this in a project directory:
# Create 200 MB of junk that the build does not need
mkdir -p big && dd if=/dev/zero of=big/blob bs=1M count=200
# Build and look at the "transferring context" line
docker build --progress=plain -t ctx-size .--progress=plain prints each step as plain text, so the transfer line is easy to find. Rebuild once more without changing anything. The second transfer is much smaller with BuildKit, because unchanged files are not sent again.
Then add big to .dockerignore and build again. If the Dockerfile has no COPY that touches big, BuildKit may not transfer it even without the ignore rule. But COPY . . does touch it, and then the ignore rule is what keeps it out.
Common errors
- Assuming a slow start is the Dockerfile. A long delay at "load build context" points to a large context, not to a slow instruction.
- Building with the home directory as the context.
docker build ~tries to read everything under it. Choose the smallest directory that contains what the Dockerfile needs. - Expecting the legacy builder's behavior. Advice found online about "sending context to the daemon" describes the full upload. Check which builder you use with
docker buildx versionor by looking at the output format.
Exercise
A build prints "transferring context: 1.2GB" on every run, even though the Dockerfile only copies package.json and a src/ directory. Name two possible causes and one fix.
Show answer
Possible causes: the Dockerfile uses COPY . . (or another broad copy) that requests every file, or the build runs with the legacy builder that uploads the whole directory. Large directories such as .git or node_modules in the context are the likely content. Fix: add those directories to .dockerignore, or narrow the context with a smaller PATH argument.