How to Deploy Go to Docker Without Writing a Dockerfile
You can containerize any Go application without writing a Dockerfile using migetpacks. It reads your Go version from go.mod, produces a statically-linked binary with CGO_ENABLED=0, strips debug symbols for minimal size, and outputs a slim production image. One command, zero configuration.
Why Is Writing a Dockerfile for Go So Complicated?
A production-ready Go Dockerfile requires:
- Multi-stage builds to separate compilation from runtime
CGO_ENABLED=0for static linking- Linker flags
-s -wto strip debug symbols - Proper layer ordering for
go mod downloadcaching - CA certificates in the runtime image for HTTPS
- Choosing between scratch, distroless, or alpine base images
Get any of this wrong and you end up with 700MB images instead of 15MB, or binaries that can't make HTTPS requests.
How Do I Containerize a Go App Without a Dockerfile?
Open your terminal, navigate to your Go project, and run:
cd ~/projects/my-go-api
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-go-api:latest \
miget/migetpacks:latest
That's it. migetpacks will:
- Detect Go from your
go.mod - Read version from
go.mod'sgodirective - Run
go mod downloadwith caching - Build with
CGO_ENABLED=0and-ldflags="-s -w" - Copy CA certificates to the runtime image
- Output a minimal production image
How Does migetpacks Detect My Go Version?
Version is resolved in this order:
| Priority | Source | Example |
|---|---|---|
| 1 | GO_VERSION env var | GO_VERSION=1.23 |
| 2 | .go-version file | 1.25.0 |
| 3 | go directive in go.mod | go 1.23 |
| 4 | Default | Latest stable |
What Does the Generated Dockerfile Look Like?
migetpacks generates an optimized multi-stage Dockerfile:
# Builder stage
FROM golang:1.25 AS builder
WORKDIR /build
ENV CGO_ENABLED=0
ENV GOOS=linux
COPY go.mod go.sum* ./
RUN go mod download
COPY . .
RUN go build -ldflags="-s -w" -o /build/myapp . \
&& rm -rf .git *.go go.mod go.sum vendor/
# Runtime stage
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates
WORKDIR /app
COPY --from=builder /build /app
You never write or maintain this file - migetpacks handles it automatically.
How Do I Build Multiple Binaries?
For projects with multiple cmd/ packages, use the // +heroku install comment in go.mod:
module github.com/user/myapp
// +heroku install ./cmd/api ./cmd/worker
go 1.23
Then build and specify which binary to run:
cd ~/projects/my-go-monorepo
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-app:latest \
-e RUN_COMMAND="./bin/api" \
miget/migetpacks:latest
Both binaries are available at /app/bin/api and /app/bin/worker.
How Do I Inject Version Information?
Use linker symbols to embed version information at build time:
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-app:latest \
-e GO_LINKER_SYMBOL=main.version \
-e GO_LINKER_VALUE=1.2.3 \
miget/migetpacks:latest
This adds -X main.version=1.2.3 to the linker flags, so your app can print its version at runtime.
Does migetpacks Support golang-migrate?
Yes. If github.com/golang-migrate/migrate is detected in go.mod, the migrate CLI tool is automatically installed in the builder stage.
How Do I Build Secure Distroless Images?
Enable Docker Hardened Images for minimal containers:
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-app:latest \
-e USE_DHI=true \
miget/migetpacks:latest
Since Go produces static binaries, the distroless runtime works perfectly - no shell, no package manager, minimal attack surface.
What's the Difference Between migetpacks and Paketo Buildpacks?
| Feature | migetpacks | Paketo |
|---|---|---|
| Configuration | Zero-config | Requires project.toml |
| Image size | Minimal (slim base) | Larger (Ubuntu-based) |
| Build speed | Fast (BuildKit) | Slower |
| Static linking | Default | Configurable |
| DHI support | Yes (distroless) | No |
Next Steps
- Full Go documentation - version detection, caching, multi-binary builds
- Deploy on Miget - unlimited apps, one price
- Star on GitHub - migetpacks is open source
- Related guides: Rust, Python