# Stage 1: Build the Go binary FROM golang:1.22-alpine AS builder # Set the Current Working Directory inside the container WORKDIR /app # Copy go mod and sum files # COPY go.mod go.sum ./ # RUN go mod download # Copy the source code into the container COPY main.go . # Build the Go app # CGO_ENABLED=0 is needed for a static build # GOOS=linux is to specify the target OS # -a installs all packages to be rebuilt # -installsuffix cgo is used with CGO_ENABLED=0 # -o main specifies the output file name RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . # Stage 2: Create the final, minimal image FROM alpine:latest WORKDIR /root/ # Copy the Pre-built binary file from the previous stage COPY --from=builder /app/main . # Expose port 8080 to the outside world EXPOSE 8080 # Command to run the executable CMD ["./main"]