Skip to content

Commit 8b30782

Browse files
committed
docker: add cann build pipline
1 parent 20b7bf8 commit 8b30782

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

.devops/cann.Dockerfile

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# ==============================================================================
2+
# ARGUMENTS
3+
# ==============================================================================
4+
5+
# 定义CANN基础镜像,方便后续统一更新版本
6+
ARG CANN_BASE_IMAGE=quay.io/ascend/cann:8.1.rc1-910b-openeuler22.03-py3.10
7+
8+
9+
# ==============================================================================
10+
# BUILD STAGE
11+
# 编译所有二进制文件和库
12+
# ==============================================================================
13+
FROM ${CANN_BASE_IMAGE} AS build
14+
15+
# 定义昇腾芯片型号,用于编译。默认为 Ascend910B3
16+
ARG ASCEND_SOC_TYPE=Ascend910B3
17+
18+
# -- 安装构建依赖 --
19+
RUN yum install -y gcc g++ cmake make git libcurl-devel python3 python3-pip && \
20+
yum clean all && \
21+
rm -rf /var/cache/yum
22+
23+
# -- 设置工作目录 --
24+
WORKDIR /app
25+
26+
# -- 拷贝项目文件 --
27+
COPY . .
28+
29+
# -- 设置CANN环境变量 (编译时需要) --
30+
# 相比于 `source`,使用 ENV 可以让环境变量在整个镜像层中持久生效
31+
ENV ASCEND_TOOLKIT_HOME=/usr/local/Ascend/ascend-toolkit/latest
32+
ENV LD_LIBRARY_PATH=${ASCEND_TOOLKIT_HOME}/lib64:${LD_LIBRARY_PATH}
33+
ENV PATH=${ASCEND_TOOLKIT_HOME}/bin:${PATH}
34+
ENV ASCEND_OPP_PATH=${ASCEND_TOOLKIT_HOME}/opp
35+
ENV LD_LIBRARY_PATH=${ASCEND_TOOLKIT_HOME}/runtime/lib64/stub:$LD_LIBRARY_PATH
36+
# ... 您可以根据需要添加原始文件中其他的环境变量 ...
37+
# 为了简洁,这里只列出核心变量,您可以将原始的ENV列表粘贴于此
38+
39+
# -- 编译 llama.cpp --
40+
# 使用传入的 ASCEND_SOC_TYPE 参数,并增加通用编译选项
41+
RUN source /usr/local/Ascend/ascend-toolkit/set_env.sh --force \
42+
&& \
43+
cmake -B build \
44+
-DGGML_CANN=ON \
45+
-DCMAKE_BUILD_TYPE=Release \
46+
-DSOC_TYPE=${ASCEND_SOC_TYPE} \
47+
. && \
48+
cmake --build build --config Release -j$(nproc)
49+
50+
# -- 整理编译产物,方便后续阶段拷贝 --
51+
# 创建一个lib目录存放所有.so文件
52+
RUN mkdir -p /app/lib && \
53+
find build -name "*.so" -exec cp {} /app/lib \;
54+
55+
# 创建一个full目录存放所有可执行文件和Python脚本
56+
RUN mkdir -p /app/full && \
57+
cp build/bin/* /app/full/ && \
58+
cp *.py /app/full/ && \
59+
cp -r gguf-py /app/full/ && \
60+
cp -r requirements /app/full/ && \
61+
cp requirements.txt /app/full/
62+
# 如果您有 tools.sh 脚本,也请确保它在此处被拷贝
63+
# cp .devops/tools.sh /app/full/tools.sh
64+
65+
66+
# ==============================================================================
67+
# BASE STAGE
68+
# 创建一个包含CANN运行时和通用库的最小基础镜像
69+
# ==============================================================================
70+
FROM ${CANN_BASE_IMAGE} AS base
71+
72+
# -- 安装运行时依赖 --
73+
RUN yum install -y libgomp curl && \
74+
yum clean all && \
75+
rm -rf /var/cache/yum
76+
77+
# -- 设置CANN环境变量 (运行时需要) --
78+
ENV ASCEND_TOOLKIT_HOME=/usr/local/Ascend/ascend-toolkit/latest
79+
ENV LD_LIBRARY_PATH=/app:${ASCEND_TOOLKIT_HOME}/lib64:${LD_LIBRARY_PATH}
80+
ENV PATH=${ASCEND_TOOLKIT_HOME}/bin:${PATH}
81+
ENV ASCEND_OPP_PATH=${ASCEND_TOOLKIT_HOME}/opp
82+
# ... 您可以根据需要添加原始文件中其他的环境变量 ...
83+
84+
WORKDIR /app
85+
86+
# 从build阶段拷贝编译好的.so文件
87+
COPY --from=build /app/lib/ /app
88+
89+
90+
# ==============================================================================
91+
# FINAL STAGES (TARGETS)
92+
# ==============================================================================
93+
94+
### Target: full
95+
# 包含所有工具、Python绑定和依赖的完整镜像
96+
# ==============================================================================
97+
FROM base AS full
98+
99+
COPY --from=build /app/full /app
100+
101+
# 安装Python依赖
102+
RUN yum install -y git python3 python3-pip && \
103+
pip3 install --no-cache-dir --upgrade pip setuptools wheel && \
104+
pip3 install --no-cache-dir -r requirements.txt && \
105+
yum clean all && \
106+
rm -rf /var/cache/yum
107+
108+
# 您需要提供一个 tools.sh 脚本作为入口点
109+
ENTRYPOINT ["/app/tools.sh"]
110+
# 如果没有 tools.sh,可以设置默认启动 server
111+
# ENTRYPOINT ["/app/llama-server"]
112+
113+
114+
### Target: light
115+
# 仅包含 llama-cli 的轻量级镜像
116+
# ==============================================================================
117+
FROM base AS light
118+
119+
COPY --from=build /app/full/llama-cli /app
120+
121+
122+
ENTRYPOINT [ "/app/llama-cli" ]
123+
124+
125+
### Target: server
126+
# 仅包含 llama-server 的专用服务器镜像
127+
# ==============================================================================
128+
FROM base AS server
129+
130+
ENV LLAMA_ARG_HOST=0.0.0.0
131+
132+
COPY --from=build /app/full/llama-server /app
133+
134+
135+
HEALTHCHECK CMD [ "curl", "-f", "http://localhost:8080/health" ]
136+
137+
ENTRYPOINT [ "/app/llama-server" ]

0 commit comments

Comments
 (0)