更新 docker 配置,替换pip为uv 加速构建

This commit is contained in:
zcr
2025-12-23 15:18:36 +08:00
parent 9fbfe9c674
commit aa1b087090
2 changed files with 31 additions and 34 deletions

View File

@@ -1,40 +1,37 @@
# Change CUDA and cuDNN version here
FROM ghcr.io/astral-sh/uv:latest AS uv_bin
FROM nvidia/cuda:12.4.1-base-ubuntu22.04
ARG PYTHON_VERSION=3.11
ENV DEBIAN_FRONTEND=noninteractive
# 1. 基础环境配置
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
PYTHONUNBUFFERED=1 \
# 核心:强制 uv 把虚拟环境建在 /app/.venv
UV_PROJECT_ENVIRONMENT=/app/.venv
COPY --from=uv_bin /uv /uvx /bin/
# 2. 安装系统依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
software-properties-common \
wget \
&& add-apt-repository ppa:deadsnakes/ppa \
&& apt-get update && apt-get install -y --no-install-recommends \
python$PYTHON_VERSION \
python$PYTHON_VERSION-dev \
python$PYTHON_VERSION-venv \
&& wget https://bootstrap.pypa.io/get-pip.py -O get-pip.py \
&& python$PYTHON_VERSION get-pip.py \
&& rm get-pip.py \
&& ln -sf /usr/bin/python$PYTHON_VERSION /usr/bin/python \
&& ln -sf /usr/local/bin/pip$PYTHON_VERSION /usr/local/bin/pip \
&& python --version \
&& pip --version \
&& apt-get purge -y --auto-remove software-properties-common \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
####### Add your own installation commands here #######
# RUN pip install some-package
# RUN wget https://path/to/some/data/or/weights
RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y \
&& apt install -y build-essential g++
ffmpeg libsm6 libxext6 build-essential g++ \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . /app
# Install litserve and requirements
RUN pip install --no-cache-dir litserve==0.2.16
RUN pip install -r requirements-GPU.txt
RUN pip install opencv-python
# 3. 安装依赖 (不加 --system让 uv 创建受管的虚拟环境)
# 这里会根据 pyproject.toml 自动下载并安装 Python 3.11
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project --python 3.11
# 4. 拷贝项目文件并安装项目本身
COPY . .
RUN uv sync --frozen --no-dev --python 3.11
# 5. 【最关键】将虚拟环境的 bin 目录提到最前面
# 注意uv sync 创建的 python 就在这个目录下
ENV PATH="/app/.venv/bin:$PATH"
EXPOSE 8000
CMD ["python", "litserver_main.py"]
#CMD ["tail", "-f","/dev/null"]
# 验证路径并运行
# 此时运行 python 实际上是运行 /app/.venv/bin/python
CMD ["uv", "run","litserver_main.py"]

View File

@@ -13,6 +13,6 @@ services:
reservations:
devices:
- driver: nvidia
device_ids: [ '1' ]
device_ids: [ '0' ]
capabilities:
- gpu