Sub topic 3
Controlling the build context with .dockerignore
This chapter assumes you can write a basic Dockerfile and run docker build, as covered in the previous chapter. You should also be comfortable with a Node.js project that has a package.json and a node_modules folder.
What the build context is
When you run docker build -t myapp:1.0 ., the final . is the build context. It is the directory whose contents Docker makes available to the build. Docker sends the whole directory to the builder before it runs any instruction. A COPY instruction can only read files that are inside this context.
Everything in that directory is sent unless you exclude it. That includes node_modules, the .git history, log files, and .env files with secrets. Three problems follow from this:
- Large contexts slow every build, because the files must be transferred each time.
- Local
node_modulescan overwrite the dependencies installed inside the image when you runCOPY . .. Native modules compiled for your laptop's operating system will not work in an Alpine Linux container. - Secrets in
.envcan end up inside an image layer, where anyone who can pull the image can read them.
Writing a .dockerignore file
Place a file named .dockerignore in the root of the build context, next to the Dockerfile. Each line is a pattern for a path to exclude. Docker applies it before sending the context.
node_modules
npm-debug.log
.git
.env
Dockerfile
.dockerignoreEach entry has a reason:
| Pattern | Why it is excluded |
|---|---|
| `node_modules` | The image installs its own dependencies with `npm ci`. |
| `npm-debug.log` | Local debugging output has no place in the image. |
| `.git` | Version control history is large and not needed at runtime. |
| `.env` | It holds local secrets. Pass configuration at run time instead. |
| `Dockerfile`, `.dockerignore` | The build reads these itself. The app does not need them. |
How it affects COPY
With this file in place, the instruction COPY . . from the Dockerfile copies the source files but skips the excluded paths. The dependency step still works, because npm ci --omit=dev runs inside the image and creates a fresh node_modules there.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]You can see the effect in the build output. With BuildKit, the default builder, the line transferring context reports how many bytes were sent. Run the build once without a .dockerignore and once with it, and compare the numbers. In a project with a large node_modules, the difference is often hundreds of megabytes.
Common mistakes
- Wrong location. The file must be at the root of the context. A
.dockerignorein a subfolder is ignored unless that folder is the context. - Hiding a file you need. If a
COPYfails with "not found", check whether.dockerignoreexcludes the file. Excluded files behave as if they do not exist. - Trusting it as a secret store.
.dockerignorekeeps.envout ofCOPY . ., but it does not protect a secret you copy by another route, such as an explicitARGorENVline. - Missing nested folders. A pattern like
node_modulesmatches only at the context root. To match at any depth, use**/node_modules.
Exercise
Your build fails at COPY config/settings.json ./ with a "not found" error, but the file exists on disk. What do you check first, and why?
Show answer
Check whether .dockerignore excludes the file, for example through a pattern such as config or *.json. Docker removes excluded paths from the build context before the build starts, so COPY cannot see them even though they exist locally. Fix it by narrowing the pattern or adding a !config/settings.json exception.
Exercise
Why is it a problem to run COPY . . without excluding node_modules, even though the Dockerfile already runs npm ci?
Show answer
The local node_modules is copied after npm ci and overwrites the installed dependencies. It may contain native modules built for a different operating system, so the app can crash in the container. It also makes the context much larger, which slows every build.