[英]Docker Go image: starting container process caused: exec: "app": executable file not found in $PATH: unknown
I have been reading a lot of similar issues on different languages, none of them are Go.我一直在阅读很多关于不同语言的类似问题,它们都不是 Go。
I just created a Dockerfile with the instructions I followed on official Docker hub page :我刚刚按照我在官方 Docker 集线器页面上的说明创建了一个 Dockerfile:
FROM golang:1.17.3
WORKDIR /go/src/app
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["app"]
This is my folder structure:这是我的文件夹结构:
users-service
|-> .gitignore
|-> Dockerfile
|-> go.mod
|-> main.go
|-> README.md
If anyone needs to see some code, this is how my main.go
looks like:如果有人需要查看一些代码,这就是我的
main.go
样子:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
docker build -t users-service.
docker build -t users-service.
: $ docker build -t users-service .
[+] Building 5.5s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 154B 0.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:1.17.3 3.3s
=> [auth] library/golang:pull token for registry-1.docker.io 0.0s
=> [1/5] FROM docker.io/library/golang:1.17.3@sha256:6556ce40115451e40d6afbc12658567906c9250b0fda250302dffbee9d529987 0.3s
=> [internal] load build context 0.1s
=> => transferring context: 2.05kB 0.0s
=> [2/5] WORKDIR /go/src/app 0.1s
=> [3/5] COPY . . 0.1s
=> [4/5] RUN go get -d -v ./... 0.6s
=> [5/5] RUN go install -v ./... 0.7s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:1f0e97ed123b079f80eb259dh3e34c90a48bf93e8f55629d05044fec8bfcaca6 0.0s
=> => naming to docker.io/library/users-service 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
docker run users-service
but I get that error:docker run users-service
但我得到了那个错误:$ docker run users-service
$ docker 运行用户服务
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "app": executable file not found in $PATH: unknown.
docker:来自守护进程的错误响应:OCI 运行时创建失败:container_linux.go:380:启动容器进程导致:exec:“app”:在 $PATH 中找不到可执行文件:未知。
I remember I had some troubles with GOPATH
environment variable in Visual Studio Code on Windows, maybe it's related... Any sugguestions?我记得我在 Windows 上的 Visual Studio Code 中的
GOPATH
环境变量遇到了一些麻烦,也许它是相关的......有什么建议吗?
Your "app" executable binary should be available in your $PATH
to call globally without any path prefix.您的“app”可执行二进制文件应该在您的
$PATH
中可用,以便在没有任何路径前缀的情况下全局调用。 Otherwise, you have to supply your full path to your executable like CMD ["/my/app"]
否则,您必须提供可执行文件的完整路径,例如
CMD ["/my/app"]
Also, I recommend using an ENTRYPOINT
instruction.另外,我建议使用
ENTRYPOINT
指令。 ENTRYPOINT
indicates the direct path to the executable, while CMD
indicates arguments supplied to the ENTRYPOINT
. ENTRYPOINT
表示可执行文件的直接路径,而CMD
表示 arguments 提供给ENTRYPOINT
。
Using combined RUN
instructions make your image layers minimal, your overall image size becomes little bit smaller compared to using multiple RUN
s.使用组合的
RUN
指令使您的图像层最小化,与使用多个RUN
相比,您的整体图像大小变得更小一些。
The official Docker documentation has useful instructions for building a Go image: https://docs.docker.com/language/golang/build-images/ The official Docker documentation has useful instructions for building a Go image: https://docs.docker.com/language/golang/build-images/
In summary, you need to build your Go binary and you need to configure the CMD appropriately, eg:总之,您需要构建您的 Go 二进制文件,并且您需要适当地配置 CMD,例如:
FROM golang:1.17.3
WORKDIR /app
COPY main.go .
COPY go.mod ./
RUN go build -o /my-go-app
CMD ["/my-go-app"]
Build the container:构建容器:
$ docker build -t users-service .
Run the docker container:运行 docker 容器:
$ docker run --rm -it users-service
Hello, World!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.