1

I follow Using the BusyBox Docker Image for Building Applications : A Complete Guide to customize an image.

Using code 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

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

compile as executable file server with GOOS=linux GOARCH=amd64 go build server.go

built image based 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  

*when run the container, server is not found.I I have no clues about this.

[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 

but it works for this application

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

Does it not support web server executable file?

docker: executable file not found in $PATH is not helpful

Any solutions for this?

Nick Dong
  • 3,638
  • 8
  • 47
  • 84

1 Answers1

2

Your server is a dynamic executable...

$ 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 the busybox image doesn't have any of the required runtime libraries. One solution is to use something other than busybox, e.g:

FROM ubuntu:22.04
COPY ./server /home/server
CMD ["/home/server"]

(I've modified your CMD statement here so that it's possible to kill the container using CTRL-C.)

The other option is to build a static executable:

$ CGO_ENABLED=0 go build
$ ldd server
        not a dynamic executable

This works fine with your original Dockerfile.

larsks
  • 277,717
  • 41
  • 399
  • 399