2026-02-03 23:45:27 +08:00
|
|
|
# 第一阶段:构建
|
2026-02-03 23:54:40 +08:00
|
|
|
FROM golang:1.24-alpine AS builder
|
2026-02-03 23:45:27 +08:00
|
|
|
|
|
|
|
|
WORKDIR /app
|
2026-02-04 00:11:24 +08:00
|
|
|
RUN apk add --no-cache gcc musl-dev # 仅当需要 CGO 时才保留(你的代码可能不需要,但保留无害)
|
2026-02-03 23:45:27 +08:00
|
|
|
|
|
|
|
|
COPY go.mod go.sum ./
|
|
|
|
|
RUN go mod download
|
|
|
|
|
|
|
|
|
|
COPY . .
|
2026-02-03 23:54:40 +08:00
|
|
|
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o server ./cmd/server/main.go
|
|
|
|
|
|
2026-02-03 23:45:27 +08:00
|
|
|
# 第二阶段:运行
|
|
|
|
|
FROM alpine:latest
|
2026-02-04 00:11:24 +08:00
|
|
|
RUN addgroup -g 1001 -S app && adduser -u 1001 -S app -G app
|
2026-02-03 23:45:27 +08:00
|
|
|
WORKDIR /app
|
|
|
|
|
COPY --from=builder /app/server .
|
|
|
|
|
COPY --from=builder /app/web ./web
|
2026-02-03 23:54:40 +08:00
|
|
|
RUN chown -R app:app /app
|
|
|
|
|
USER app
|
2026-02-03 23:45:27 +08:00
|
|
|
EXPOSE 2779
|
|
|
|
|
ENV PORT=2779
|
2026-02-03 23:54:40 +08:00
|
|
|
CMD ["./server"]
|