我正在尝试创建一个带有我的 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 用户进程导致"exec 格式错误">
我有同样的错误消息。对我来说,解决方法是交叉构建正确的架构。就我而言,amd64。喜欢这个:
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o [OUTPUT] .
检查这是否类似于问题 475containers/buildah
:
我认为这是因为系统不知道如何执行文件。
仅供参考:什么是合适的Go shebang线?还要注意 Dockerfile 中 CMD/ENTRYPOINT 的 shell 形式和 exec 形式之间的区别。
只需将
#!/bin/bash
添加到我的入口点文件即可解决问题。
或:
事实证明,
#!/bin/bash
在我的入口点文件中,但由于我复制并粘贴到该文件中,因此第一行是换行符,而不是#!/bin/bash
,有效地忽略了它。
如果这也对任何人有帮助:删除了空行,一切正常。
或:
如果有人觉得这很有用,如果您的 shell 脚本使用 CRLF 作为行尾和/或 UTF-8 与 BOM(例如,如果您在 Visual Studio 中创建了 shell 脚本文件),则可能会出现此问题。
仅更改为 LF 和直接的 UTF-8 为我修复了它。
或者(可能不是你的情况,但要完整):
对于在应用此修复程序后遇到
standard_init_linux.go:190: exec user process caused "no such file or directory"
错误的任何人来说,您可能使用的是不附带 bash 的高山基础映像。用
#!/bin/sh
替换#!/bin/bash
就可以了!