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:
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
docker build -qbuilds 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
-t hello:firstgives the image the namehelloand the tagfirst. - 3
--formatprints each image with a template that you write, instead of the usual table. - 4
whoamiis the command that runs inside the container. It prints the name of the user that runs it, and--rmremoves 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.
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?
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
-f Dockerfile.goodtells Docker to read the fileDockerfile.goodinstead of the default fileDockerfile. - 2
docker images hellolists every tag of the repositoryhello, 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.
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:
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
-dstarts the container in the background and prints its ID. - 2
--name goodgives the container a name, so we can use the name instead of the ID. - 3
-p 127.0.0.1:8090:8080connects port 8090 on your machine to port 8080 in the container. The address127.0.0.1keeps it private to your machine. - 4
curl -ssends a request to the program and hides the progress bar. - 5
docker top good -o pid,user,argslists the processes in the container. The option-ochooses 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:
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
timeis a shell word that runs the command after it and prints how long it took. - 2
docker stop -t 10asks 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:
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
docker stopsends the signal SIGTERM to the main process, and it waits for the process to end. - 2
quickis the name we gave with--name. Docker finds the container by this name.
docker stop sends SIGTERM to PID 1 in the container.
Clean up with docker rm -f good first quick.
Key points
- Build in one stage and ship from another.
COPY --fromcarries only the finished program: here, 17.8 MB instead of 1.38 GB. USERwith a non-root user makes the program run without root inside the container.- Use the exec form,
ENTRYPOINT ["/app"]. With the shell form,shis PID 1 and SIGTERM never reaches the program. Every stop then waits for SIGKILL. .dockerignoreand narrowCOPYlines 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.