Created
April 14, 2022 23:45
-
-
Save mirusky/bf6d03207b3f0d78b6b9f3f681ac4ee4 to your computer and use it in GitHub Desktop.
Golang Multi-Stage Build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################ | |
# STEP 1 build static binary | |
############################ | |
FROM golang:1.18.1-alpine3.15 as builder | |
WORKDIR /workspace | |
# Fetch dependencies. | |
COPY go.mod . | |
COPY go.sum . | |
RUN go mod download | |
COPY . . | |
# Build static binary | |
# CGO_ENABLED=0 to create a static executable | |
# -ldflags="-w -s" to shrink golang executable | |
# -trimpath to remove workspace path from debug | |
# -o bin output | |
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /app/api | |
############################ | |
# STEP 2 build a small image | |
############################ | |
FROM scratch | |
# Copy our static executable. | |
COPY --from=builder /app/api /app/api | |
# Run it | |
ENTRYPOINT ["/app/api"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment