Skip to content

x509: certificate signed by unknown authority in a scratch image

Containertested on Docker 29.9.0, buildx v0.38.0

A Go program in a scratch image has no file system except what you copy in. Without a CA bundle it trusts no certificate authority, so every HTTPS connection fails – while the build and the health check stay green.

Symptom

sleep 1; curl -s localhost:8080/upstream | jq -r .error | grep -o 'x509: .*'
x509: certificate signed by unknown authority

Since switching to an “even smaller” image, the /upstream endpoint, which fetches an HTTPS page, only returns this error.

Diagnosis

Day 5 shows the comparison – the same image once with and once without the line that copies the CA bundle.

curl -s localhost:8081/upstream; echo
curl -s localhost:8080/upstream; echo
{"error":"Get \"https://example.com\": tls: failed to verify certificate: x509:
certificate signed by unknown authority"}
{"status":200,"url":"https://example.com"}

Causes

FROM scratchNo CA bundle in the empty image

On Linux, Go looks for the CA bundle at fixed paths such as /etc/ssl/certs/ca-certificates.crt. scratch contains no files, so it is missing.

Build greenThe error only shows at runtime

Neither the build nor a health check on /healthz opens an HTTPS connection – the error only appears on the first real call.

Fix

Copy the bundle from the build stage (golang:*-alpine ships it), then rebuild and check.
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
sleep 1; curl -s localhost:8080/upstream; docker rm -f notes-api
{"status":200,"url":"https://example.com"}

Don't: Turn off TLS verification in the code (InsecureSkipVerify) to make the error go away – that makes every connection open to interception.

▶
Break it yourself and find it – free
Dockerfiles hands-on, Day 15: diagnose and repair ten broken Dockerfiles against the clock.
Go to day 15 →

FAQ

Is there an alternative to copying?

Yes, gcr.io/distroless/static-debian13:nonroot already ships the CA certificates.

What else is missing in scratch?

The time zone database. Without it Go silently falls back to UTC; the build tag timetzdata embeds it in the binary.

Related error patterns

More