Skip to main content

Distroless and stripped binaries

Distroless and scratch images​

DebugHero works with distroless and scratch images. Your image does not need a shell, ps, or Delve.

The debug container brings its own tools. It uses targetContainerName, so it sees the processes of your container, and it reads your binary through /proc/<pid>/root. Nothing is copied into your container.

Stripped binaries​

Delve needs the DWARF debug information in the binary. Some build flags remove it:

BuildBreakpointsVariablesAutomatic path mapping
go buildYesYes. Some show as "optimized away".Yes
go build -gcflags="all=-N -l"YesYes, allYes
go build -trimpathYesYesNo. See Map source paths.
go build -ldflags="-w"NoNoNo
go build -ldflags="-s -w"NoNoNo
Binary compressed with UPXNoNoNo

To check that a binary has debug information, run file on it:

shell
file ./checkout
Output
./checkout: ELF 64-bit LSB executable, x86-64, ..., with debug_info, not stripped

If the output does not contain with debug_info, Delve cannot debug the binary.

Optimized code​

By default, the Go compiler optimizes the code and inlines small functions. Delve can debug optimized code, but:

  • some variables show as "optimized away",
  • stepping can skip lines or jump between lines,
  • breakpoints in inlined functions can behave unexpectedly.

-gcflags="all=-N -l" turns off optimizations (-N) and inlining (-l) for all packages. The binary is larger and slower. Use it for images that you plan to debug, not for every production image.

Build an image that you can debug​

This example builds a distroless image with full debug information. WORKDIR /src makes /src the build directory, so the automatic path mapping works.

Dockerfile
FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -gcflags="all=-N -l" -o /out/checkout ./cmd/checkout

FROM gcr.io/distroless/static-debian12
COPY --from=build /out/checkout /checkout
ENTRYPOINT ["/checkout"]

Use Go 1.25 or older for now. See Go version.

A common pattern is to build two images from the same commit: a release image with the default flags, and a debug image with -gcflags="all=-N -l". Deploy the debug image to a development or staging environment when you need it.