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

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

问题描述:

我正在尝试用我的 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"]

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

data-migrator_1 | standard_init_linux.go:190: exec user process caused "exec format error"

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

下面是我的Dockerfile。 p>

  FROM golang:1.11 AS build_base 
 
WORKDIR / app 
 
ENV GO111MODULE = on \  n#我们要基于go。{mod,sum}文件填充模块缓存。
COPY go.mod。
COPY go.sum。
RUN go mod下载
 
FROM build_base AS binary_builder 
#在这里,我们 复制其余的源代码
COPY。  。
 
RUN CGO_ENABLED = 0 GOOS = linux GOARCH = amd64 go build 
 
#在最后一个阶段,我们从一个新的Alpine映像开始,以减小映像大小,并且不将Go编译器交付到我们的生产工件中。  
FROM高山AS数据库迁移器
#我们添加证书以能够验证远程实例
RUN apk添加ca证书
COPY --from = binary_builder / app / app 
 
ENTRYPOINT [“ / app / binary- 名称“] 
  code>  pre> 
 
 

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

data-migrator_1 | standard_init_linux.go:190:exec用户进程导致“ exec格式错误” p> blockquote> div>

Check if this is similar to containers/buildah issue 475 :

I think it is because the system does not know how to execute the file.
FYI: What's the appropriate Go shebang line?

Also be aware of the difference between the shell form and exec form of CMD/ENTRYPOINT in Dockerfile.

just adding #!/bin/bash to my entry point file fixed the issue.

Or:

Turns out the #!/bin/bash was in my entry point file, but since I did a copy and paste into that file, the first line was a newline, not the #!/bin/bash, effectively ignoring it.
If this helps anyone as well: Deleted the empty line and all worked fine.

Or:

In case anyone finds this useful, you can get this issue if your shell script uses CRLF for line endings and/or UTF-8 with BOM (e.g. if you created a shell script file in Visual Studio).
Changing to LF only and straight UTF-8 fixed it for me.

Or (probably not your case, but to be complete):

For anyone who got a standard_init_linux.go:190: exec user process caused "no such file or directory" error after applying this fix, you're probably on an alpine base image which does not come with bash.

Replacing #!/bin/bash with #!/bin/sh will do the trick!