This document describes the SQLite service configuration for the PyNewsServer project.
The SQLite service is configured as part of the Docker Compose setup to provide persistent database storage for the application.
- sqlite-init: An initialization service that creates the SQLite database file and sets proper permissions
- pynews-api: The main application service that connects to the SQLite database
- sqlite_data volume: A bind mount volume that maps to
./datadirectory for data persistence
The following environment variables are used for SQLite configuration:
SQLITE_PATH: Path to the SQLite database file (default:/app/data/pynewsdb.db)SQLITE_URL: SQLAlchemy connection URL (default:sqlite+aiosqlite:///app/data/pynewsdb.db)
- Host Path:
./data - Container Path:
/app/data - Database File:
pynewsdb.db
This service:
- Creates the data directory if it doesn't exist
- Creates an empty SQLite database file
- Sets proper file permissions (664)
- Sets proper ownership (1000:1000)
- Runs only once and exits
The main application service:
- Depends on
sqlite-initto ensure database initialization - Mounts the SQLite data volume
- Uses async SQLite operations via
aiosqlite - Automatically creates tables on first run
docker-compose up -dThis will:
- Start the
sqlite-initservice to initialize the database - Start the
pynews-apiservice after initialization is complete
The SQLite database file is located at:
- Host:
./data/pynewsdb.db - Container:
/app/data/pynewsdb.db
The application uses SQLModel with async SQLAlchemy for database operations:
- Connection: Async SQLite with
aiosqlite - ORM: SQLModel (built on SQLAlchemy)
- Sessions: Async session management
- Migrations: Automatic table creation via SQLModel metadata
Database data is persisted in the ./data directory on the host system. This directory is:
- Created automatically by the services
- Excluded from Git via
.gitignore - Bound to the container's
/app/datadirectory
# Copy database file
cp ./data/pynewsdb.db ./data/pynewsdb.db.backup
# Or use SQLite dump
sqlite3 ./data/pynewsdb.db .dump > backup.sql# Restore from backup file
cp ./data/pynewsdb.db.backup ./data/pynewsdb.db
# Or restore from SQL dump
sqlite3 ./data/pynewsdb.db < backup.sqlIf you encounter permission issues:
# Fix ownership
sudo chown 1000:1000 ./data/pynewsdb.db
# Fix permissions
chmod 664 ./data/pynewsdb.dbIf the database becomes corrupted:
# Remove corrupted database
rm ./data/pynewsdb.db
# Restart services to recreate
docker-compose restartIf volume mounting fails:
# Ensure data directory exists
mkdir -p ./data
# Check Docker permissions
ls -la ./data/