Sub topic 8
Running as a non-root user
This chapter assumes you can write a multi-stage Dockerfile and that you know how to build a static Go binary. It also assumes you have seen the scratch image, which holds only what you copy into it.
Why not run as root
A container process runs as root (UID 0) unless you say otherwise. Container root is not the same as host root, but it is still risky. If an attacker escapes the container through a kernel or runtime bug, they land on the host with UID 0. Root inside the container can also write to any file the image makes writable, and it holds more Linux capabilities than a web server needs.
Our Go service only listens on port 8080. It needs no special privileges, so it should not have any.
Numeric users in scratch
Scratch has no /etc/passwd, so a name like appuser cannot be resolved. Use a numeric UID and GID instead. The USER instruction accepts them directly.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app .
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /out/app /app
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/app"]The value 65532 is a convention. Distroless uses it for its nonroot user. Any UID above 0 works, but a high number is unlikely to collide with a real user on the host.
Port 8080 is above 1024, so a non-root process can bind it. A service that must listen on port 80 would fail with a permission error. Keep the container port high and map it to 80 on the host, for example docker run -p 80:8080.
Named users in distroless
Distroless images ship a /etc/passwd with a nonroot user at UID 65532. The :nonroot tag sets USER for you, so the final stage needs no USER line.
FROM golang:1.22 AS build
WORKDIR /src
COPY go.mod ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/app .
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
EXPOSE 8080
ENTRYPOINT ["/app"]Without the :nonroot suffix, the image runs as root. The plain tag and :nonroot differ only in this default, so check the tag you pulled.
File permissions
A non-root process can only read files it has permission to read. COPY keeps the permissions from the build stage, so a binary built with execute permission stays executable. Files written by COPY are owned by root by default, which is fine for a read-only binary.
Trouble starts when the program must write. Scratch has no /tmp, and root owns every directory you create. If your service needs to write files, use COPY --chown=65532:65532 to hand a directory to the user, or mount a writable volume at runtime.
COPY --from=build --chown=65532:65532 /data /dataVerifying the user
Do not assume the setting took effect. Inspect the image configuration.
docker image inspect --format '{{.Config.User}}' myapp:latestThe command prints 65532:65532 for the scratch example. An empty result means the image runs as root. Because scratch and distroless have no id command, you cannot check from inside the container, so the inspect output is your evidence.
Common errors
| Symptom | Likely cause |
|---|---|
| `bind: permission denied` on startup | The program listens on a port below 1024 |
| `permission denied` when writing a file | The directory is root-owned or missing |
| `unable to find user appuser` | A name was used on scratch, which has no `/etc/passwd` |
| Container runs as root on distroless | The `:nonroot` tag was not used and no `USER` was set |
Exercise
You build a Go service on scratch and add USER appuser. The container fails to start with an error that the user cannot be found. What is the cause, and what is the fix?
Show answer
Scratch has no /etc/passwd, so the name appuser cannot be resolved. Use a numeric ID instead, for example USER 65532:65532.
Exercise
Your non-root service on distroless fails with "permission denied" while binding to port 80. How do you fix it without running as root?
Show answer
Make the program listen on a port above 1024, such as 8080, and map it at runtime with docker run -p 80:8080. Binding ports below 1024 needs extra privileges that the non-root user does not have.