-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
78 lines (61 loc) · 2.78 KB
/
Copy pathDockerfile
File metadata and controls
78 lines (61 loc) · 2.78 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
78
# syntax=docker/dockerfile:1
# -----------------------------------------------------------------------------
# Builder stage
# -----------------------------------------------------------------------------
# Build all Python dependencies as wheels in a throwaway image. This keeps the
# final runtime image smaller and avoids leaving compilers/build headers behind.
FROM python:3.12-slim AS builder
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1
WORKDIR /build
# gcc/python headers/libpq-dev are needed by native dependencies such as
# asyncpg/psycopg2 when a prebuilt wheel is unavailable for the target platform.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential \
gcc \
libpq-dev \
python3-dev \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
# Pre-build dependency wheels into /wheels. The runner stage installs only from
# this directory, which avoids contacting PyPI during the final image assembly.
RUN python -m pip install --upgrade pip wheel \
&& python -m pip wheel --wheel-dir /wheels -r requirements.txt
# -----------------------------------------------------------------------------
# Runner stage
# -----------------------------------------------------------------------------
# Same Python base as the builder for ABI compatibility with compiled wheels.
FROM python:3.12-slim AS runner
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PATH="/home/appuser/.local/bin:${PATH}"
WORKDIR /srv/liberostack
# libpq5 is the small runtime library used by PostgreSQL drivers. No compilers
# are installed here, reducing both image size and attack surface.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
libpq5 \
&& rm -rf /var/lib/apt/lists/* \
&& addgroup --system appuser \
&& adduser --system --ingroup appuser --home /home/appuser appuser
COPY --from=builder /wheels /wheels
COPY requirements.txt .
# Install dependencies from local wheels only. /wheels is removed afterward so
# the shipped image contains just the installed packages and application code.
RUN python -m pip install --upgrade pip \
&& python -m pip install --no-index --find-links=/wheels -r requirements.txt \
&& rm -rf /wheels
# Copy only runtime application files. Secrets such as .env are intentionally
# not copied into the image; docker-compose injects them at container runtime.
COPY app ./app
COPY migrations ./migrations
COPY alembic.ini ./alembic.ini
USER appuser
EXPOSE 8000
# Production command: no reload, no debug server. Uvicorn binds to all container
# interfaces so Caddy can reverse proxy to the service over Docker networking.
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]