Skip to content

Create Dockerfile for the FastAPI application #330

@nanotaboada

Description

@nanotaboada

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

  1. Use an official Python image as the base (python:3.x-slim or similar).
  2. Install the dependencies from requirements.txt only (requirements-lint.txt and requirements-test.txt are not necessary in production)
  3. Copy the entire application code into the container.
  4. Expose port 9000 (configured FastAPI port).
  5. Copy the SQLite database (players-sqlite3.db) from the data folder into the container.
  6. 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.

Metadata

Metadata

Assignees

Labels

containersPull requests that update containers codeenhancementNew feature or requestpythonPull requests that update Python code

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions