Some executable files show "no such file or directory" after run in docker, while some are not

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

Some executable files show "no such file or directory" after run in docker, while some are not

问题

这是我的Dockerfile:

FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY ls .
COPY tail .
COPY test .
COPY manager .
ENTRYPOINT ["/manager"]

之后执行以下命令:

[root@master go-docker-test]# docker build -t strangething:v1.13 .
[root@master go-docker-test]# docker run -d strangething:v1.13
[root@master go-docker-test]# docker logs b2

显示如下:

exec /manager: no such file or directory

我非常确定它在那里。我使用dive来查看它:

[Layers]───────────────────────────────────────────────────────────────────── [● Current Layer Contents]──────────────────────────────────────────────────
Cmp Image ID                     Size  Command                                Permission    UID:GID       Size  Filetree
    sha256:cb60fb9b862c6a89f9  2.3 MB  FROM sha256:cb60fb9b862c6a89f9         drwxr-xr-x        0:0     2.3 MB  ├── .
    sha256:3e884d7c2d4ba9bac6  118 kB  COPY ls . # buildkit                   drwxr-xr-x        0:0        0 B  │   ├── bin
    sha256:e75e9da8f1605f7944   67 kB  COPY tail . # buildkit                 drwxr-xr-x        0:0        0 B  │   ├── boot
    sha256:7a0f1970f36a364672  1.8 MB  COPY test . # buildkit                 drwxr-xr-x        0:0        0 B  │   ├── dev
    sha256:c9ab59cb1ce11477ca   47 MB  COPY manager . # buildkit              drwxr-xr-x        0:0     220 kB  │   ├─⊕ etc
                                                                              drwxr-xr-x 65532:65532        0 B  │   ├─⊕ home
[Layer Details]────────────────────────────────────────────────────────────── drwxr-xr-x        0:0        0 B  │   ├── lib
                                                                              drwxr-xr-x        0:0        0 B  │   ├── proc
Digest: sha256:c9ab59cb1ce11477cac4d634bb81cf7316c344b50f01a62a8e5ddcf355d5fe drwx------        0:0        0 B  │   ├── root
cf                                                                            drwxr-xr-x        0:0        0 B  │   ├── run
Tar ID: 998c57d00785ccffaf3b308a529c7f816633897097d1ef6519269a8e3c5af59b      drwxr-xr-x        0:0        0 B  │   ├── sbin
Command:                                                                      drwxr-xr-x        0:0        0 B  │   ├── sys
COPY manager . # buildkit                                                     drwxrwxrwx        0:0        0 B  │   ├── tmp
                                                                              drwxr-xr-x        0:0     2.1 MB  │   ├─⊕ usr
[Image Details]────────────────────────────────────────────────────────────── drwxr-xr-x        0:0     1.8 kB  │   └─⊕ var
                                                                              -rwxr-xr-x        0:0     118 kB  ├── ls
Total Image size: 51 MB                                                       -rwxr-xr-x        0:0      47 MB  ├── manager
Potential wasted space: 0 B                                                   -rwxr-xr-x        0:0      67 kB  ├── tail
Image efficiency score: 100 %                                                 -rwxr-xr-x        0:0     1.8 MB  └── test

Count   Total Space  Path

这个文件树让我有点困惑。

无论如何,奇怪的是,ls tail manager无法执行,但test可以执行。

当我将ENTRYPOINT ["/manager"]更改为ENTRYPOINT ["/test"]后:

[root@master go-docker-test]# docker logs c11
empty

test是由test.go构建的程序,我自己编写了它:

package main

import (
        "fmt"
)
type Per struct {
        Name string
        Age int
}
type Person struct {
        Name string
        Age int
        Lov Per
}

func main() {
        var one Person
        one.Name="abc"
        one.Age=11
        var two Per
        one.Lov=two
        if one.Lov != (Per{}){
                fmt.Println("not empty!")
        }else {
                fmt.Println("empty")
        }
}
英文:

This is my Dockerfile:

FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY ls .
COPY tail .
COPY test .
COPY manager .
ENTRYPOINT ["/manager"]

after

