English 中文(简体)
必须在cker集装箱内挖掘二手提炼器,但“无此类文档或目录”。
原标题:Want to exec ADDed binary inside docker container but "no such file or directory"

我的Dockerfile在我用在集装箱内复制我的源代码,然后建造,最后发射。 之后,我决定,它需要太长的时间,现在,我要复制本,使之成为现成的cker。

为了这一问题的目的,我只是假定这只是简单地 go兰服务器:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
        fmt.Fprintf(w, "Hello World!!!")
    })

    log.Fatal(http.ListenAndServe(":8080", nil))

}

然后,我汇编一下它。

go build  -o webserver *.go

然后,我想抄送双筒,装入cker子集装箱并发射。

FROM golang:1.20-alpine

RUN go version

WORKDIR /go/src/golang-baseapp

ADD ./webserver         /go/src/golang-baseapp/
RUN ls -a               /go/src/golang-baseapp/

RUN chmod +x /go/src/golang-baseapp/webserver
RUN stat /go/src/golang-baseapp/webserver

EXPOSE 8080

CMD ["/go/src/golang-baseapp/webserver"]

The ls command shows that everything is being copied into the container but the CMD step fails and says exec /go/src/golang-baseapp/webserver: no such file or directory

我的第一个理论是,这是特权的错误,但使用“chmod +x”没有成功。 我以以下指挥方式塑造和掌握我的形象:

docker build --no-cache --progress=plain -t sample-image .
docker run sample-image

产出:

$ docker build --no-cache --progress=plain -t sample-image .
#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 332B done
#1 DONE 0.0s

#2 [internal] load .dockerignore
#2 transferring context: 2B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/golang:1.20-alpine
#3 DONE 0.5s

#4 [1/7] FROM docker.io/library/golang:1.20-alpine@sha256:3ae92bcab3301033767e8c13d401394e53ad2732f770c313a34630193ed009b8
#4 CACHED

#5 [internal] load build context
#5 transferring context: 33B done
#5 DONE 0.0s

#6 [2/7] RUN go version
#6 0.410 go version go1.20.12 linux/amd64
#6 DONE 0.5s

#7 [3/7] WORKDIR /go/src/golang-baseapp
#7 DONE 0.0s

#8 [4/7] ADD ./webserver        /go/src/golang-baseapp/
#8 DONE 0.1s

#9 [5/7] RUN ls -a /go/src/golang-baseapp/
#9 0.491 .
#9 0.491 ..
#9 0.491 webserver
#9 DONE 0.6s

#10 [6/7] RUN chmod +x /go/src/golang-baseapp/webserver
#10 DONE 0.6s

#11 [7/7] RUN stat /go/src/golang-baseapp/webserver
#11 0.529   File: /go/src/golang-baseapp/webserver
#11 0.529   Size: 6678922       Blocks: 13048      IO Block: 4096   regular file
#11 0.529 Device: 63h/99d   Inode: 916071      Links: 1
#11 0.529 Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
#11 0.529 Access: 2023-12-10 01:59:03.542679113 +0000
#11 0.529 Modify: 2023-12-10 01:59:03.542679113 +0000
#11 0.529 Change: 2023-12-10 02:00:00.582680528 +0000
#11 DONE 0.6s

#12 exporting to image
#12 exporting layers
#12 exporting layers 0.2s done
#12 writing image sha256:692735852bfbe2de7a94490f9f6143991aea1406c84e6731adff761e23f51ff7 done
#12 naming to docker.io/library/sample-image done
#12 DONE 0.2s
$ docker run sample-image
exec /go/src/golang-baseapp/webserver: no such file or directory

任何建议都是无价的。

EDIT:我简化了这个例子,并增加了Agolang服务器代码,问题依然存在。

最佳回答

Problem diagnosis

铺设图人图像,提供更多的信息,特别是集装箱的出走,发出以下错误信息:

{"msg":"exec container process (missing dynamic library?) `/go/src/golang-baseapp/webserver`: No such file or directory","level":"error","time":"2023-12-10T02:05:37.668177Z"}

我怀疑这是使用alpine的工艺品,特别是以下事实:alpine-linux的用途:musl libc (musl.libc.org)。 然而,我们的系统很可能无法运行和编辑musl libc。 防止这种行为有几个途径。 我将强调两个变量。

Variant 1: build in an alpine-environment

例如,这可以通过多层次的cker器来实现。

FROM golang:1.20-alpine as BUILDER
WORKDIR /src/
COPY  main.go .
RUN go build -o webserver *.go

FROM golang:1.20-alpine
RUN go version
WORKDIR /go/src/golang-baseapp
COPY --from=BUILDER 
  /src/webserver 
  /go/src/golang-baseapp/

RUN chmod +x /go/src/golang-baseapp/webserver

EXPOSE 8080

CMD ["/go/src/golang-baseapp/webserver"]

Variant 2: build a pure go-version of netgo

为此,我们可以在东道国的基础上再接再厉,但在我们的汇编指挥中添加“净”旗帜:

go build -tags net -o webserver *.go

目前,我们独立于任何系统图书馆。 因此,我们甚至可以使用<代码>cratch-image来管理我们的申请:

FROM scratch

WORKDIR /app
COPY webserver /app/

EXPOSE 8080
CMD [ "/app/webserver" ]

Testing the result

不管我们采用什么方法,我们最终都掌握了功能形象。 为了测试图像,我们应当使用集装箱-港口编码<80<>80,暴露于某些东道港口。 我选择了东道港口18080,例如:

docker run --publish 18080:8080 --rm sample-image

我们现在可访问http:// localhost:18080。 或使用简单的<代码>curl http:// localhost:18080,以获得预期的答复:

Hello World!!!

Remarks

我建议作一些改动。


为了编纂一项生产可用应用,我们应当取消试装符号,以尽量减少可执行的最终规模:

go build 
  -tags netgo 
  -ldflags="-s -w" `# omit symbol table (-s) and DWARF symbol table (-w)` 
  -o webserver 
  *.go

这使最终可执行的规模从6.4MB降至44.4MB。


关于集装箱档案,我建议使用<代码>COPY而不是ADD/code>。 我还建议使用<代码>--chmod的旗帜<>COPY来简化集装箱档案(详情见

FROM golang:1.20-alpine

WORKDIR /go/src/golang-baseapp
COPY 
  --chmod=111 
  ./webserver /go/src/golang-baseapp/

EXPOSE 8080

CMD ["/go/src/golang-baseapp/webserver"]

给予用户、团体和世界可执行轨道意味着该集装箱可以随意使用(如开放式货车在违约时使用),而且该集装箱仍将有效。

问题回答

暂无回答




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签