sanbo110 commited on
Commit
75777c4
·
verified ·
1 Parent(s): 4034f5c

Update Dockerfile

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