Sub topic 2
Controlling the build context with .dockerignore
This chapter assumes you can write a basic Dockerfile, run docker build, and read a GitHub Actions workflow. It explains what the build context is, why it matters in CI, and how a .dockerignore file controls it.
What the build context is
When you run docker build ., the final . is the build context. The client packs the files under that directory and sends them to the builder. Only files in the context can be used by COPY and ADD. A COPY cannot reach a file outside it.
The whole context is sent before the first instruction runs, whether or not the Dockerfile uses every file. A large context makes every build slower, and this cost is paid again on each CI run because runners start from a fresh checkout.
What goes wrong without a .dockerignore
Three problems show up when the context holds more than it should.
- Slow builds. A
node_modulesdirectory or a.githistory can be hundreds of megabytes. Sending it takes time on every build. - Broken caching.
COPY . .is cached by file content. If a log file or a build artifact changes, the layer is invalidated and every later step reruns. - Leaked files.
COPY . .copies everything it can see. A.envfile with secrets then lands in an image layer. Anyone who can pull the image can read it.
Copying a local node_modules also causes bugs. Native modules built on your laptop can be the wrong architecture for the Linux image. The Dockerfile should run npm ci itself.
Writing the file
Put a file named .dockerignore in the root of the context. Each line is a pattern, and matching paths are excluded from the context.
node_modules
.git
dist
*.log
.envEach entry has a reason:
node_modulesis rebuilt inside the image bynpm ci..gitis large and not needed to run the app.distis produced by the build stage, so a stale local copy must not be sent.*.logchanges often and would invalidate the cache..envholds secrets that must not enter a layer.
Pattern rules
The syntax looks like .gitignore but differs in a few places.
- Paths are relative to the context root.
*matches within one directory level. Use**/*.logto match logs at any depth.- A line starting with
!re-includes a path that an earlier line excluded. - Later lines override earlier ones.
A common allowlist style excludes everything, then adds back what the build needs:
*
!package.json
!package-lock.json
!src
!tsconfig.jsonThis is stricter. A new file is excluded until you list it, so secrets cannot slip in by accident. The cost is that you must update the list when the project layout changes.
Why this matters in CI
A CI runner checks out the repository, which includes the .git directory and any files the earlier steps created. Those extra files all go into the context. Excluding them gives faster uploads and more stable cache keys. It also means a step that writes a credentials file earlier in the job does not end up in the image.
The .dockerignore file must sit in the context root. If the workflow sets context: ./app, Docker reads ./app/.dockerignore, not the one at the repository root.
Checking what is sent
Build output shows the context size. With BuildKit, look for the transferring context line:
docker build --progress=plain -t example-app:test . 2>&1 | grep "transferring context"A line like transferring context: 2.1MB is healthy for a small service. A value in the hundreds of megabytes means something large is still being sent.
Exercise
A workflow sets context: ./services/api. Your .dockerignore is at the repository root and lists .env. A file services/api/.env exists in the checkout. Is it excluded from the build context, and how do you fix it if not?
Show answer
It is not excluded. Docker reads .dockerignore from the context root, which is services/api, so the root file is ignored. Move or copy the file to services/api/.dockerignore, or use a Dockerfile-specific ignore file named <Dockerfile name>.dockerignore next to the Dockerfile.