Skip to Content
Skip to content
Run a Go Binary in FROM scratch With CA Certificates and Non-Root User
Advanced
Tutorials

Run a Go Binary in FROM scratch With CA Certificates and Non-Root User

Package a static Go binary in a FROM scratch Docker image with CA certificates for outbound HTTPS calls and a non-root numeric UID so the container runs safely in production. After this, your final image stays tiny without failing TLS verification or running as root.

Updated 4 min read GoDeployment

A Go binary compiled with CGO_ENABLED=0 runs inside an empty FROM scratch container—shrinking an 850 MB golang build image down to ~5–15 MB so Kubernetes pods pull in ~200 ms with zero OS CVEs. Yet the first time teams deploy FROM scratch to staging, two things break immediately: outbound HTTPS requests fail with x509: certificate signed by unknown authority, and production security scanners reject the container for running as UID 0 (root).

Three lines in your Dockerfile fix both cleanly while keeping the tiny scratch image. Verified on Go 1.27.

Case 1: Checking what the scratch container actually needs

Run a tiny static Go program that verifies x509.SystemCertPool() and prints its runtime UID, and measure both the binary and the Debian CA certificate bundle:

package main

import (
	"crypto/x509"
	"fmt"
	"os"
)

func main() {
	pool, err := x509.SystemCertPool()
	if err != nil {
		fmt.Println("cert_pool_err:", err)
	} else {
		fmt.Println("cert_pool_ok:", pool != nil)
	}
	fmt.Printf("uid=%d\n", os.Getuid())
}
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o app .
$ ls -lh app
-rwxr-xr-x 1 root root 2.9M Nov 27 13:31 app
$ ./app
cert_pool_ok: true
uid=0
$ ls -lh /etc/ssl/certs/ca-certificates.crt
-rw-r--r-- 1 root root 217K Nov 24 04:39 /etc/ssl/certs/ca-certificates.crt

The entire static binary is 2.9 MB and the root CA bundle is only 217 KB—giving you full outbound TLS verification inside a ~3.1 MB container.

Case 2: Multi-stage FROM scratch Dockerfile with CA certs and numeric UID

Copy /etc/ssl/certs/ca-certificates.crt from the builder stage, and set a numeric non-root user (USER 65532:65532) because scratch has no /etc/passwd to resolve a username like nonroot:

FROM golang:1.27-trixie AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -ldflags="-s -w" -trimpath -o /out/app ./cmd/server

FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /out/app /app
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/app"]

Case 3: Timezones (time/tzdata) and healthchecks with no shell

Two more production details appear once a real service runs in FROM scratch:

  • IANA timezones: time.LoadLocation("America/New_York") fails in scratch because /usr/share/zoneinfo does not exist. Add import _ "time/tzdata" in main.go to embed Go's timezone database directly inside your binary (~450 KB).
  • Container healthchecks: Because scratch has no curl or wget, either use Kubernetes HTTP probes directly or add a tiny -healthcheck flag to your binary so HEALTHCHECK CMD ["/app", "-healthcheck"] runs self-contained.

Case 4: When to choose scratch vs. distroless vs. alpine

Base image Includes CA certs & non-root? Best when
scratch (+ copied certs & USER 65532:65532) Only when you copy ca-certificates.crt and set numeric USER Smallest image size and zero OS packages
gcr.io/distroless/static-debian12:nonroot Yes—CA certs, tzdata, and nonroot UID 65532 included You want zero boilerplate (or need glibc via base-debian12 for CGo)
alpine Has shell & apk (~8 MB, musl libc) You explicitly need shell/CLI tools inside the container

💡 Tip

Because scratch has no writable temp folder by default, run your container with --read-only (or readOnlyRootFilesystem: true in Kubernetes) and mount a small tmpfs at /tmp only if your app writes temp files.

Common mistakes

  • Writing USER nonroot in FROM scratch: Fails at container start because /etc/passwd does not exist; always pass numeric USER 65532:65532.
  • Using shell-form ENTRYPOINT /app: Tries to execute /bin/sh -c /app, which does not exist in scratch; always use exec form ENTRYPOINT ["/app"].
  • Binding port 80 or 443 as a non-root user: Fails with permission denied; listen on :8080 behind your reverse proxy or load balancer.

⚠ Warning

Never bypass missing CA certificates by setting InsecureSkipVerify: true on http.Transport—that disables TLS verification completely and exposes outbound requests to man-in-the-middle attacks.

What happens if you skip this

A FROM scratch image without CA certs passes local HTTP checks and then fails every outbound HTTPS call in staging; omitting numeric USER leaves the container running as root and fails production security policies.

Related course: Go Advanced.

About

Package a static Go binary in a FROM scratch Docker image with CA certificates for outbound HTTPS calls and a non-root numeric UID so the container runs safely in production. After this, your final image stays tiny without failing TLS verification or running as root.

Responsible
Jay P
Last Update
11/27/2026
Reading Time
4 min
Was this helpful?
​ 0 ​ 0

Comments

Sign in to comment

No comments yet.