-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
47 lines (37 loc) · 1.08 KB
/
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
# ---- Build Stage ----
FROM golang:1.23.2-alpine AS builder
# 1. Install build dependencies as root
RUN apk add --no-cache git \
&& go install github.com/swaggo/swag/cmd/swag@latest
# 2. Create app user and set up directories
RUN addgroup -S app && adduser -S -G app app \
&& mkdir -p /app \
&& chown -R app:app /app \
&& mkdir -p /go-mod-cache \
&& chown -R app:app /go-mod-cache
# 3. Set Go module environment variables
ENV GOMODCACHE=/go-mod-cache \
GOPATH=/go
# 4. Switch to app user
USER app
WORKDIR /app
# 5. Copy dependency files
COPY --chown=app:app go.mod go.sum ./
# 6. Download dependencies
RUN go mod download
# 7. Copy source code
COPY --chown=app:app . .
# 8. Generate Swagger docs
RUN swag init -g cmd/server/main.go -o docs
# 9. Build binary
RUN CGO_ENABLED=0 GOOS=linux \
go build -ldflags="-s -w" -trimpath -o ./main ./cmd/server
# ---- Final Stage ----
FROM alpine:3.21
RUN apk --no-cache add ca-certificates
RUN addgroup -S app && adduser -S -G app app
USER app
WORKDIR /app
COPY --from=builder --chown=app:app /app/main .
EXPOSE 9090
CMD ["./main"]