-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerfile
More file actions
100 lines (84 loc) · 3.29 KB
/
Copy pathdockerfile
File metadata and controls
100 lines (84 loc) · 3.29 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
# docker build -t zimage-fastapi:v0.1 .
# docker run --rm --gpus all zimage-fastapi:v0.1 python -c "import torch; print(f'CUDA Available: {torch.cuda.is_available()}, Count: {torch.cuda.device_count()}, {torch.cuda.get_device_name(0)}')"
# docker run --rm -it --gpus all zimage-fastapi:v0.1 bash # 会覆盖容器中的 CMD
# docker run --rm --gpus all -e ZIMAGE_API_KEY="tmp-zimg-key" zimage-fastapi:v0.1
# ============================
# 基础镜像(CUDA + PyTorch)
# 可以先提前 pull 基础镜像以加快构建速度
# ============================
FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04
# FROM nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04
# FROM nvidia/cuda:13.0.0-cudnn-runtime-ubuntu22.04
# FROM library/alpine:latest
# 设置工作目录
WORKDIR /app
# ============================
# 设置环境变量
# ============================
# 将通用的、不常变动的环境变量放在一起
ENV DEBIAN_FRONTEND=noninteractive \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
TZ=Asia/Shanghai
# 将与业务逻辑或环境相关的变量放在一起
# 强制让 Python 知道这是生产环境
ENV APP_ENV=production \
APP_USER=zuser \
WATCHDOG_USEC=""
# 服务端口(统一配置)
ENV PORT=8003
ENV PATH="/home/${APP_USER}/.local/bin:/usr/local/bin:${PATH}"
# ============================
# 系统依赖
# ============================
RUN apt-get update && apt-get install -y ca-certificates && \
sed -i 's|http://.*.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list && \
sed -i 's|http://security.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y --no-install-recommends \
python3.10 \
python3-pip \
python3-dev \
curl \
vim \
ranger \
# wget \
# ncdu \
# git \
# net-tools \
# libgl1-mesa-glx \
# libglib2.0-0 \
# ffmpeg \
&& ln -s /usr/bin/python3 /usr/bin/python \
&& rm -rf /var/lib/apt/lists/*
# ============================
# 创建非 root 用户(对应 systemd User)
# ============================
RUN groupadd -g 1000 ${APP_USER} && \
useradd -m -u 1000 -g ${APP_USER} -s /bin/bash ${APP_USER} && \
chown -R ${APP_USER}:${APP_USER} /app
RUN echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple \
\ntrusted-host = pypi.tuna.tsinghua.edu.cn" > /etc/pip.conf && \
pip install --upgrade pip setuptools wheel
# ============================
# 安装 Python 依赖
# ============================
RUN pip install torch==2.6.0 torchvision --index-url https://download.pytorch.org/whl/cu124
# 确保拷贝进去的文件属于 zuser
COPY --chown=${APP_USER}:${APP_USER} . .
RUN python -m pip install -e ./LTX-Video[inference]
COPY --chown=${APP_USER}:${APP_USER} requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# 切换到非 root 用户 (后续 CMD 将以 zuser 运行)
USER ${APP_USER}
# ============================
# 暴露端口
# ============================
EXPOSE ${PORT}
HEALTHCHECK --interval=180s --timeout=10s --retries=3 --start-period=300s \
CMD curl -f http://localhost:${PORT}/health || exit 1
# ============================
# 启动命令(等价于 ExecStart)
# -u (Unbuffered) 确保日志实时输出
# ============================
CMD ["python3", "-u", "run_server.py"]