-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
49 lines (35 loc) · 1.64 KB
/
Dockerfile
File metadata and controls
49 lines (35 loc) · 1.64 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
# Multi-stage build для оптимизации размера образа
# Stage 1: Builder
FROM python:3.11-slim as builder
WORKDIR /app
# Установка зависимостей для сборки
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Копирование и установка Python зависимостей
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
pip wheel --no-cache-dir --wheel-dir /app/wheels -r requirements.txt
# Stage 2: Production
FROM python:3.11-slim
WORKDIR /app
# Создание непривилегированного пользователя
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
# Копирование wheels из builder
COPY --from=builder /app/wheels /wheels
COPY --from=builder /app/requirements.txt .
# Установка зависимостей
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir --no-index --find-links=/wheels -r requirements.txt && \
rm -rf /wheels
# Копирование кода приложения
COPY --chown=appuser:appuser . .
# Переключение на непривилегированного пользователя
USER appuser
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Открытие порта
EXPOSE 8000
# Команда запуска
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips=*"]