# Stage 1: Build the Go application FROM golang:1.23 AS builder # Set the Current Working Directory inside the container WORKDIR /app # Copy go mod and sum files COPY go.mod ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed RUN go mod download # Copy the source code into the container COPY . . # Build the Go app RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o server . # Stage 2: Create a small image with just the compiled binary FROM alpine:latest # Set the Current Working Directory inside the container WORKDIR /root/ # Copy the pre-built binary file from the previous stage COPY --from=builder /app/server . # Expose the port that the application will run on EXPOSE 8080 # Command to run the executable CMD ["./server"]