Skip to content

Add Docker Compose support with persistent SQLite volume #360

@nanotaboada

Description

@nanotaboada

Description

Currently, the project uses manual Docker build/run commands and stores the SQLite database inside the container filesystem. This violates container immutability and doesn’t persist data between runs.

Proposed Solution

Introduce Docker Compose to:

  • Automate image building and container orchestration
  • Mount a Docker-managed volume to persist the SQLite database across container restarts
  • Copy a pre-seeded players-sqlite3.db from the image to the volume only on first run

Suggested Implementation

docker-compose.yml

services: api: image: python-samples-fastapi-restful container_name: fastapi-app build: context: . dockerfile: Dockerfile ports: - "9000:9000" volumes: - storage:/storage/ environment: - PYTHONUNBUFFERED=1 - STORAGE_PATH=/storage/players-sqlite3.db restart: unless-stopped volumes: storage:

Dockerfile

# Copy entrypoint sctipt and SQLite database COPY --chown=root:root --chmod=755 scripts/entrypoint.sh ./entrypoint.sh COPY --chown=root:root --chmod=755 storage ./docker-compose # Create non-root user and make volume mount point writable RUN groupadd --system fastapi && \ adduser --system --ingroup fastapi --disabled-password --gecos '' fastapi && \ mkdir -p /storage && \ chown fastapi:fastapi /storage

scripts/entrypoint.sh

#!/bin/bash set -e IMAGE_STORAGE_PATH="/app/docker-compose/players-sqlite3.db" VOLUME_STORAGE_PATH="/storage/players-sqlite3.db" echo "✔ Starting container..." if [ ! -f "$VOLUME_STORAGE_PATH" ]; then echo "⚠️ No existing database file found in volume." if [ -f "$IMAGE_STORAGE_PATH" ]; then echo "Copying database file to writable volume..." cp "$IMAGE_STORAGE_PATH" "$VOLUME_STORAGE_PATH" echo "✔ Database initialized at $VOLUME_STORAGE_PATH" else echo "⚠️ Database file missing at $IMAGE_STORAGE_PATH" exit 1 fi else echo "✔ Existing database file found. Skipping seed copy." fi echo "✔ Ready!" echo "🚀 Launching app..." exec "$@"

Acceptance Criteria

  • docker compose up builds and runs the container with FastAPI app on port 9000
  • SQLite DB is copied to the volume only if not already present
  • Data persists after docker compose down and is restored on next up
  • docker compose down -v resets the DB, and re-seeds it on next run
  • README.md is updated with Compose usage instructions

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