Skip to content

Instantly share code, notes, and snippets.

@cqsd
Created September 14, 2021 07:14
Show Gist options
  • Save cqsd/a50a4597c9b02336d944e5821eea5424 to your computer and use it in GitHub Desktop.
Save cqsd/a50a4597c9b02336d944e5821eea5424 to your computer and use it in GitHub Desktop.
workaround for qemu segmentation fault when compiling inside of docker build on m1 mac

if you've been using qemu emulation to do your whole build in a different architecture like this

docker buildx build --platform=linux/amd64 -t whatever .

and you've been getting a segmentation fault at some point like this,

> [builder 4/4] RUN CGO_ENABLED=0 go build -ldflags '-w -extldflags "-static"' -o whatever ./cmd/whatever
#15 0.892 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
#15 0.896 Segmentation fault

and your compiler supports cross-compilation (like the go compiler), you might be able to get around the issue with a multi-stage build. remove the explicit platform from the build command, cross-compile in the build stage on your default architecture, then copy the binary into the final stage on your target architecture

for example

FROM golang:alpine as builder

WORKDIR /build

ADD . .

# cross-compile here
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags '-w -extldflags "-static"' -o whatever ./cmd/whatever

# note this --platform= bit
FROM --platform=linux/amd64 alpine:latest

# do whatever

WORKDIR /opt/runtime

COPY --from=builder /build/whatever /opt/runtime/whatever

CMD /opt/runtime/whatever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment