forked from oficcejo/tdx-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
72 lines (54 loc) · 2.01 KB
/
Dockerfile
File metadata and controls
72 lines (54 loc) · 2.01 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
66
67
68
69
70
71
72
# 多阶段构建 - 第一阶段:构建
# 使用官方镜像(如果国内拉取慢,可以配置docker daemon的registry-mirrors)
FROM golang:1.22-alpine AS builder
# 替换Alpine镜像源为阿里云
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置工作目录
WORKDIR /app
# 设置Go代理(使用国内镜像加速)
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy/,direct \
GOTOOLCHAIN=auto \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# 复制 Go 模块文件
COPY go.mod go.sum ./
RUN go mod download
# 复制整个项目的源代码
COPY . .
# 在子 shell 中编译,避免模块路径混淆问题
RUN go mod tidy && (cd web && go build -ldflags="-s -w" -o ../stock-web .)
# 多阶段构建 - 第二阶段:运行
FROM alpine:latest
# 替换Alpine镜像源为阿里云,安装必要的运行时依赖
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk --no-cache add ca-certificates tzdata wget
# 设置时区为上海
ENV TZ=Asia/Shanghai
# 创建非root用户
RUN addgroup -g 1000 appuser && \
adduser -D -u 1000 -G appuser appuser
# 设置工作目录
WORKDIR /app
# ===================================================================
# 【语法修正】
# 从构建阶段复制编译好的二进制文件
COPY --from=builder /app/stock-web .
# ===================================================================
# ===================================================================
# 【语法修正】
# 复制静态文件
COPY --from=builder /app/web/static ./static
# ===================================================================
# 更改文件所有者
RUN chown -R appuser:appuser /app
# 切换到非root用户
USER appuser
# 暴露端口
EXPOSE 8080
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/api/health || exit 1
# 启动应用
CMD ["./stock-web"]