-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile.backend
More file actions
113 lines (92 loc) · 2.72 KB
/
Dockerfile.backend
File metadata and controls
113 lines (92 loc) · 2.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# ITSM Backend Dockerfile
# 多阶段构建,最小化镜像体积
# ============================================
# 第一阶段:构建依赖(builder)
# ============================================
FROM golang:1.25-alpine AS builder
# 设置工作目录
WORKDIR /app
# 安装构建依赖
RUN apk add --no-cache \
gcc \
musl-dev \
make \
git \
bash \
ca-certificates \
tzdata
# 复制依赖文件
COPY go.mod go.sum ./
COPY internal/ ./internal/
COPY pkg/ ./pkg/
COPY cmd/ ./cmd/
# 下载依赖(使用缓存层)
RUN go mod download && go mod verify
# 复制全部源码
COPY . .
# 构建参数(可通过 --build-arg 传入)
ARG GIT_COMMIT=unknown
ARG BUILD_DATE=unknown
ARG VERSION=dev
# 设置编译参数
ENV CGO_ENABLED=1 \
GOOS=linux \
GOARCH=amd64
# 构建二进制文件(带调试信息用于符号化)
RUN go build \
-ldflags="-X main.GitCommit=${GIT_COMMIT} \
-X main.BuildDate=${BUILD_DATE} \
-X main.Version=${VERSION} \
-s -w" \
-o bin/itsm \
./cmd/main.go
# ============================================
# 第二阶段:运行时镜像(runtime)
# ============================================
FROM alpine:3.19 AS runtime
# 添加元数据
LABEL maintainer="ITSM Team <dev@heidsoft.com>"
LABEL description="ITSM Backend Service"
LABEL org.opencontainers.image.title="itsm-backend"
LABEL org.opencontainers.image.description="IT Service Management Backend"
LABEL org.opencontainers.image.vendor="Heidsoft"
# 安装运行时依赖
RUN apk add --no-cache \
ca-certificates \
tzdata \
curl \
wget \
bash \
&& update-ca-certificates
# 创建非 root 用户
RUN addgroup -g 1001 -S itsm && \
adduser -u 1001 -S itsm -G itsm
# 设置工作目录
WORKDIR /app
# 从 builder 阶段复制二进制文件
COPY --from=builder --chown=itsm:itsm /app/bin/itsm /app/itsm
COPY --from=builder --chown=itsm:itsm /app/.env.example /app/.env.example
COPY --from=builder --chown=itsm:itsm /app/config /app/config:ro
COPY --from=builder --chown=itsm:itsm /app/migrations /app/migrations:ro
COPY --from=builder --chown=itsm:itsm /app/docs /app/docs:ro
# 创建必要的目录
RUN mkdir -p /app/logs /app/uploads /app/tmp && \
chown -R itsm:itsm /app
# 切换到非 root 用户
USER itsm
# 暴露端口
EXPOSE 8080
EXPOSE 9090
# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# 环境变量
ENV \
PORT=8080 \
GIN_MODE=release \
SERVER_ENV=production \
LOG_FORMAT=json \
LOG_LEVEL=error
# 启动命令
ENTRYPOINT ["/app/itsm"]
CMD ["serve"] # 默认运行 serve 命令(开发可用 air)