我一直在尝试创建一个Dockerfile,能够让我将我的Go服务器构建为二进制文件,然后从scratch镜像或alpine中运行它。这个服务器在本地的macOS 10.13.5上运行良好,并且当没有使用二进制文件时,我已经设法让其在Docker中正常工作。
我一直遇到这个错误:
standard_init_linux.go:190: exec user process caused "exec format error"
我已经在谷歌上搜索了一下,发现了一些关于系统架构的内容。但我不知道如何检查是否存在此错误以及如何修复它。
如果您有任何调试提示或可能的解决方案,都将不胜感激。
以下是我的Dockerfile:
FROM golang:1.10.3 as builder
WORKDIR /go/src/gitlab.com/main/server
COPY . .
RUN go get -d -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
FROM scratch
ADD main /
CMD ["/main"]
输出结果:
Building go
Step 1/9 : FROM golang:1.10.3 as builder
---> 4e611157870f
Step 2/9 : WORKDIR /go/src/gitlab.com/main/server
Removing intermediate container 20cd4d66008b
---> 621d9fc02dde
Step 3/9 : COPY . .
---> cab639571baf
Step 4/9 : RUN go get -d -v ./...
---> Running in 7681f9adc7b2
Removing intermediate container 7681f9adc7b2
---> 767a4c9dfb94
Step 5/9 : RUN go build -a -installsuffix cgo -o main .
---> Running in a6ec73121163
Removing intermediate container a6ec73121163
---> b9d7d1c0d2f9
Step 6/9 : FROM alpine:latest
---> 11cd0b38bc3c
Step 7/9 : WORKDIR /app
---> Using cache
---> 6d321d334b8f
Step 8/9 : COPY . .
---> 048a59fcdd8f
Step 9/9 : CMD ["/app/main"]
---> Running in d50d174644ff
Removing intermediate container d50d174644ff
---> 68f8f3c6cdf7
Successfully built 68f8f3c6cdf7
Successfully tagged main_go:latest
Creating go ... done
Attaching to go
go | standard_init_linux.go:190: exec user process caused "exec format error"
go exited with code 1
COPY --from=builder
这一行,它应该将之前阶段构建的产物复制到新的阶段中。你是否检查过这是否是问题所在? - tgogosADD main /
注释掉,并添加COPY --from=builder /go/src/gitlab.com/main/server/main .
,然后它就能正常构建和执行...至少在我的环境中是这样的。 - Scott Stensland