- Notifications
You must be signed in to change notification settings - Fork 16
Closed
Labels
containersPull requests that update containers codePull requests that update containers codeenhancementNew feature or requestNew feature or requestpythonPull requests that update Python codePull requests that update Python code
Description
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.dbfrom 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 /storagescripts/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 upbuilds 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 downand is restored on nextup docker compose down -vresets the DB, and re-seeds it on next runREADME.mdis updated with Compose usage instructions
Metadata
Metadata
Assignees
Labels
containersPull requests that update containers codePull requests that update containers codeenhancementNew feature or requestNew feature or requestpythonPull requests that update Python codePull requests that update Python code