Sub topic 4
Running docker build
This chapter assumes you can write a basic Dockerfile (FROM, WORKDIR, COPY, RUN) and know what a build context is. It covers the docker build command: what it takes as input, how to point it at the right files, and the errors you will meet most often.
What the command does
docker build takes two inputs: a Dockerfile and a build context. It runs the Dockerfile's instructions in order and produces an image. The Docker client packages the context and sends it to the builder. The builder, BuildKit in recent Docker versions, executes each instruction and stores the result as a layer.
The basic form
The general shape is docker build [options] PATH. The final argument is the context, and it is required.
docker build -t myapp:1.0 -t myapp:latest .The . is the context: the current directory. The builder looks for a file named Dockerfile at the root of the context. The -t flag, covered in the next chapter, names the resulting image. Here it applies two tags to one image.
Choosing a different Dockerfile
Use -f when the Dockerfile has another name or lives elsewhere. The context stays separate from the Dockerfile's location.
docker build -f docker/Dockerfile.prod -t myapp:prod .Here the Dockerfile is in docker/, but the context is still the current directory. COPY paths resolve against the context, not against the Dockerfile's folder.
Useful options
| Option | Purpose |
|---|---|
| `-t name:tag` | Name and tag the image. Repeatable. |
| `-f path` | Use a specific Dockerfile. |
| `--no-cache` | Ignore cached layers and rebuild everything. |
| `--progress=plain` | Print full, unfolded build output. |
| `--build-arg K=V` | Set an `ARG` value. |
| `--target stage` | Stop at a named stage of a multi-stage build. |
When a build misbehaves, rebuild with the output unfolded and the cache disabled:
docker build --no-cache --progress=plain -t myapp:debug .--progress=plain shows each command's full output. --no-cache rules out a stale layer as the cause.
Reading the output
Each Dockerfile instruction appears as a numbered step. A step marked CACHED was reused from an earlier build. Any other step actually ran. A failing RUN stops the build and shows the command's error output. Layers built before the failure stay in the cache, so a fixed build resumes quickly.
Common errors
docker build requires exactly 1 argument. You left out the context. Add.at the end.failed to read dockerfileorno such file. There is noDockerfileat the context root. Check your working directory or pass-f.COPY failed: file not found in build context. The file is outside the context, or.dockerignoreexcludes it. Paths such as../config.jsoncannot be copied. Change the context to a parent directory and use-f.- A slow "Sending build context" step. The context is large. Add a
.dockerignoreto exclude.git, logs, and similar files.
Checking the result
After a successful build, confirm the image exists:
docker image ls myappYou should see a row for each tag you applied, all sharing one image ID.
Exercise
Your Dockerfile is at build/Dockerfile, but it needs COPY requirements.txt . from the project root. Which command builds it, and what is the context?
Show answer
Run docker build -f build/Dockerfile -t myapp:1.0 . from the project root. The context is ., the project root, so requirements.txt is found. -f only tells Docker where the Dockerfile is. It does not change the context.
Exercise
A build fails with "COPY failed: file not found in build context" for a file that exists on disk. Name two likely causes.
Show answer
The file is outside the context directory, or a .dockerignore rule excludes it.