Add multi-stage Dockerfile and .dockerignore

This commit is contained in:
yuqianhe
2026-06-10 02:35:17 +00:00
parent b1867e13c0
commit 19f01a88b4
2 changed files with 51 additions and 0 deletions

32
Dockerfile Normal file
View File

@@ -0,0 +1,32 @@
# ============================================================
# Stage 1: Build
# ============================================================
FROM golang:1.24-alpine AS builder
WORKDIR /build
# 依赖缓存层(变更少,利用 Docker 缓存加速)
COPY go.mod go.sum ./
RUN go mod download
# 编译
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o api-server .
# ============================================================
# Stage 2: Runtime
# ============================================================
FROM alpine:3.21
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
COPY --from=builder /build/api-server .
# ip2region.xdb 运行时数据目录(卷挂载或 COPY
RUN mkdir -p /app/data
EXPOSE 8080
ENTRYPOINT ["./api-server", "serve"]