What a good Dockerfile looks like

A first Dockerfile works, and ships a 1.38 GB image that runs as root and takes ten seconds to stop. A build stage, a non-root user and the exec form fix all three.

Part 7 of 9 · Containers4 min read
the workshop: 1.38 GBcompiler, Debian, sourceCOPY --fromships: 17.8 MBonly the programUSER nonrootnot root insideENTRYPOINT ["/app"]SIGTERM reaches the app: stops in 0.1 sshell form: 10 s

Your first Dockerfile works. The image is over a gigabyte, the program inside runs as root, and docker stop waits ten seconds every time. All three problems come from five lines of the Dockerfile. Three small changes fix them.

The first Dockerfile

Take the smallest web server Go can write. Save it as main.go in a folder with a go.mod:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "hello from the box")
	})
	http.ListenAndServe(":8080", nil)
}
module hello

go 1.25

And the Dockerfile most of us write first:

FROM golang:1.25
WORKDIR /src
COPY . .
RUN go build -o /app .
CMD /app

Build it, then look at what came out:

Shell
docker build -q -t hello:first .  
docker images hello:first --format '{{.Repository}}:{{.Tag}}  {{.Size}}' 
docker run --rm hello:first whoami 
sha256:9e0123afda108030ef93197f5df6b91b0bf32b1c7ed22e98ad57d98b6beb7ff2
hello:first  1.38GB
root

Here is what each marked part does:

  1. 1docker build -q builds an image from the Dockerfile and prints only the image ID. The dot at the end is the folder with the files to build from.
  2. 2-t hello:first gives the image the name hello and the tag first.
  3. 3--format prints each image with a template that you write, instead of the usual table.
  4. 4whoami is the command that runs inside the container. It prints the name of the user that runs it, and --rm removes the container afterwards.

1.38 GB, as Docker Desktop reports it, to ship one small program. The box holds the whole workshop: the Go compiler, its standard library, a Debian system, the source code and the build cache. Whatever runs in the image also runs as root.

hello:firstdebianGo 1.25 toolchainsource + build cache/appthe part we need1.38 GB
The first image: a whole Debian system and the Go toolchain around one small program.

Build in the workshop, ship only the part

The program needs the compiler to build, but not to run. A multi-stage build uses two boxes. The first is the workshop, where we build. The second is the shipping box, which gets only the finished program. Save this as Dockerfile.good, next to the first one:

FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod ./
COPY *.go ./
RUN CGO_ENABLED=0 go build -o /app .

FROM gcr.io/distroless/static-debian12
COPY --from=build /app /app
USER nonroot
ENTRYPOINT ["/app"]

The first stage is the old Dockerfile with its own name, build. The second stage starts from distroless static. That image has no shell and no package manager, and the stage copies one file into it. CGO_ENABLED=0 makes Go build a program that needs no C library. So the program runs in an image that has no C library.

The second stage copies one file. What is left of the 1.38 GB?

Shell
docker build -q -f Dockerfile.good -t hello:good . 
docker images hello --format '{{.Repository}}:{{.Tag}}  {{.Size}}' 
sha256:28034d92ddec0e7b159065570868b9a41b084e192942f5f30f864a23cc3b3dd4
hello:good  17.8MB
hello:first  1.38GB

Here is what each marked part does:

  1. 1-f Dockerfile.good tells Docker to read the file Dockerfile.good instead of the default file Dockerfile.
  2. 2docker images hello lists every tag of the repository hello, so we see both images.

17.8 MB: the program and a thin base with certificates and time zones. The workshop stays on the build machine. A smaller image pulls faster and has fewer packages that can carry a vulnerability.

FROM golang:1.25 AS buildGo toolchainsourcescache/appstays behindCOPY --from=build /app /app FROM distroless/staticships: 17.8 MB
Stage one is the workshop. COPY --from carries only the finished program into stage two, the box that ships.

Put .git and Dockerfile* in a .dockerignore file. Then docker build does not send the repository, the two Dockerfiles or other files that are not code to the builder. Copying only go.mod and *.go, instead of ., also keeps these files out of the layers.

