创建一个最小的基本镜像:
tar cv --files-from /dev/null | sudo docker import - skycn/base
建一个hello.go:
package main import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello World from Go in docker") } func main() { http.HandleFunc("/", helloHandler) fmt.Println("Started, serving at 8080") err := http.ListenAndServe(":8080", nil) if err != nil { panic("ListenAndServe: " + err.Error()) } }
编译hello.go
CGO_ENABLED=0 go install
建一个Dockerfile
FROM skycn/base:latest COPY hello / CMD ["/hello"]
编译Dockerfile
docker build -t skycn/hello-go .
运行hello-go
docker run -it -p 8080:8080 skycn/hello-go
发布docker image
docker push skycn/hello-go