Docker: python:3.12-slim, CPU-torch, веса модели в образе, compose на порт 8011

This commit is contained in:
2026-10-07 13:54:40 +03:00
parent d448884294
commit 7ee49e8c9f
3 changed files with 67 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
.git
.venv
.cache
__pycache__
.pytest_cache
tests
docs
*.md
.env
.env.example
docker-compose*.yml
Dockerfile
+42
View File
@@ -0,0 +1,42 @@
# Multi-stage: builder с зависимостями -> тонкий runtime-слой.
# Цель: CPU-инференс на VPS Ubuntu; torch ставится строго CPU-сборкой.
FROM python:3.12-slim AS builder
ENV PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
WORKDIR /build
COPY requirements.txt .
# torch/torchvision первыми и только из CPU-индекса PyTorch,
# затем остальное из PyPI (torch уже удовлетворён и не переустанавливается)
RUN pip install --prefix=/install torch torchvision \
--index-url https://download.pytorch.org/whl/cpu \
&& pip install --prefix=/install -r requirements.txt
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PORT=8000 \
WORKERS=1
RUN useradd --create-home --shell /usr/sbin/nologin appuser
WORKDIR /srv
COPY --from=builder /install /usr/local
COPY app ./app
USER appuser
# Кэш torch.hub (код dinov2 + веса ~84 МБ) запекаем в образ:
# контейнер стартует мгновенно и не зависит от github/fbaipublicfiles
ENV TORCH_HOME=/srv/.cache/torch
RUN python -c "from app.dino.model import DinoEmbedder; DinoEmbedder('dinov2_vits14', 'cpu', 224).load()"
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:' + __import__('os').environ.get('PORT', '8000') + '/health', timeout=3)"
ENTRYPOINT ["uvicorn", "app.main:create_app", "--factory", "--no-access-log"]
CMD ["--host", "0.0.0.0", "--port", "8000"]
+13
View File
@@ -0,0 +1,13 @@
services:
dino-service:
build: .
image: dino-v2:latest
ports:
- "8011:8000"
env_file: .env
environment:
PORT: "8000"
WORKERS: "1"
restart: unless-stopped
# Stateless: реплики можно масштабировать за балансировщиком,
# но каждая держит свою копию модели в RAM (~0.5 ГБ)