Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
47 lines (34 loc) · 1.55 KB
/
Copy pathDockerfile
File metadata and controls
47 lines (34 loc) · 1.55 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
# syntax=docker/dockerfile:1
# --------------------------------------------------------------------------- #
# Stage 1: builder — install dependencies into an isolated virtualenv.
# --------------------------------------------------------------------------- #
FROM python:3.11-slim AS builder
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
UV_PROJECT_ENVIRONMENT=/opt/venv
RUN pip install --no-cache-dir uv
WORKDIR /build
# Copy only what's needed to resolve/install deps first (better layer caching).
COPY pyproject.toml README.md ./
COPY app ./app
# Create a self-contained venv with the project + runtime deps installed.
RUN uv venv /opt/venv && uv pip install --python /opt/venv/bin/python --no-cache .
# --------------------------------------------------------------------------- #
# Stage 2: runtime — copy the venv only; no build tooling in the final image.
# --------------------------------------------------------------------------- #
FROM python:3.11-slim AS runtime
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PATH="/opt/venv/bin:$PATH"
# Run as a non-root user.
RUN useradd --create-home --uid 10001 appuser
WORKDIR /srv
COPY --from=builder /opt/venv /opt/venv
COPY app ./app
COPY README.md ./
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:8000/api/v1/health').status==200 else 1)"
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]