Description
We need to create a Dockerfile to containerize the FastAPI application for easier deployment and testing. The goal is to create an efficient image that includes all dependencies, prepares the environment, and exposes the necessary ports for FastAPI to run inside a container.
Suggested Approach
- Use an official Python image as the base (
python:3.x-slim or similar).
- Install the dependencies from
requirements.txt only (requirements-lint.txt and requirements-test.txt are not necessary in production)
- Copy the entire application code into the container.
- Expose port
9000 (configured FastAPI port).
- Copy the SQLite database (
players-sqlite3.db) from the data folder into the container.
- Use
uvicorn to start the FastAPI application on port 9000.
Suggested Implementation
# Stage 1: Build
FROM python:3.12-slim-bookworm AS build
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Stage 2: Runtime
FROM python:3.12-slim-bookworm AS runtime
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY models ./models
COPY routes ./routes
COPY schemas ./schemas
COPY services ./services
COPY data ./data
COPY main.py .
# Add non-root 'fastapi' user (optional for hardening)
RUN adduser --disabled-password --gecos '' fastapi \
&& chown -R fastapi:fastapi /app
USER fastapi
EXPOSE 9000
ENV PYTHONUNBUFFERED=1
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "9000"]
Acceptance Criteria
- Docker image builds without errors.
- The SQLite database file (
players-sqlite3.db) is properly copied into the container and available to the app.
- The container runs the FastAPI app on port
9000 and is accessible at http://localhost:9000.
Description
We need to create a
Dockerfileto containerize the FastAPI application for easier deployment and testing. The goal is to create an efficient image that includes all dependencies, prepares the environment, and exposes the necessary ports for FastAPI to run inside a container.Suggested Approach
python:3.x-slimor similar).requirements.txtonly (requirements-lint.txtandrequirements-test.txtare not necessary in production)9000(configured FastAPI port).players-sqlite3.db) from thedatafolder into the container.uvicornto start the FastAPI application on port9000.Suggested Implementation
Acceptance Criteria
players-sqlite3.db) is properly copied into the container and available to the app.9000and is accessible athttp://localhost:9000.