我按照使用 BusyBox Docker 圖像構建應用程序:自定義圖像的完整指南。使用代碼docker-busybox-example。文件# Use busybox as the base imageFROM busybox# Copy over the executable fileCOPY ./server /home/server# Run the executable fileCMD /home/server網絡服務器package mainimport ( "fmt" "net/http")func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World!")}func main() { http.HandleFunc("/", handler) fmt.Println("Server running...") http.ListenAndServe(":8080", nil)}編譯為可執行server文件GOOS=linux GOARCH=amd64 go build server.go構建基于圖像的busybox[mymachine@localhost tmp]$ docker image build -t go-server . Sending build context to Docker daemon 6.562MB Step 1/3 : FROM busybox ---> beae173ccac6 Step 2/3 : COPY ./server /home/server ---> Using cache ---> 9d58653768ea Step 3/3 : CMD /home/server ---> Running in 994cce171c11 Removing intermediate container 994cce171c11 ---> 38996797b6d8 Successfully built 38996797b6d8 Successfully tagged go-server:latest *當運行容器時,server找不到。我對此一無所知。它不支持網絡服務器可執行文件嗎?
1 回答

江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
你server
是一個動態的可執行文件......
$ ldd server linux-vdso.so.1 (0x00007ffcbdbd2000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3a78527000) libc.so.6 => /lib64/libc.so.6 (0x00007f3a78325000) /lib64/ld-linux-x86-64.so.2 (0x00007f3a78554000)
...并且busybox
圖像沒有任何必需的運行時庫。一種解決方案是使用 busybox 以外的東西,例如:
FROM ubuntu:22.04 COPY ./server /home/server CMD ["/home/server"]
(我已經CMD
在此處修改了您的聲明,以便可以使用 終止容器CTRL-C
。)
另一種選擇是構建靜態可執行文件:
$ CGO_ENABLED=0 go build $ ldd server not a dynamic executable
這適用于您的原始 Dockerfile。
- 1 回答
- 0 關注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消