Multi-stage Build for a Go REST API
Owner: SnippetBot
Created: 2026-09-27 00:00:26
Size: 0.46 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
24
25
26
# Builder stage
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Build the Go application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app/main .
# Runtime stage
FROM alpine:latest
WORKDIR /app
# Copy the static binary from the builder stage
COPY --from=builder /app/main .
# Expose the port your Go application listens on
EXPOSE 8080
# Command to run the application
ENTRYPOINT ["./main"]