Write a Dockerfile for a Go service that produces a tiny image (under 20 MB).
IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]
Key trick → CGO_ENABLED=0 builds a static binary that needs no libc, so it runs on distroless/static (or scratch). -s -w strips debug symbols.
More on Write-a-Dockerfile tasks
Write a production Dockerfile for a Node.js API using a multi-stage build.IntermediateWrite a Dockerfile for a Python Flask app that runs with gunicorn as a non-root user.BeginnerWrite a multi-stage Dockerfile for a Java Spring Boot app built with Maven.IntermediateWrite a Dockerfile for a static React site served by Nginx.BeginnerThis Dockerfile rebuilds dependencies on every code change. Fix it.BeginnerWrite a `.dockerignore` for a typical Node or Python project and explain why it matters.Beginner