31 lines
740 B
Docker
31 lines
740 B
Docker
# Stage 1: Build the Go application
|
|
FROM golang:1.22-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy go.mod and go.sum files to download dependencies
|
|
COPY go.mod go.sum ./
|
|
RUN go mod download
|
|
|
|
# Copy the rest of the application source code
|
|
COPY . .
|
|
|
|
# Build the application
|
|
# CGO_ENABLED=0 is important for a clean static build for Alpine
|
|
# -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 lightweight image
|
|
FROM alpine:latest
|
|
|
|
WORKDIR /root/
|
|
|
|
# Copy the built binary from the builder stage
|
|
COPY --from=builder /app/main .
|
|
|
|
# Expose port 8080 (you can change this if your app uses a different port)
|
|
EXPOSE 8080
|
|
|
|
# Command to run the executable
|
|
CMD ["./main"]
|