Spaces:
Running
Running
Update Dockerfile
Browse files- Dockerfile +20 -25
Dockerfile
CHANGED
@@ -1,35 +1,30 @@
|
|
1 |
-
#
|
2 |
-
FROM python:3.
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
python3-dev \
|
9 |
-
&& rm -rf /var/lib/apt/lists/*
|
10 |
|
11 |
-
#
|
12 |
-
RUN
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
#
|
|
|
21 |
USER appuser
|
22 |
-
WORKDIR /app
|
23 |
-
|
24 |
-
# 安装Python依赖
|
25 |
-
COPY requirements.txt .
|
26 |
-
RUN pip install --user --no-cache-dir -r requirements.txt
|
27 |
|
28 |
# 复制应用代码
|
29 |
COPY app.py .
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
CMD ["
|
|
|
1 |
+
# 使用轻量级基础镜像
|
2 |
+
FROM python:3.10-slim-bookworm
|
3 |
|
4 |
+
# 设置容器级环境变量
|
5 |
+
ENV TRANSFORMERS_CACHE=/model-cache \
|
6 |
+
HF_HOME=/model-cache \
|
7 |
+
PYTHONUNBUFFERED=1
|
|
|
|
|
8 |
|
9 |
+
# 安装必需的系统组件(保持root权限)
|
10 |
+
RUN apt-get update && \
|
11 |
+
apt-get install -y --no-install-recommends gcc python3-dev && \
|
12 |
+
rm -rf /var/lib/apt/lists/*
|
13 |
|
14 |
+
# 安装Python依赖(自动清理缓存)
|
15 |
+
COPY requirements.txt .
|
16 |
+
RUN pip install --upgrade pip && \
|
17 |
+
pip install --no-cache-dir -r requirements.txt
|
18 |
|
19 |
+
# 切换到非root用户
|
20 |
+
RUN useradd -m appuser && chown -R appuser /model-cache
|
21 |
USER appuser
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# 复制应用代码
|
24 |
COPY app.py .
|
25 |
|
26 |
+
# 暴露API端口
|
27 |
+
EXPOSE 8080
|
28 |
|
29 |
+
# 启动命令(生产级WSGI服务器)
|
30 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "--timeout", "120", "app:app"]
|