From 461832db4fc1f3cfa2d964ae63e8738a21dbe8a5 Mon Sep 17 00:00:00 2001 From: Nano Taboada Date: Tue, 6 May 2025 15:18:24 -0300 Subject: [PATCH] chore: add .dockerignore to optimize build context --- .dockerignore | 21 +++++++++++++++++++++ Dockerfile | 34 ++++++++++++++++++---------------- README.md | 4 ++-- 3 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e62d337 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +.DS_Store +.git/ +.github/ +.pytest_cache/ +.venv/ +.vscode/ +htmlcov/ +postman-collections/ +.codacy.yml +.coverage +.coveragerc +.flake8 +.gitignore +.pylintrc +CODE_OF_CONDUCT.md +codecov.yml +commitlint.config.mjs +CONTRIBUTING.md +coverage.xml +LICENSE +/tests/ diff --git a/Dockerfile b/Dockerfile index 9eb9911..9282a5d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,35 +1,33 @@ -# - Stage 1 -------------------------------------------------------------------- +# - Stage 1: Build dependencies into wheels ------------------------------------ FROM python:3.12-slim-bookworm AS build WORKDIR /app - # Install build tools needed to compile some Python packages + # Install system build tools needed to compile Python packages with native + # extensions RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential gcc && \ rm -rf /var/lib/apt/lists/* - # Copy and build all required packages (with dependencies) into wheels + # Pre-build all third-party dependencies into wheel files. This enables faster, + # more reliable installation later without relying on network access COPY requirements.txt . - RUN pip wheel --no-cache -r requirements.txt -w /app/wheelhouse + RUN pip wheel --no-cache-dir --wheel-dir=/app/wheelhouse -r requirements.txt - # Copy full app source (not strictly needed in build stage unless building static assets) - COPY . . - -# - Stage 2 -------------------------------------------------------------------- + # - Stage 2: Runtime image ---------------------------------------------------- FROM python:3.12-slim-bookworm AS runtime WORKDIR /app - # Only bring in requirements and prebuilt wheels from build stage + # Install dependencies from prebuilt wheels (no network access) + # This improves build speed and avoids dependency drift COPY requirements.txt . COPY --from=build /app/wheelhouse /app/wheelhouse - - # Install app dependencies from local wheelhouse RUN pip install --no-cache-dir --no-index --find-links /app/wheelhouse -r requirements.txt - # Copy only the necessary runtime source files + # Copy only runtime-relevant application code (excluding tests and tooling) COPY models ./models COPY routes ./routes COPY schemas ./schemas @@ -37,16 +35,20 @@ COPY data ./data COPY main.py . - # Add non-root user for security hardening + # Copy README and assets needed for GHCR package page metadata + COPY README.md ./ + COPY assets ./assets + + # Add a non-root user for better container security RUN adduser --disabled-password --gecos '' fastapi && \ chown -R fastapi:fastapi /app USER fastapi - # Prevent Python from buffering stdout/stderr + # Ensure logs and errors appear in Docker logs immediately ENV PYTHONUNBUFFERED=1 - # Expose FastAPI port + # Expose FastAPI default port EXPOSE 9000 - # Start the FastAPI app with Uvicorn + # Start the FastAPI application using Uvicorn ASGI server CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"] diff --git a/README.md b/README.md index 37c613b..c5ab57e 100644 --- a/README.md +++ b/README.md @@ -48,13 +48,13 @@ This project includes a multi-stage `Dockerfile` for local development and produ ### Build the image ```bash -docker build -t fastapi-app . +docker build -t python-samples-fastapi-restful . ``` ### Run the container ```bash -docker run -p 9000:9000 fastapi-app +docker run -p 9000:9000 python-samples-fastapi-restful:latest ``` ## Credits