sanbo110 commited on
Commit
7000a10
·
verified ·
1 Parent(s): 8a6c45b

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +26 -12
Dockerfile CHANGED
@@ -1,28 +1,42 @@
1
- FROM golang:1.22 AS builder
2
-
3
- ENV CGO_ENABLED=0
4
 
 
5
  WORKDIR /app
6
 
7
- RUN go mod tidy
8
  COPY go.mod go.sum ./
9
  RUN go mod download
10
 
 
11
  COPY . .
12
- RUN CGO_ENABLED=0 GOARCH=amd64 go build -o test .
13
 
14
- FROM alpine:latest
 
 
 
 
15
 
 
16
  WORKDIR /app
17
 
18
- COPY --from=builder /app/test .
 
19
 
20
- ENV MAX_RETRY_COUNT="3"
21
- ENV RETRY_DELAY="5000"
22
- ENV PORT="7860"
 
23
 
 
24
  EXPOSE 7860
25
 
26
- RUN chmod +x test
 
 
 
 
 
27
 
28
- CMD ["./test"]
 
 
1
+ # 构建阶段
2
+ FROM golang:1.22-alpine AS builder
 
3
 
4
+ # 设置工作目录
5
  WORKDIR /app
6
 
7
+ # 为了减少Docker层数,先复制 go.mod 和 go.sum 并下载依赖
8
  COPY go.mod go.sum ./
9
  RUN go mod download
10
 
11
+ # 复制源代码
12
  COPY . .
 
13
 
14
+ # 编译Go应用,启用优化参数
15
+ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ddg .
16
+
17
+ # 运行阶段
18
+ FROM alpine:3.18
19
 
20
+ # 设置工作目录
21
  WORKDIR /app
22
 
23
+ # 将编译好的二进制文件从构建阶段复制过来
24
+ COPY --from=builder /app/ddg .
25
 
26
+ # 设置环境变量
27
+ ENV MAX_RETRY_COUNT=3
28
+ ENV RETRY_DELAY=5000
29
+ ENV PORT=7860
30
 
31
+ # 公开端口
32
  EXPOSE 7860
33
 
34
+ # 确保二进制文件具有可执行权限
35
+ RUN chmod +x ddg
36
+
37
+ # 使用非 root 用户运行应用(增强安全性)
38
+ RUN addgroup -S appgroup && adduser -S appuser -G appgroup
39
+ USER appuser
40
 
41
+ # 启动应用
42
+ ENTRYPOINT ["./ddg"]