A go.mod file and a main package are enough: the build compiles the binary and places it alone in a lightweight image.
What the build does
| Step | Detail |
|---|---|
| Compile image | golang:<version>-alpine, version read from the go directive in go.mod, 1.24 by default |
| Dependencies | go mod download, with a cache kept from one build to the next |
| Compilation | CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" |
| Final image | Alpine 3.21 with ca-certificates and tzdata, plus the binary |
| Start | ./server |
The binary is static (CGO_ENABLED=0). Exception: if go.mod mentions github.com/mattn/go-sqlite3, which requires cgo, compilation is done with CGO_ENABLED=1.
The final image contains neither the compiler nor the sources. It usually weighs a few tens of megabytes.
Which package is compiled
- The
mainpackage at the root of the repository, if there is one. - Otherwise a
cmd/<name>folder: preferably the one named after the last segment of the module name, otherwise the first.
With module example.com/shop and the folders cmd/migrate and cmd/shop, ./cmd/shop is compiled. For a different choice, use a Dockerfile.
Files kept next to the binary
Folders read at runtime are copied into the final image if they exist at the root of the repository: templates, static, public, views, web, assets, migrations, locales, i18n, config, resources, db. Any other folder must be embedded in the binary with //go:embed.
Recognized frameworks
net/http, Gin, Echo, Fiber, Chi, Gorilla Mux, Iris, Beego, Buffalo and go-zero are built the same way. For Gin, the image adds GIN_MODE=release.
Hugo is a special case: a Hugo site is generated (hugo --minify --gc) and the public folder is served by Nginx.
Listening on PORT
net/http
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
})
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
srv := &http.Server{Addr: ":" + port, Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, os.Interrupt)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
srv.Shutdown(ctx)
}
":" + port listens on all addresses, which is what you want. "127.0.0.1:" + port or "localhost:" + port make the service unreachable.
Shutting down on SIGTERM lets in-flight requests finish. Webgune waits 30 seconds before forcibly stopping the process: a 25-second timeout leaves some margin.
Gin
package main
import (
"os"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.SetTrustedProxies(nil)
r.GET("/healthz", func(c *gin.Context) { c.String(200, "ok") })
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r.Run("0.0.0.0:" + port)
}
Time zones and outgoing HTTPS
tzdata and ca-certificates are in the image: time.LoadLocation("Europe/Paris") and HTTPS calls to external APIs work with no setup.
Private modules
The service's GOPRIVATE, GOPROXY, GONOSUMDB, GONOPROXY, GOFLAGS and GOINSECURE variables are passed to the dependency download step. A private module also needs authentication with Git, which auto build does not configure. Two solutions:
- commit the
vendor/folder (go mod vendor): the build then has nothing to download; - use a Dockerfile that configures access.
A replace directive pointing to a local folder (=> ../lib) or a vendor/ folder causes the whole repository to be copied before compilation. The build works, but the dependency cache is less effective.
Replacing the command
A Procfile at the root replaces ./server:
web: ./server --config config/prod.yaml
Common errors
| Symptom | Cause | What to do |
|---|---|---|
| "Framework not detected" on a Go repository | No main package at the root or in cmd/ | Move the entry point, or add a Dockerfile |
| Wrong binary compiled | Several folders in cmd/ | Name the folder like the module, or use a Dockerfile |
open templates/index.html: no such file | Folder not in the copied list | Use //go:embed |
go: ... unknown revision or terminal prompts disabled | Private module | Commit vendor/ |
| Service does not respond | Listens on localhost or a fixed port | Read PORT, listen on : + port |
Updated on