我跟随使用BusyBox码头映像构建应用程序:完整指南来定制一个图像。
使用代码码头工人-忙箱-例子。
Dockerfile
# Use busybox as the base image
FROM busybox
# Copy over the executable file
COPY ./server /home/server
# Run the executable file
CMD /home/serverweb服务器
package main
import (
"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
构建了基于图像的总线盒
[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,我对此没有任何线索。
[mymachine@localhost tmp]$ docker run -p 8080:8080 --rm -it go-server ls -l /home
total 6408
-rwxrwxr-x 1 root root 6559402 Oct 13 19:53 server
[mymachine@localhost tmp]$ docker run -p 8080:8080 --rm -it go-server
/bin/sh: /home/server: not found 但是它适用于这个应用程序。
package main
import "fmt"
func main() {
fmt.Println("hello world")
}它不支持web服务器可执行文件吗?
docker:在$PATH中找不到可执行文件是没有帮助的
有什么解决办法吗?
发布于 2022-10-13 23:42:18
您的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)...and 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很好。
https://stackoverflow.com/questions/74061957
复制相似问题