-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
65 lines (46 loc) · 1.51 KB
/
Dockerfile
File metadata and controls
65 lines (46 loc) · 1.51 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 阶段 1: 构建
FROM golang:1.25-alpine AS builder
LABEL stage="builder"
# 设置 Go 模块代理
ENV GOPROXY=https://goproxy.cn,direct
WORKDIR /app
# 安装 git (swag 依赖)
RUN apk add --no-cache git
# 复制 go mod 文件并下载依赖
COPY go.mod go.sum ./
RUN go mod download
# 复制源代码
COPY . .
# 生成 Swagger docs (如果需要)
# RUN swag init -g ./cmd/app/main.go
# 构建应用 (基于 LionChat/Dockerfile)
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/app
# 阶段 2: 运行
FROM alpine:latest
# 安装基础依赖 (基于 LionChat/Dockerfile)
RUN apk --no-cache add ca-certificates tzdata
# 设置时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
# 创建非 root 用户 (基于 LionChat/Dockerfile)
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/main .
# 复制配置文件 (精简)
COPY --from=builder /app/config ./config
# 创建日志目录并设置权限 (基于 LionChat/Dockerfile)
RUN mkdir -p /app/logs/server && \
chown -R appuser:appgroup /app && \
chmod -R 755 /app && \
chmod -R 777 /app/logs
# 切换到非 root 用户
USER appuser
# 暴露端口
EXPOSE 9922
# 设置环境变量 (基于 LionChat/Dockerfile)
ENV GIN_MODE=release
ENV APP_ENV=prod
ENV CONFIG_PATH=./config/config.prod.yaml
# 运行应用
CMD ["./main"]