Unable to build Docker image using new Mac M1

Viewed 5356

I am attempting to build a Docker image for my application to use within Integration tests.

The image can be built fine on my old 2017 Macbook but fails when trying on my new Macbook with the M1 chip.

The error I receive is:

unable to build image: 
The command '/bin/sh -c make build' returned a non-zero code: 2 
{"version": "TEST", "output": "Step 1/9 : FROM golang:1.15.3-alpine3.12 AS builder---> 9701aa6ab80a
Step 2/9 : RUN apk update && apk add gcc make git libc-dev ---> Using cache ---> 87ff8d250e2d 
Step 3/9 : ADD ./ /src/ ---> Using cache ---> ef95bb030ff7
Step 4/9 : WORKDIR /src/ ---> Using cache\n ---> 3b982c9ab004
Step 5/9 : RUN make build ---> Running in f7596e65a80b\u001b[91m# github.com/qadre/huski.go\n/usr/local/go/pkg/tool/linux_arm64/link: running gcc failed: exit status 1 
collect2: fatal error: cannot find 'ld'\ncompilation terminated.\n\n\u001b[0m\u001b[91
mmake: *** [Makefile:8: build] Error 2\n\u001b[0mRemoving intermediate container f7596e65a80b\n"}

My make build is

build:
  @go build -race -o huski-go -ldflags="-X 'main.Version=${VERSION}'"

When I run ld -v I get:

@(#)PROGRAM:ld PROJECT:ld64-609.8 BUILD 15:07:46 Dec 18 2020 configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em LTO support using: LLVM version 12.0.0, (clang-1200.0.32.29) (static support for 27, runtime is 27) TAPI support using: Apple TAPI version 12.0.0 (tapi-1200.0.23.5)

Has anyone encountered this with the new Macbooks?

2 Answers

After some investigation, I found out this seemed to be an issue when using the Alpine container for my Go application. To fix this I had to add the binutils-gold dependency within my Dockerfile.

My Dockerfile now looks like the below and has fixed the issue:

FROM golang:1.15.3-alpine3.12 AS builder

RUN apk update && apk add gcc make git libc-dev binutils-gold

ADD ./ /src/

WORKDIR /src/

RUN make build 

FROM alpine:3.12

COPY --from=builder /src/static /app/static
COPY --from=builder /src/huski-go /app/

ENTRYPOINT ["/app/huski-go"]

You can read more about this where I found the answer here: https://github.com/nodejs/node/issues/4212

You should build in this way:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ...

this command allows u to build a x86 image in arm

Related