Not root

USER nonroot is the second change. The distroless image ships a user with that name, UID 65532, and from that line on everything runs as that user. The image has no whoami command, so we look from the host:

Shell
docker run -d --name good -p 127.0.0.1:8090:8080 hello:good   
curl -s localhost:8090 
docker top good -o pid,user,args 
64d81e10a6062fc582dca3a8832a05e655b883c839795dd11439c653a745908c
hello from the box
PID                 USER                COMMAND
77495               65532               /app

Here is what each marked part does:

  1. 1-d starts the container in the background and prints its ID.
  2. 2--name good gives the container a name, so we can use the name instead of the ID.
  3. 3-p 127.0.0.1:8090:8080 connects port 8090 on your machine to port 8080 in the container. The address 127.0.0.1 keeps it private to your machine.
  4. 4curl -s sends a request to the program and hides the progress bar.
  5. 5docker top good -o pid,user,args lists the processes in the container. The option -o chooses the columns: process number, user and command.

After the container ID, we see the program’s answer. The program runs as user 65532, not root. If an attacker breaks into the program, the attacker does not get root. The attacker cannot change what only root may change. But any files you mount for the program are still as reachable as you made them.

Who gets the stop signal

The third change is about who receives the stop signal. docker stop sends a signal, SIGTERM, to the process with PID 1 in the container, waits, and then sends SIGKILL. The first image wrote CMD /app, the shell form. Docker turned that into /bin/sh -c /app, so PID 1 is a shell, not our program:

Shell
docker run -d --name first hello:first
docker top first -o pid,user,args
time docker stop -t 10 first  
63bfba087f72dad7749e159b7c5de81e8c133fd0d0f1efe3966b4e5d20d112a9
PID                 USER                COMMAND
77627               root                /bin/sh -c /app
77641               root                /app
first
real	0m10.144s

Here is what each marked part does:

  1. 1time is a shell word that runs the command after it and prints how long it took.
  2. 2docker stop -t 10 asks the program to stop, and after 10 seconds it forces the program to end.

-t 10 is the default wait time, written in the command. The shell does not pass SIGTERM to /app. As PID 1, the shell also ignores the signal itself. Docker waits the full ten seconds. Then it kills both processes with SIGKILL, and they exit with code 137. They have no chance to finish a request or close a connection. The second image wrote ENTRYPOINT ["/app"], the exec form, so /app is PID 1 and gets the signal itself:

Shell
docker run -d --name quick hello:good
time docker stop -t 10 quick  
72185f119a86a46abd0c7c063c69bbd56f38affcbe0cdbf2030604ce056f9240
quick
real	0m0.107s

Here is what each marked part does:

  1. 1docker stop sends the signal SIGTERM to the main process, and it waits for the process to end.
  2. 2quick is the name we gave with --name. Docker finds the container by this name.
docker stopSIGTERM → PID 1CMD /appsh (PID 1)/appnot passed on10 s, then SIGKILLENTRYPOINT ["/app"]/app (PID 1)exits at once

docker stop sends SIGTERM to PID 1 in the container.

Clean up with docker rm -f good first quick.

FROM golang:1.25 AS buildWORKDIR /srcCOPY go.mod ./COPY *.go ./RUN CGO_ENABLED=0 go build -o /app .FROM gcr.io/distroless/static-debian12COPY --from=build /app /appUSER nonrootENTRYPOINT ["/app"]the workshoponly what the build needsa program with no C librarythe box that shipsonly the finished partnot rootthe program is PID 1
The finished Dockerfile, each line next to the problem it fixed.

Key points

  • Build in one stage and ship from another. COPY --from carries only the finished program: here, 17.8 MB instead of 1.38 GB.
  • USER with a non-root user makes the program run without root inside the container.
  • Use the exec form, ENTRYPOINT ["/app"]. With the shell form, sh is PID 1 and SIGTERM never reaches the program. Every stop then waits for SIGKILL.
  • .dockerignore and narrow COPY lines keep the repository and other files that are not code out of the build and the image.

Next, in part 8: the compose file from part 6, made ready for a real server.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS