Sub topic 7
What scratch leaves out: certificates, timezone data, users, and /tmp
This chapter assumes you can write a multi-stage Dockerfile and that you know scratch is an empty base image. You will see what a program quietly expects from a normal Linux filesystem, how each missing piece fails, and how to add it back.
An empty filesystem has no defaults
On Debian or Alpine, many files exist before your program runs. Your Go binary may read some of them without you noticing. On scratch, the only file is the one you copy in. Four gaps cause most of the surprises.
| Missing item | Typical symptom | Fix |
|---|---|---|
| CA certificates | `x509: certificate signed by unknown authority` on any HTTPS call | Copy `/etc/ssl/certs/ca-certificates.crt` |
| Timezone data | `unknown time zone America/New_York` | Copy `/usr/share/zoneinfo` or embed it in the binary |
| `/etc/passwd` and `/etc/group` | User names do not resolve | Use a numeric `USER` |
| `/tmp` | `open /tmp/...: no such file or directory` | Create it with mode 1777 |
Reproducing the failures
This program touches all three runtime gaps. It calls an HTTPS endpoint, loads a timezone, and creates a temporary file.
package main
import (
"fmt"
"net/http"
"os"
"time"
)
func main() {
if _, err := http.Get("https://example.com"); err != nil {
fmt.Println("https:", err)
}
if _, err := time.LoadLocation("America/New_York"); err != nil {
fmt.Println("tzdata:", err)
}
if f, err := os.CreateTemp("", "demo"); err != nil {
fmt.Println("tmp:", err)
} else {
f.Close()
}
}Built with CGO_ENABLED=0 and copied alone onto scratch, it prints all three errors. The build succeeds because the files are needed only at runtime.
Adding the files back
The build stage is a full Debian system, so it holds everything you need. Assemble a small root filesystem there, then copy it into the final stage in one step.
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 .
RUN mkdir -p /rootfs/etc/ssl/certs /rootfs/usr/share /rootfs/tmp \
&& cp /etc/ssl/certs/ca-certificates.crt /rootfs/etc/ssl/certs/ \
&& cp -r /usr/share/zoneinfo /rootfs/usr/share/zoneinfo \
&& chmod 1777 /rootfs/tmp
FROM scratch
COPY --from=build /rootfs/ /
COPY --from=build /out/app /app
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/app"]Copying the whole /rootfs/ directory keeps the mode on /tmp. The 1777 value sets the sticky bit and makes the directory writable by every user, which a non-root process needs.
Users without /etc/passwd
Docker's USER instruction accepts a name or a number. A name is looked up in /etc/passwd, which scratch does not have, so USER nobody fails. A numeric USER 65532:65532 needs no lookup. The kernel only needs a UID and GID.
The consequence is that the process has no user name and no home directory. Code that calls os/user.Current() or expects $HOME will fail. If your program needs a name, copy a minimal /etc/passwd and /etc/group into the image from the build stage.
Check the result
Build the image, then run it with a read-only root filesystem. This shows what the program really writes.
docker build -t demo .
docker run --rm --read-only --tmpfs /tmp demoWith --read-only, the image /tmp is not writable, so the --tmpfs flag supplies a writable one. In Kubernetes, use an emptyDir volume mounted at /tmp for the same purpose.
Exercise
A Go service on scratch runs as USER 65532:65532. It starts, but fails on its first HTTPS request and also on os.CreateTemp("", "x"). Name the two files or directories that are missing, and the mode the second one needs.
Show answer
The CA bundle /etc/ssl/certs/ca-certificates.crt is missing, which causes the x509 error. The /tmp directory is missing, and it needs mode 1777 so a non-root user can create files in it.