[root@master go-docker-test]# docker build -t strangething:v1.13 .
[root@master go-docker-test]# docker run -d strangething:v1.13
[root@master go-docker-test]# docker logs b2

it shows:

exec /manager: no such file or directory

I'm pretty sure it is there. I use dive to see it:

[Layers]───────────────────────────────────────────────────────────────────── [● Current Layer Contents]──────────────────────────────────────────────────
Cmp Image ID                     Size  Command                                Permission    UID:GID       Size  Filetree
    sha256:cb60fb9b862c6a89f9  2.3 MB  FROM sha256:cb60fb9b862c6a89f9         drwxr-xr-x        0:0     2.3 MB  ├── .
    sha256:3e884d7c2d4ba9bac6  118 kB  COPY ls . # buildkit                   drwxr-xr-x        0:0        0 B  │   ├── bin
    sha256:e75e9da8f1605f7944   67 kB  COPY tail . # buildkit                 drwxr-xr-x        0:0        0 B  │   ├── boot
    sha256:7a0f1970f36a364672  1.8 MB  COPY test . # buildkit                 drwxr-xr-x        0:0        0 B  │   ├── dev
    sha256:c9ab59cb1ce11477ca   47 MB  COPY manager . # buildkit              drwxr-xr-x        0:0     220 kB  │   ├─⊕ etc
                                                                              drwxr-xr-x 65532:65532        0 B  │   ├─⊕ home
[Layer Details]────────────────────────────────────────────────────────────── drwxr-xr-x        0:0        0 B  │   ├── lib
                                                                              drwxr-xr-x        0:0        0 B  │   ├── proc
Digest: sha256:c9ab59cb1ce11477cac4d634bb81cf7316c344b50f01a62a8e5ddcf355d5fe drwx------        0:0        0 B  │   ├── root
cf                                                                            drwxr-xr-x        0:0        0 B  │   ├── run
Tar ID: 998c57d00785ccffaf3b308a529c7f816633897097d1ef6519269a8e3c5af59b      drwxr-xr-x        0:0        0 B  │   ├── sbin
Command:                                                                      drwxr-xr-x        0:0        0 B  │   ├── sys
COPY manager . # buildkit                                                     drwxrwxrwx        0:0        0 B  │   ├── tmp
                                                                              drwxr-xr-x        0:0     2.1 MB  │   ├─⊕ usr
[Image Details]────────────────────────────────────────────────────────────── drwxr-xr-x        0:0     1.8 kB  │   └─⊕ var
                                                                              -rwxr-xr-x        0:0     118 kB  ├── ls
Total Image size: 51 MB                                                       -rwxr-xr-x        0:0      47 MB  ├── manager
Potential wasted space: 0 B                                                   -rwxr-xr-x        0:0      67 kB  ├── tail
Image efficiency score: 100 %                                                 -rwxr-xr-x        0:0     1.8 MB  └── test

Count   Total Space  Path

This file tree confuses me more or less.

Anyway, strange thing is, ls tail manager can't exec, but testcan exec.

after I change ENTRYPOINT ["/manager"] to ENTRYPOINT ["/test"]:

[root@master go-docker-test]# docker logs c11
empty

test is a program built by test.go, I write it myself:

package main

import (
        "fmt"
)
type Per struct {
        Name string
        Age int
}
type Person struct {
        Name string
        Age int
        Lov Per
}

func main() {
        var one Person
        one.Name="abc"
        one.Age=11
        var two Per
        one.Lov=two
        if one.Lov != (Per{}){
                fmt.Println("not empty!")
        }else {
                fmt.Println("empty")
        }
}

答案1

得分: 3

在阅读了silh的评论和kubebuilder的Dockerfile后,我通过在go build中添加CGO_ENABLED=0来解决了这个问题。这将动态链接器更改为静态链接器。

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

至少现在已经消除了"no such file or directory"错误。

英文:

After reading silh's comment and the kubebuilder Dockerfile again, I somehow solved this problem by adding CGO_ENABLED=0 to go build. It changes dynamic linker to static linker.

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

At least the no such file or directory error is now gone.

huangapple
  • 本文由 发表于 2023年2月14日 17:56:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446134.html
匿名

发表评论

匿名网友

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

确定