douque2016 2018-07-16 09:17
浏览 100
已采纳

如何从二进制文件在Docker上运行Go Server

I have been trying to make a Dockerfile, that would let me build my go server as binary and then run it either from the scratch image or alpine. The server works fine locally, on macOS 10.13.5, and I made it work when it wasn't from binary on Docker.

I keep getting this error:

standard_init_linux.go:190: exec user process caused "exec format error"

I have been googling around and found something about system architecture. I am not sure how to check if that is the error and/or how to fix it.

Any hints for debugging or possible fix are much appreciated.

My 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"]

The output:

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
  • 写回答

1条回答 默认 最新

  • doubi4491 2018-07-29 11:27
    关注

    As @tgogos pointed out did I need to use what I build in the first step.

    My final Dockerfile ended like this with a few further improvements: The important part is second last line though:

    FROM golang:1.10.3 AS build
    WORKDIR /go/src/gitlab.com/main/server
    COPY . .
    RUN go get github.com/golang/dep/cmd/dep && \
          dep ensure && \
          rm -f schema/bindata.go && \
          go generate ./schema
    RUN CGO_ENABLED=0 GOOS=linux go build -a -o main .
    
    FROM alpine
    RUN apk add --no-cache ca-certificates
    COPY --from=build /go/src/gitlab.com/main/server/main .
    CMD ["/main"]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