-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgolang-multi-stage.after.Dockerfile
49 lines (35 loc) · 1.15 KB
/
golang-multi-stage.after.Dockerfile
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Created from patterns seen at https://github.com/AlphaWong/go-test-multi-stage-build
# This Dockerfile demonstrates a multi-stage Go build with package optimization
# Build stage
FROM cgr.dev/ORG/golang:1.20-dev AS builder
USER root
WORKDIR /app
# Copy go.mod and go.sum files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
# Install UPX for binary compression (optional)
RUN apk add --no-cache wget xz
RUN wget -P /tmp/ https://github.com/upx/upx/releases/download/v3.95/upx-3.95-amd64_linux.tar.xz
RUN tar -x -v -C /tmp -f /tmp/upx-3.95-amd64_linux.tar.xz
RUN mv /tmp/upx-3.95-amd64_linux/upx /usr/local/bin/upx
# Compress the binary to reduce size
RUN upx --ultra-brute -qq app && \
upx -t app
# Final stage
FROM cgr.dev/ORG/alpine:3.17-dev
USER root
# Install any runtime dependencies
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
# Copy the binary from the builder stage
COPY --from=builder /app/app .
# Create a non-root user
RUN adduser -D appuser
USER appuser
# Command to run
ENTRYPOINT ["/app/app"]