登录
首页 >  Golang >  Go问答

在运行容器时找不到将 Web 服务器命令复制到映像的路径

来源:stackoverflow

时间:2024-02-10 20:24:26 281浏览 收藏

本篇文章向大家介绍《在运行容器时找不到将 Web 服务器命令复制到映像的路径》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我遵循使用 busybox docker 映像构建应用程序:自定义映像的完整指南。

使用代码 docker-busybox-example。

dockerfile

# 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)
}

使用 goos=linux goarch=amd64 go build server.go 编译为可执行文件 server

构建基于图像的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")
}

不支持web服务器可执行文件吗?

docker:在 $path 中找不到可执行文件没有帮助

有什么解决办法吗?


正确答案


您的 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 配合得很好。

到这里,我们也就讲完了《在运行容器时找不到将 Web 服务器命令复制到映像的路径》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>