-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
77 lines (59 loc) · 2.71 KB
/
Dockerfile
File metadata and controls
77 lines (59 loc) · 2.71 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
# syntax=docker/dockerfile:1
# ---------------------------------------------------------------------------
# Build stage: install dependencies into a virtual environment
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS builder
WORKDIR /build
# Install build tools needed by some packages (e.g. asyncpg compiles C ext)
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Copy packaging metadata and source so setuptools can locate the app package
COPY pyproject.toml README.md ./
COPY app/ app/
# Create and populate the venv with runtime dependencies only (no [dev] extras)
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir .
# ---------------------------------------------------------------------------
# Runtime stage: lean image with only what is needed to run the app
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS runtime
# Runtime dependency of asyncpg
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Run as a non-root user for security
RUN useradd --create-home --shell /bin/bash appuser
WORKDIR /app
# Bring in the pre-built venv from the builder stage (all deps + compiled exts)
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy application source and packaging metadata
COPY app/ ./app/
COPY scripts/ ./scripts/
COPY pyproject.toml README.md ./
# Install the package so all sub-packages (app.api, app.core, …) are importable.
# Compiled extensions were already installed in the builder stage; --no-deps avoids
# reinstalling them.
RUN pip install --no-cache-dir --no-build-isolation --no-deps . \
&& chown -R appuser:appuser /app
USER appuser
# Expose the default uvicorn port
EXPOSE 8000
# DATABASE_URL must be supplied at runtime (e.g. via --env or docker-compose).
# The default below points to a service named "db" as used in docker-compose.yml.
ENV DATABASE_URL="postgresql+asyncpg://postgres:postgres@db:5432/commerce_demo" \
API_PREFIX="/api/v1" \
AUTO_CREATE_SCHEMA="true" \
DEFAULT_LIMIT="20" \
MAX_LIMIT="100"
# WEB_CONCURRENCY controls the number of uvicorn worker processes.
# Platforms that auto-detect CPUs will set this automatically;
# fall back to 2 workers when not set.
ENV WEB_CONCURRENCY=2
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
CMD uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers ${WEB_CONCURRENCY}