dongsong73032 2019-01-29 23:31
浏览 745
已采纳

Golang Docker多阶段构建无法运行:exec:“执行”:在$ PATH中找不到可执行文件

When deploying a simple API with a Docker multi-stage build the following error is being thrown:

$ docker build -t api:latest .
[...]
$ docker run -p 4880:4880 --rm --name=api api:latest
2019/01/29 23:02:15 exec: "go": executable file not found in $PATH

As my application is not calling go from anywhere (or any exec.Command) I ran a go mod vendor so I could check the dependency source code and found the following:

vendor/github.com/lib/pq/oid/gen.go
12:     "os/exec"
64:     cmd := exec.Command("gofmt")

Could this be the issue and if so how can I fix it? I am sure I have managed to deploy github.com/lib/pq in a multi-stage build before but I am currently stuck.

Many thanks and Happy Hacking.

Dockerfile

FROM golang:1-alpine AS builder
ENV GO111MODULE=on
RUN apk --no-cache add ca-certificates git
WORKDIR /app
# Copy go module info first...
COPY go.mod .
COPY go.sum .
# ...then download to cache dependencies between builds
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix cgo -ldflags '-extldflags "-static"' -o api cmd/api/

FROM scratch
WORKDIR /app
COPY --from=builder /app/api /app/
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
EXPOSE 8080
ENTRYPOINT ["./api"]
  • 写回答

1条回答 默认 最新

  • dongqi0644 2019-01-31 04:42
    关注

    Your actual build line:

    RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -installsuffix cgo -ldflags '-extldflags "-static"' -o api cmd/api/
    

    is missing a relative path in front of cmd, making it instead be ./cmd/api will result in a successful build/run. cmd/api is a real thing apparently, https://golang.org/cmd/api/, so it's not erring out, but also not doing what you are expecting it to do. If you type go run cmd/api in your terminal you should see a ton of output unrelated to your application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog