Преглед изворни кода

CGO_ENABLED=0 for all build steps of Dockerfile

When running a docker buildx build for ARM64:
docker buildx build --no-cache --platform linux/arm64/v8 -f Dockerfile -t "any-tag-here" .

The build fails:
 => ERROR [build-env 8/8] RUN set -e ;    go test ./test/*.go;    go test ./pkg/*;    cd cmd/costmodel;    CGO_ENABLED=0 GOOS=linux GOARCH=amd64     go build -a -installsuffix cgo -o /go/bin/app                                                      26.5s
------
 > [build-env 8/8] RUN set -e ;    go test ./test/*.go;    go test ./pkg/*;    cd cmd/costmodel;    CGO_ENABLED=0 GOOS=linux GOARCH=amd64     go build -a -installsuffix cgo -o /go/bin/app:
------
Dockerfile:13
--------------------
  12 |     # Build the binary
  13 | >>> RUN set -e ;\
  14 | >>>     go test ./test/*.go;\
  15 | >>>     go test ./pkg/*;\
  16 | >>>     cd cmd/costmodel;\
  17 | >>>     CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
  18 | >>>     go build -a -installsuffix cgo -o /go/bin/app
  19 |
--------------------
error: failed to solve: process "/dev/.buildkit_qemu_emulator /bin/sh -c set -e ;    go test ./test/*.go;    go test ./pkg/*;    cd cmd/costmodel;    CGO_ENABLED=0 GOOS=linux GOARCH=amd64     go build -a -installsuffix cgo -o /go/bin/app" did not complete successfully: exit code: 1

Based on this part of the error message:
go build runtime/cgo: gcc: exit status 1

I suspected the missing GCC. After making this commit's changes, the
buildx command succeeds.
Michael Dresser пре 4 година
родитељ
комит
662b8d4685
1 измењених фајлова са 7 додато и 1 уклоњено
  1. 7 1
      Dockerfile

+ 7 - 1
Dockerfile

@@ -5,6 +5,12 @@ WORKDIR /app
 COPY go.mod .
 COPY go.sum .
 
+# This ensures that CGO is disabled for go test running AND for the build
+# step. This prevents a build failure when building an ARM64 image with
+# docker buildx. I believe this is because the ARM64 version of the
+# golang:latest image does not contain GCC, while the AMD64 version does.
+ARG CGO_ENABLED=0
+
 # Get dependencies - will also be cached if we won't change mod/sum
 RUN go mod download
 # COPY the source code as the last step
@@ -14,7 +20,7 @@ RUN set -e ;\
     go test ./test/*.go;\
     go test ./pkg/*;\
     cd cmd/costmodel;\
-    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
+    GOOS=linux GOARCH=amd64 \
     go build -a -installsuffix cgo -o /go/bin/app
 
 FROM alpine:latest