dongsong73032
2019-01-29 23:31
浏览 388

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

图片转代码服务由CSDN问答提供 功能建议

使用Docker多阶段构建部署简单的API时,会引发以下错误: \ n

 <代码> $ docker build -t api:latest。
 [...] 
 $ docker run -p 4880:4880 --rm-  name = api api:latest 
2019 / 01/29 23:02:15 exec:“ go”:在$ PATH 
   
 
 

中找不到可执行文件 没有从任何地方(或任何 exec.Command )调用 go ,我运行了 go mod供应商,所以我可以检查依赖源代码并找到 以下内容:

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

这可能是问题所在,如果是的话,我该如何解决? 我确定,我之前已经设法在多阶段构建中部署了 github.com/lib/pq ,但是我目前仍处于困境。

非常感谢,祝黑客开心。

Dockerfile

  FROM golang:1-alpine AS构建器 
ENV GO111MODULE = on 
RUN apk --no-cache添加ca证书git 
WORKDIR / app 
#首先复制go模块信息... 
COPY go.mod。
COPY go.sum。
#..  。然后下载以缓存内部版本之间的依赖项
RUN进行mod下载
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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题