agentsclimarketplace

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

Install
npx -y skills add kjuhwa/skills-hub --skill docker-multistage-go-alpine

Assembled 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

  1. 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/migrate
    
    • CGO_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.
  2. 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-certificates for HTTPS, tzdata for time-zone-aware scheduling.
    • sed -i 's/\r$//' on entrypoint handles Windows checkouts with CRLF.
  3. 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=0 and switch runtime to alpine:3.21 → add libc6-compat or run on gcr.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.

Keep looking

Skills are one crate of 326,790. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.