【问题标题】:COPY web server command to image but not found when run container将 Web 服务器命令复制到图像但在运行容器时未找到
【发布时间】:2022-12-10 16:08:25
【问题描述】:

我关注Using the BusyBox Docker Image for Building Applications : A Complete Guide 定制图片。

使用代码docker-busybox-example。

文件

# Use busybox as the base image
FROM busybox
# Copy over the executable file
COPY ./server /home/server
# Run the executable file
CMD /home/server

网络服务器

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

构建基于图像的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。我我对此一无所知。

[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")
}

它不支持网络服务器可执行文件吗?

docker: executable file not found in $PATH 没有帮助

有什么解决办法吗?

【问题讨论】:

    标签: docker go dockerfile


    【解决方案1】:

    您的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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 2017-04-25
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      相关资源
      最近更新 更多