我的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服务器代码,问题依然存在。