Sub topic 4
Building an image with a Dockerfile
This chapter assumes you can run containers with docker run, use a terminal, and have Docker installed. You should also know that an image is a stack of layers. A Dockerfile is the recipe that produces that stack.
How a Dockerfile maps to layers
A Dockerfile is a text file of instructions, read from top to bottom. Most instructions that change the filesystem (RUN, COPY, ADD) create a new layer. Instructions such as WORKDIR, ENV, and ENTRYPOINT change image metadata and add no files of their own.
| Instruction | Purpose | Adds filesystem layer |
|---|---|---|
| `FROM` | Sets the base image for a build stage | Brings in the base's layers |
| `WORKDIR` | Sets the working directory for later instructions | No (creates the directory if missing) |
| `COPY` | Copies files from the build context into the image | Yes |
| `RUN` | Runs a command at build time and keeps the result | Yes |
| `ENTRYPOINT` | Sets the program the container starts | No |
A worked example
This Dockerfile builds a Go program. It has two stages, so only the compiled binary reaches the final image. Multi-stage builds get their own chapter, so here we focus on the instruction flow.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]Read it in order:
FROM golang:1.22 AS buildstarts a stage namedbuildfrom the Go toolchain image.WORKDIR /srcmakes/srcthe directory for the commands that follow.COPY go.mod go.sum ./copies only the dependency files.RUN go mod downloadfetches the dependencies.COPY . .copies the rest of the source.RUN ... go buildcompiles a static binary.CGO_ENABLED=0turns off C linking so the binary runs in an image with no C library.- The second
FROMstarts a fresh, minimal image.COPY --from=buildtakes the binary from the first stage. ENTRYPOINTsets the command that runs when a container starts.
The dependency files are copied before the source on purpose. That keeps the download layer reusable when only source code changes, which a later chapter covers.
Building and inspecting the result
Run the build from the directory that holds the Dockerfile. The final . is the build context.
docker build -t myapp:1.0 .
docker history myapp:1.0
docker image inspect --format '{{.Id}}' myapp:1.0-t myapp:1.0 gives the image a name and tag. docker history lists the layers and the instruction that created each one. The last command prints the image ID.
Common errors
- Shell form versus exec form.
ENTRYPOINT ["/app"](exec form, a JSON array) runs the program directly, so it receives signals such asSIGTERMfromdocker stop.ENTRYPOINT /app(shell form) wraps it in/bin/sh -c, which can block those signals. - A missing file in
COPY. The path is relative to the build context, not to your current shell directory. Files outside the context cannot be copied. - Running a shell in a minimal image. The distroless image above has no shell.
docker run -it myapp:1.0 shfails by design.
Exercise
In the Dockerfile above, why does COPY go.mod go.sum ./ come before COPY . ., and which kind of instruction in the final stage adds no filesystem layer?
Show answer
Copying the dependency files first lets the go mod download layer be reused whenever only source files change, because its inputs have not changed. ENTRYPOINT (like WORKDIR and ENV) changes only image metadata and adds no filesystem layer.