简体   繁体   English

Docker Go 映像:启动容器进程导致:exec:“app”:在 $PATH 中找不到可执行文件:未知

[英]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!")
}
  1. I ran 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
  1. Then I ran 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.

相关问题 运行 docker 容器时出错:启动容器进程导致“exec:\”python\“:在 $PATH 中找不到可执行文件”:未知 - Error running docker container: starting container process caused "exec: \"python\": executable file not found in $PATH": unknown 启动容器进程导致“exec:\\”go\\”:$PATH 中找不到可执行文件”:未知 - starting container process caused "exec: \"go\": executable file not found in $PATH": unknown Docker 启动容器进程导致“exec:\\”arg\\“:在 $PATH 中找不到可执行文件”:未知。 - Docker starting container process caused “exec: \”arg\“: executable file not found in $PATH”: unknown. Docker:启动容器进程导致“exec:\”-n\“:$PATH中找不到可执行文件”:未知 - Docker : starting container process caused “exec: \”-n\“: executable file not found in $PATH”: unknown OCI 运行时创建失败:container_linux.go:349:启动容器进程导致“exec:\\”xxxx\\“:在 $PATH 中找不到可执行文件”:未知 - OCI runtime create failed: container_linux.go:349: starting container process caused “exec: \”xxxx\“: executable file not found in $PATH”: unknown OCI 运行时创建失败:container_linux.go:348:启动容器进程导致“exec:\\”-it\\“:在 $PATH 中找不到可执行文件”:未知 - OCI runtime create failed: container_linux.go:348: starting container process caused “exec: \”-it\“: executable file not found in $PATH”:unknown Docker:OCI 运行时创建失败:container_linux.go:349:启动容器进程导致“exec:\”java\“:$PATH 中找不到可执行文件” - Docker: OCI runtime create failed: container_linux.go:349: starting container process caused “exec: \”java\“: executable file not found in $PATH” 启动容器进程导致“exec: > \\”exec\\”:在 $PATH 中找不到可执行文件”:未知 - starting container process caused "exec: > \"exec\": executable file not found in $PATH": unknown container_linux.go:349: 启动容器进程导致 exec: flask: 在 $PATH 中找不到可执行文件 - container_linux.go:349: starting container process caused exec: flask: executable file not found in $PATH 启动容器进程导致“exec:\”bash\“:在 $PATH 中找不到可执行文件”:未知 - starting container process caused "exec: \"bash\": executable file not found in $PATH": unknown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM