If we have a tiny web service which return the host name as below. We can use golang image and build the executable package, then move it into a basic linux container like alpine.
funchandler(w http.ResponseWriter, r *http.Request) { name, err := os.Hostname() if err != nil { fmt.Fprintf(w, "Can't get hostname") } else { fmt.Fprintf(w, "Go Hostname: %s\n", name) } }
funcmain() { r := mux.NewRouter() r.PathPrefix("/").HandlerFunc(handler) srv := &http.Server{ Handler: r, Addr: ":8000", // Good practice: enforce timeouts for servers you create! WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, }
log.Fatal(srv.ListenAndServe()) }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
FROM golang:1.17 AS builder # https://stackoverflow.com/questions/61515186/when-using-cgo-enabled-is-must-and-what-happens # https://gist.github.com/blessdyb/ebe59987e4a4632b28c10ec74a1eda0c ENV CGO_ENABLED=0 WORKDIR /build COPY . . RUN go mod download RUN go build -o app