-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
97 lines (79 loc) · 3.08 KB
/
Dockerfile
File metadata and controls
97 lines (79 loc) · 3.08 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
# 构建阶段(使用完整镜像)
FROM python:3.9-alpine AS builder
WORKDIR /app
# 安装构建依赖
RUN apk add --no-cache --virtual .build-deps \
gcc \
musl-dev \
linux-headers \
binutils \
curl \
tar
# 复制 requirements.txt
COPY requirements.txt .
# 安装Python包(禁用字节码编译以减小体积)
RUN pip install --user --no-cache-dir --no-compile -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 获取最新 ripgrep rg(根据构建平台选择 musl 版本),仅在构建阶段执行
ARG TARGETPLATFORM
RUN set -eux; \
case "$TARGETPLATFORM" in \
"linux/amd64") RG_ARCH="x86_64-unknown-linux-musl" ;; \
"linux/arm64") RG_ARCH="aarch64-unknown-linux-musl" ;; \
*) echo "Unsupported TARGETPLATFORM: $TARGETPLATFORM"; exit 1 ;; \
esac; \
RG_URL=$(curl -s https://api.github.com/repos/BurntSushi/ripgrep/releases/latest \
| grep browser_download_url \
| grep "${RG_ARCH}\.tar\.gz" \
| head -n1 \
| cut -d '"' -f 4); \
curl -L "$RG_URL" -o /tmp/rg.tar.gz; \
mkdir -p /tmp/rgdl; \
tar -xzf /tmp/rg.tar.gz -C /tmp/rgdl; \
mv /tmp/rgdl/ripgrep-*/rg /usr/local/bin/rg; \
chmod +x /usr/local/bin/rg; \
rg --version
# 清理Python非必要内容并优化二进制体积
RUN find /root/.local/lib/python3.9/site-packages -type d -name '__pycache__' -exec rm -rf {} + && \
find /root/.local/lib/python3.9/site-packages -type f -name '*.pyc' -delete && \
find /root/.local/lib/python3.9/site-packages -type d \( -name 'tests' -o -name 'test' -o -name 'testing' -o -name 'docs' -o -name '.pytest_cache' \) -exec rm -rf {} + && \
find /root/.local/lib/python3.9/site-packages -type f -name '*.so' -exec strip --strip-unneeded {} + || true
# 清理构建依赖与临时缓存,减小镜像体积
RUN apk del --purge .build-deps || apk del .build-deps; \
rm -rf /root/.cache /tmp/* /var/cache/apk/*
# 运行阶段(使用更小的基础镜像)
FROM python:3.9-alpine
WORKDIR /app
# 安装运行时依赖
RUN apk add --no-cache \
p7zip \
unzip
# 从构建阶段仅复制必要的Python运行内容
COPY --from=builder /root/.local/lib/python3.9/site-packages /root/.local/lib/python3.9/site-packages
COPY --from=builder /root/.local/bin /root/.local/bin
COPY --from=builder /usr/local/bin/rg /usr/bin/rg
# 确保Python可以找到用户安装的包
ENV PYTHONPATH=/root/.local/lib/python3.9/site-packages
ENV PATH=/root/.local/bin:$PATH
ENV PYTHONDONTWRITEBYTECODE=1
# 创建必要的目录结构
RUN mkdir -p templates exports
# 复制应用代码
COPY main.py .
COPY config.py .
COPY routes.py .
COPY utils.py .
COPY file_handlers.py .
COPY search_engine.py .
COPY process_manager.py .
COPY export_manager.py .
COPY templates/ ./templates/
# 设置环境变量
ENV GUNICORN_CMD_ARGS="-w 1 -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
ENV PYTHONUNBUFFERED="TRUE"
# 验证安装
RUN rg --version && \
python -c "import flask; print('Flask version:', flask.__version__)"
# 暴露端口
EXPOSE 5000
# 运行命令
CMD ["gunicorn", "main:app", "--bind", "0.0.0.0:5000"]