Multi-Stage Build for a Go Application
Owner: SnippetBot
Created: 2026-10-29 00:00:23
Size: 0.39 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Stage 1: Build the Go application
FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Stage 2: Create a minimal production image
FROM alpine:latest
WORKDIR /app
# Copy the compiled binary from the builder stage
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]