无法在具有 Linux Runner 的 Docker 容器中执行 GO 二进制文件

Go 和二进制文件是我们 docker 镜像的一部分。


我尝试了所有可能的组合来构建 Go 二进制文件


export GOARCH=386 && export GOOS=linux && go build ./cmd/status

export GOARCH=amd64 && export GOOS=windows && go build ./cmd/status


$ uname -a

 Linux runner-4KP_No95-project-35871-concurrent-0 44.44.444-115.233.amzn1.x86_64 #1 SMP Thu Feb 27 23:49:15 UTC 2020 x86_64 GNU/Linux

得到错误为


/pipeline/status: /pipeline/status: cannot execute binary file

来自 docker 文件的示例部分是 -


ARG GOLANG_VERSION=1.14


FROM golang:${GOLANG_VERSION} as build-helpers


ENV GOPRIVATE=code.abcd.com


RUN mkdir -p /pipeline-helpers


ADD /reusable-aspects/ci-caching/golang-preheat-cache  /golang-preheat-cache

RUN cd /golang-preheat-cache && go mod download


ADD helpers/go-pipeline-commands /pipeline-helpers/

RUN cd /pipeline-helpers && CGO_ENABLED=0 GOOS=linux make


FROM alpine


RUN mkdir -p /pipeline

WORKDIR /pipeline

COPY --from=build-helpers /pipeline-helpers/commit .

COPY --from=build-helpers /pipeline-helpers/status .

RUN chmod a+x commit

RUN chmod a+x status


ENTRYPOINT ["./commit"]

CMD []


翻翻过去那场雪
浏览 352回答 1
1回答

狐的传说

golang:1.14不是alpine基础,而是debian基础。因此,您当然不能在 alpine 映像中运行 debian 构建二进制文件。尝试更换FROM golang:${GOLANG_VERSION} as build-helpers和FROM golang:${GOLANG_VERSION}-alpine as build-helpers并添加以下行以下载构建二进制文件所需的库RUN apk update && \  apk --update upgrade && \  apk add --no-cache ca-certificates gcc musl-dev git && \  update-ca-certificates && \  rm -rf /var/cache/apk/*更新添加make并将apk更新并添加到下面FROM golang:...FROM golang:${GOLANG_VERSION}-alpine as build-helpersRUN apk update && \  apk --update upgrade && \  apk add --no-cache ca-certificates gcc musl-dev git make && \  update-ca-certificates && \  rm -rf /var/cache/apk/*OP 更新后更新问题由于您将alpine构建状态二进制文件从helperbase 复制到最终映像golang:${VERSION},即debian环境,因此它当然无法运行。我建议您对所有构建阶段或最终 docker 映像仅使用一个环境(alpine或debian)。所以你第一个 docker 镜像的第一个构建状态应该是FROM golang:${GOLANG_VERSION}最终图像请使用debian而不是 alpineFROM debian
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go