Add multi-stage Dockerfile and .dockerignore
This commit is contained in:
19
.dockerignore
Normal file
19
.dockerignore
Normal file
@@ -0,0 +1,19 @@
|
||||
# Binary
|
||||
api-server
|
||||
|
||||
# Git
|
||||
.git/
|
||||
.gitignore
|
||||
.dockerignore
|
||||
|
||||
# Runtime data (mounted at runtime)
|
||||
data/
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
|
||||
# Docker
|
||||
Dockerfile
|
||||
docker-compose*.yml
|
||||
32
Dockerfile
Normal file
32
Dockerfile
Normal 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"]
|
||||
Reference in New Issue
Block a user