How to Deploy Go to Docker Without Writing a Dockerfile

M
Miget Team
Writer
January 16, 2026
5 min read

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=0 for static linking
  • Linker flags -s -w to strip debug symbols
  • Proper layer ordering for go mod download caching
  • 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:

  1. Detect Go from your go.mod
  2. Read version from go.mod's go directive
  3. Run go mod download with caching
  4. Build with CGO_ENABLED=0 and -ldflags="-s -w"
  5. Copy CA certificates to the runtime image
  6. Output a minimal production image

How Does migetpacks Detect My Go Version?

Version is resolved in this order:

PrioritySourceExample
1GO_VERSION env varGO_VERSION=1.23
2.go-version file1.25.0
3go directive in go.modgo 1.23
4DefaultLatest 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?

FeaturemigetpacksPaketo
ConfigurationZero-configRequires project.toml
Image sizeMinimal (slim base)Larger (Ubuntu-based)
Build speedFast (BuildKit)Slower
Static linkingDefaultConfigurable
DHI supportYes (distroless)No

Next Steps

Stay UpdatedWith Latest Posts

Subscribe to our newsletter and never miss a new blog post, update, or special offer from the Miget team.