|
1 | | -# ------------------------------------------------------------------------------ |
2 | | -# Stage 1: Builder |
3 | | -# This stage builds the application and its dependencies. |
4 | | -# ------------------------------------------------------------------------------ |
5 | | -FROM python:3.13.3-slim-bookworm AS builder |
6 | | -WORKDIR /app |
7 | | - |
8 | | -# Install system build tools for packages with native extensions |
9 | | -RUN apt-get update && \ |
10 | | - apt-get install -y --no-install-recommends build-essential gcc libffi-dev libssl-dev && \ |
11 | | - rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb |
12 | | - |
13 | | -# Pre-build all dependencies into wheels for reproducibility and speed |
14 | | -COPY --chown=root:root --chmod=644 requirements.txt . |
15 | | -RUN pip wheel --no-cache-dir --wheel-dir=/app/wheelhouse -r requirements.txt |
16 | | - |
17 | | -# ------------------------------------------------------------------------------ |
18 | | -# Stage 2: Runtime |
19 | | -# This stage creates the final, minimal image to run the application. |
20 | | -# ------------------------------------------------------------------------------ |
21 | | -FROM python:3.13.3-slim-bookworm AS runtime |
22 | | -WORKDIR /app |
23 | | - |
24 | | -# Metadata labels |
25 | | -LABEL org.opencontainers.image.title="🧪 RESTful API with Python 3 and FastAPI" |
26 | | -LABEL org.opencontainers.image.description="Proof of Concept for a RESTful API made with Python 3 and FastAPI" |
27 | | -LABEL org.opencontainers.image.licenses="MIT" |
28 | | -LABEL org.opencontainers.image.source="https://github.com/nanotaboada/python-samples-fastapi-restful" |
29 | | - |
30 | | -# Copy prebuilt wheels and install dependencies |
31 | | -COPY --chown=root:root --chmod=644 requirements.txt . |
32 | | -COPY --from=builder --chown=root:root --chmod=755 /app/wheelhouse /app/wheelhouse |
33 | | -RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt && \ |
34 | | - rm -rf /app/wheelhouse |
35 | | - |
36 | | -# Copy application code (read-only) |
37 | | -COPY --chown=root:root --chmod=644 main.py ./ |
38 | | -COPY --chown=root:root --chmod=755 database ./database |
39 | | -COPY --chown=root:root --chmod=755 models ./models |
40 | | -COPY --chown=root:root --chmod=755 routes ./routes |
41 | | -COPY --chown=root:root --chmod=755 schemas ./schemas |
42 | | -COPY --chown=root:root --chmod=755 services ./services |
43 | | - |
44 | | -# Copy metadata for GHCR (read-only) |
45 | | -COPY --chown=root:root --chmod=644 README.md ./ |
46 | | -COPY --chown=root:root --chmod=755 assets ./assets |
47 | | - |
48 | | -# Copy entrypoint sctipt and SQLite database |
49 | | -COPY --chown=root:root --chmod=755 scripts/entrypoint.sh ./entrypoint.sh |
50 | | -COPY --chown=root:root --chmod=755 sqlite3-db ./docker-compose |
51 | | - |
52 | | -# Create non-root user and make volume mount point writable |
53 | | -RUN groupadd --system fastapi && \ |
54 | | - adduser --system --ingroup fastapi --disabled-password --gecos '' fastapi && \ |
55 | | - mkdir -p /sqlite3-db && \ |
56 | | - chown fastapi:fastapi /sqlite3-db |
57 | | - |
58 | | -# Drop privileges |
59 | | -USER fastapi |
60 | | - |
61 | | -# Logging output immediately |
62 | | -ENV PYTHONUNBUFFERED=1 |
63 | | - |
64 | | -EXPOSE 9000 |
65 | | - |
66 | | -ENTRYPOINT ["./entrypoint.sh"] |
67 | | -CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"] |
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# Stage 1: Builder |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | + FROM python:3.13.3-slim-bookworm AS builder |
| 5 | + WORKDIR /app |
| 6 | + |
| 7 | + # Install build dependencies |
| 8 | + RUN apt-get update && \ |
| 9 | + apt-get install -y --no-install-recommends \ |
| 10 | + build-essential \ |
| 11 | + gcc \ |
| 12 | + libffi-dev \ |
| 13 | + libssl-dev && \ |
| 14 | + apt-get clean && \ |
| 15 | + rm -rf /var/lib/apt/lists/* |
| 16 | + |
| 17 | + # Copy and pre-build Python dependencies |
| 18 | + COPY requirements.txt . |
| 19 | + RUN pip install --upgrade pip && \ |
| 20 | + pip wheel --no-cache-dir --wheel-dir=/app/wheelhouse -r requirements.txt |
| 21 | + |
| 22 | + # ------------------------------------------------------------------------------ |
| 23 | + # Stage 2: Runtime |
| 24 | + # ------------------------------------------------------------------------------ |
| 25 | + FROM python:3.13.3-slim-bookworm AS runtime |
| 26 | + WORKDIR /app |
| 27 | + |
| 28 | + # Metadata |
| 29 | + LABEL org.opencontainers.image.title="🧪 RESTful API with Python 3 and FastAPI" |
| 30 | + LABEL org.opencontainers.image.description="Proof of Concept for a RESTful API made with Python 3 and FastAPI" |
| 31 | + LABEL org.opencontainers.image.licenses="MIT" |
| 32 | + LABEL org.opencontainers.image.source="https://github.com/nanotaboada/python-samples-fastapi-restful" |
| 33 | + |
| 34 | + # Install runtime dependencies |
| 35 | + COPY requirements.txt . |
| 36 | + COPY --from=builder /app/wheelhouse /app/wheelhouse |
| 37 | + RUN pip install --no-cache-dir --no-index --find-links=/app/wheelhouse -r requirements.txt && \ |
| 38 | + rm -rf /app/wheelhouse |
| 39 | + |
| 40 | + # Copy app code |
| 41 | + COPY main.py . |
| 42 | + COPY database/ ./database/ |
| 43 | + COPY models/ ./models/ |
| 44 | + COPY routes/ ./routes/ |
| 45 | + COPY schemas/ ./schemas/ |
| 46 | + COPY services/ ./services/ |
| 47 | + COPY README.md . |
| 48 | + COPY assets/ ./assets/ |
| 49 | + |
| 50 | + # Copy startup script and SQLite DB seed |
| 51 | + COPY scripts/entrypoint.sh ./entrypoint.sh |
| 52 | + RUN chmod +x ./entrypoint.sh |
| 53 | + COPY sqlite3-db ./docker-compose |
| 54 | + |
| 55 | + # Create non-root user and make volume writable |
| 56 | + RUN groupadd --system fastapi && \ |
| 57 | + useradd --system --gid fastapi --create-home fastapi && \ |
| 58 | + mkdir -p /sqlite3-db && \ |
| 59 | + chown -R fastapi:fastapi /app /sqlite3-db |
| 60 | + |
| 61 | + # Configure environment |
| 62 | + ENV PYTHONUNBUFFERED=1 |
| 63 | + EXPOSE 9000 |
| 64 | + |
| 65 | + ENTRYPOINT ["./entrypoint.sh"] |
| 66 | + CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"] |
| 67 | + |
| 68 | + # 👇 Importante: solo al final |
| 69 | + USER fastapi |
| 70 | + |
0 commit comments