Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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/
34 changes: 18 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
# - 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 \

Check warning on line 9 in Dockerfile

View check run for this annotation

Codeac.io / Codeac Code Quality

DL3008

Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
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
COPY services ./services
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"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading