-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
45 lines (31 loc) · 1020 Bytes
/
Copy pathDockerfile
File metadata and controls
45 lines (31 loc) · 1020 Bytes
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
# 第一阶段:构建Go应用
FROM golang:1.24.3-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制go.mod和go.sum文件并下载依赖
COPY go.mod go.sum ./
RUN go mod download
# 复制所有源代码
COPY . .
# 构建应用(关闭CGO以确保静态链接)
RUN CGO_ENABLED=0 GOOS=linux go build -o coder_edu_backend .
# 第二阶段:创建轻量级镜像
FROM alpine:3.19
# 安装必要的包
RUN apk --no-cache add ca-certificates
# 创建非root用户运行应用
RUN adduser -D appuser
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/coder_edu_backend .
# 复制不含密钥的配置模板,实际敏感值通过环境变量注入
COPY configs/config.yaml.example ./configs/config.yaml
# 创建运行时所需目录并设置权限
RUN mkdir -p uploads resource_file logs && chown -R appuser:appuser uploads resource_file logs
# 切换到非root用户
USER appuser
# 暴露端口
EXPOSE 8080
# 启动应用
CMD ["./coder_edu_backend"]