diff --git a/cli/htmgo/tasks/run/build.go b/cli/htmgo/tasks/run/build.go index 802a261..c20c28a 100644 --- a/cli/htmgo/tasks/run/build.go +++ b/cli/htmgo/tasks/run/build.go @@ -6,6 +6,7 @@ import ( "github.com/maddalax/htmgo/cli/htmgo/tasks/copyassets" "github.com/maddalax/htmgo/cli/htmgo/tasks/css" "github.com/maddalax/htmgo/cli/htmgo/tasks/process" + "os" ) func Build() { @@ -19,8 +20,13 @@ func Build() { process.RunOrExit("rm -rf ./dist") process.RunOrExit("mkdir -p ./dist") - process.RunOrExit("env go build -o ./dist .") - process.RunOrExit("go build -o ./dist/app .") + flags := "" + if os.Getenv("CGO_ENABLED") == "1" { + flags = `-a -ldflags '-linkmode external -extldflags "-static"' ` + } + + process.RunOrExit(fmt.Sprintf("go build -o ./dist/app %s.", flags)) + process.RunOrExit(fmt.Sprintf("go build -o ./dist/app %s.", flags)) fmt.Printf("Executable built at %s\n", process.GetPathRelativeToCwd("dist")) } diff --git a/examples/todo-list/Dockerfile b/examples/todo-list/Dockerfile index dd27055..715980e 100644 --- a/examples/todo-list/Dockerfile +++ b/examples/todo-list/Dockerfile @@ -1,9 +1,5 @@ # Stage 1: Build the Go binary -FROM golang:1.23-alpine AS builder - -RUN apk update -RUN apk add git -RUN apk add curl +FROM golang:1.23 AS builder # Set the working directory inside the container WORKDIR /app @@ -20,9 +16,7 @@ RUN go get github.com/maddalax/htmgo/framework@latest # Copy the source code into the container COPY . . -# Build the Go binary for Linux -RUN GOPRIVATE=github.com/maddalax LOG_LEVEL=debug go run github.com/maddalax/htmgo/cli/htmgo@latest build - +RUN CGO_ENABLED=1 GOPRIVATE=github.com/maddalax LOG_LEVEL=debug go run github.com/maddalax/htmgo/cli/htmgo@latest build # Stage 2: Create the smallest possible image FROM gcr.io/distroless/base-debian11