Go - Dockerization

Beginner 10/10 Teacher 10/10 Architect 10/10
Tip: Dockerize the Sample REST API with this multi-stage build.

Minimal Dockerfile

# build stage
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN go build -o /bin/app ./cmd/api

# run stage
FROM gcr.io/distroless/base-debian12
COPY --from=build /bin/app /app
USER nonroot:nonroot
ENTRYPOINT ["/app"]

Notes

  • Use multi-stage builds to minimize final image size.
  • Run as non-root in production.