Skip to content

Docker image doesn't run Alembic migrations on startup, causing 500 errors after upgrades #122

@julienma

Description

@julienma

Problem

When upgrading the Docker image between versions that include new migrations (e.g. 0.3.0 → 0.4.0), the app crashes with a 500 error on startup:

sqlite3.OperationalError: no such column: feeds.bucket

The root cause: init_db() uses db.create_all() to initialize the schema, but doesn't record the Alembic state in alembic_version. So when a new version adds a column via a migration, Alembic has no record of where the existing DB stands, and the new column is never added.

The prod-update-code Makefile target correctly runs alembic upgrade head, but the Docker CMD only starts Gunicorn directly — no migration step.

Workaround (for existing installs)

For users running the Docker image who hit this issue, the fix requires exec'ing into the container and manually stamping + upgrading:

# Tell Alembic the current DB state matches the last pre-0.4.0 migration
alembic stamp c5f1b8431345

# Apply new migrations
alembic upgrade head

# If any "already exists" errors (indexes created by db.create_all()):
alembic stamp head

Suggested fix

Run alembic upgrade head before starting Gunicorn in the Dockerfile CMD:

CMD ["sh", "-c", "alembic upgrade head && gunicorn -b 0.0.0.0:9988 --env FLASK_ENV=${FLASK_ENV}"]

And consider removing (or guarding) the db.create_all() call in models.py, since Alembic handles schema creation for fresh installs via migrations.


Disclaimer: I'm not a Flask/Alembic expert — this diagnosis and fix were suggested by an AI assistant. That said, the workaround above worked for me on my own Docker-based install, and the suggested CMD change seems consistent with how the Makefile handles production deployments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions