Sub topic 11
Checking what the build actually receives
This chapter assumes you can write a simple Dockerfile, run docker build and docker run, and that you know the build context is the directory passed as the final argument to docker build. It also assumes you have seen .dockerignore patterns. Here you will learn how to see which files a build really gets, so you can debug missing or unexpected files.
Why inspect the context
Two kinds of bugs come up often:
- A
COPYfails with "file not found", even though the file exists on disk. A.dockerignorerule probably removed it before the build saw it. - The image contains files you did not mean to ship, such as
.envornode_modules. A pattern probably did not match what you expected.
Reading the .dockerignore file and guessing is slow. A better approach is to look at the filtered context directly.
Build a debug image
The idea is to copy the whole context into a throwaway image, then list its contents. Save this as Dockerfile.debug in the same directory as your project:
FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD ["find", "."]COPY . /build-context copies everything the builder received. Files removed by .dockerignore are not in the context, so they are not copied.
Build and run it:
docker build -t context-debug -f Dockerfile.debug .
docker run --rm context-debugThe -f flag only selects the Dockerfile. The context is still .. The output is a list of every file that survived filtering.
Read the result
Compare the list with what you expect. Suppose your .dockerignore contains:
*.md
!README*.md
README-secret.mdThe last matching rule decides. *.md excludes all Markdown files in the context root. !README*.md brings back README.md and README-secret.md. The final line then excludes README-secret.md again. If you had written the rules in a different order, the debug listing would show a different result. Run the debug image to confirm which files remain.
To check one file, filter the output:
docker run --rm context-debug find . -name "*.env*"If this prints nothing, the file is not in the context. Overriding CMD this way works because busybox includes find.
Common errors
| Symptom | Likely cause | What to check |
|---|---|---|
| `COPY failed: file not found` | The file is excluded by `.dockerignore`, or it lies outside the context | Look for it in the debug listing |
| The debug image lists `.git` or `node_modules` | The pattern is missing, or it is in the wrong directory | Confirm `.dockerignore` sits at the context root |
| A rule seems ignored | A later `!` rule re-includes the file | Read rules top to bottom; the last match wins |
| Debug build uses the wrong filters | A Dockerfile-specific ignore file takes precedence | Look for `Dockerfile.debug.dockerignore` |
The last row matters. With BuildKit, a file named after the Dockerfile plus .dockerignore replaces the root .dockerignore for that build. Your debug Dockerfile, named Dockerfile.debug, would look for Dockerfile.debug.dockerignore. If it does not exist, the root .dockerignore applies, which is what you want when testing.
Exercise
Exercise
Your build fails with "COPY failed: file not found: config/local.json", but the file exists in your project. Using the debug approach from this chapter, how do you find out whether .dockerignore removed it? Write the commands.
Show answer
Save the debug Dockerfile as Dockerfile.debug (FROM busybox, COPY . /build-context, WORKDIR /build-context, CMD ["find", "."]), then run docker build -t context-debug -f Dockerfile.debug . followed by docker run --rm context-debug find . -name "local.json". If nothing is printed, a .dockerignore rule excluded the file (or the context is the wrong directory). Review the rules in order, since the last matching rule decides, and add a !config/local.json exception after the broader pattern if the file is needed.