Docker Go image: starting container process caused: exec: "app": executable file not found in $PATH: unknown

huangapple go评论85阅读模式
英文:

Docker Go image: starting container process caused: exec: "app": executable file not found in $PATH: unknown

问题

我在不同的语言中阅读了很多类似的问题,但没有一个是关于Go语言的。

我刚刚根据官方Docker Hub页面上的指示创建了一个Dockerfile:

FROM golang:1.17.3

WORKDIR /go/src/app
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]

这是我的文件夹结构:

users-service
 |-> .gitignore
 |-> Dockerfile
 |-> go.mod
 |-> main.go
 |-> README.md

如果有人需要看一些代码,这是我的main.go文件的内容:

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}
  1. 我运行了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. 然后我运行了docker run users-service命令,但是出现了以下错误:

$ docker run users-service

docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "app": 在$PATH中找不到可执行文件: unknown.

我记得在Windows上使用Visual Studio Code时,我遇到了一些关于GOPATH环境变量的问题,也许与此有关... 有什么建议吗?

英文:

I have been reading a lot of similar issues on different languages, none of them are Go.

I just created a Dockerfile with the instructions I followed on official Docker hub page:

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:

package main

import "fmt"

func main() {
	fmt.Println("Hello, World!")
}
  1. I ran 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: 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.

I remember I had some troubles with GOPATH environment variable in Visual Studio Code on Windows, maybe it's related... Any sugguestions?

答案1

得分: 2

官方的Docker文档提供了构建Go镜像的有用指令:https://docs.docker.com/language/golang/build-images/

简而言之,您需要构建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"]

构建容器:

$ docker build -t users-service .

运行Docker容器:

$ docker run --rm -it users-service
Hello, World!
英文:

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, e.g.:

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 run --rm -it users-service
Hello, World!

答案2

得分: 1

你的可执行二进制文件应该在 $PATH 中可用,以便在全局范围内调用而无需路径前缀。否则,你必须提供可执行文件的完整路径,例如 CMD ["/my/app"]

另外,我建议使用 ENTRYPOINT 指令。ENTRYPOINT 指示可执行文件的直接路径,而 CMD 则指示提供给 ENTRYPOINT 的参数。

使用组合的 RUN 指令可以使你的镜像层更小,与使用多个 RUN 相比,整体镜像大小会稍小一些。

英文:

Your "app" executable binary should be available in your $PATH to call globally without any path prefix. Otherwise, you have to supply your full path to your executable like CMD ["/my/app"]

Also, I recommend using an ENTRYPOINT instruction. ENTRYPOINT indicates the direct path to the executable, while CMD indicates arguments supplied to the ENTRYPOINT.

Using combined RUN instructions make your image layers minimal, your overall image size becomes little bit smaller compared to using multiple RUNs.

huangapple
  • 本文由 发表于 2021年11月30日 22:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/70171108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定