Docker multistage go alpine
Skill kjuhwa/skills-hub/skills/devops/docker-multistage-go-alpine
Two-stage Docker build for a Go service — golang:alpine builder stage produces static binaries, alpine runtime stage ships them minimally with ca-certificates and tzdata.From its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill docker-multistage-go-alpineAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.7 KB, 617 tokens by cl100k_base, as published. Nobody here has run it
When to use
- Deploying a Go backend service via Docker.
- You want a small, fast-to-pull image without shipping the Go toolchain in production.
- You have multiple binaries from the same module (server, CLI, migrator).
Steps
- Builder stage — full Go toolchain, layer-caches deps before source:
FROM golang:1.26-alpine AS builder RUN apk add --no-cache git WORKDIR /src COPY server/go.mod server/go.sum ./server/ RUN cd server && go mod download COPY server/ ./server/ ARG VERSION=dev ARG COMMIT=unknown RUN cd server && CGO_ENABLED=0 go build -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${COMMIT}" -o bin/server ./cmd/server RUN cd server && CGO_ENABLED=0 go build -ldflags "-s -w" -o bin/migrate ./cmd/migrateCGO_ENABLED=0→ static binary, runs on plain alpine.-s -w→ strip symbol and DWARF tables (~30% smaller).-X main.version→ inject version/commit at link time.
- Runtime stage — minimal alpine with only what the binary needs at runtime:
FROM alpine:3.21 RUN apk add --no-cache ca-certificates tzdata WORKDIR /app COPY --from=builder /src/server/bin/server . COPY --from=builder /src/server/bin/migrate . COPY server/migrations/ ./migrations/ COPY docker/entrypoint.sh . RUN sed -i 's/\r$//' entrypoint.sh && chmod +x entrypoint.sh EXPOSE 8080 ENTRYPOINT ["./entrypoint.sh"]ca-certificatesfor HTTPS,tzdatafor time-zone-aware scheduling.sed -i 's/\r$//'on entrypoint handles Windows checkouts with CRLF.
- Pass version metadata at build time:
docker build --build-arg VERSION=$(git describe --tags) --build-arg COMMIT=$(git rev-parse --short HEAD) -t app .
Example
Final image size for a mid-size Go service: ~30-50 MB (vs ~800 MB for a naive single-stage with the toolchain).
Caveats
- If your code uses cgo (e.g. sqlite), drop
CGO_ENABLED=0and switch runtime toalpine:3.21→ addlibc6-compator run ongcr.io/distroless/cc. - Multiple binaries in the same image bloat it; if you deploy them as separate containers, split into separate Dockerfiles.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.