standard_init_linux.go:190: exec 用户进程在运行 Go 二进制文件时导致“exec 格式错误”

poc*_*ckn 8 go docker

我正在尝试使用 Go 二进制文件创建一个容器,用作数据库迁移器。但是,如果我运行二进制文件,它可以完美运行,但是,我正在努力将它放入容器中并在我的 docker-compose 堆栈中运行它。

下面是我的 Dockerfile。

FROM golang:1.11 AS build_base

WORKDIR /app

ENV GO111MODULE=on
# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .
RUN go mod download

FROM build_base AS binary_builder
# Here we copy the rest of the source code
COPY . .

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

#In this last stage, we start from a fresh Alpine image, to reduce the image size and not ship the Go compiler in our production artifacts.
FROM alpine AS database-migrator
# We add the certificates to be able to verify remote instances
RUN apk add ca-certificates
COPY --from=binary_builder /app /app

ENTRYPOINT ["/app/binary-name"]
Run Code Online (Sandbox Code Playgroud)

当我运行 docker-compose 堆栈时,MySQL 数据库设置正确,但我在数据库迁移器容器的日志中收到此错误。

数据迁移器_1 | standard_init_linux.go:190: exec 用户进程导致“exec 格式错误”

per*_*lin 10

我有同样的错误信息。对我来说,解决方法是为正确的架构交叉构建。在我的情况下 amd64。像这样:

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o [OUTPUT] .
Run Code Online (Sandbox Code Playgroud)