dalibo / explain.dalibo.com

PEV2 Flask service. Visualizing and understanding PostgreSQL EXPLAIN plans made easy.
PostgreSQL License
44 stars 16 forks source link

How to run migrations in docker? #24

Closed rotten closed 6 months ago

rotten commented 7 months ago

I can bring up the docker container, and it connects happily to my postgresql database via the environment variables.

I can connect to the web page, and in the error logs I can see it connects to the database when I do a submit. However the initial functions and tables are missing from the database. It looks like I need to run alembic upgrade head when I start the container, but unfortunately I can't get that to work.

I connected to the running container with sh and tried running in in /app and in /app/migrations. I don't see any wrapper scripts or other tips for how to run it.

rotten commented 7 months ago

Forgot to include the error when I try to run it:

/app/migrations # alembic upgrade head
  FAILED: No 'script_location' key found in configuration.
pgiraud commented 6 months ago

I suggest you run the upgrade using the following command in the app directory: FLASK_APP=app flask db upgrade

rotten commented 6 months ago

Thank you, that did the trick. FWIW I updated our docker file in our docker-compose based deployment to look like this:

FROM python:3-alpine

ENV FLASK_APP "app"

COPY . /app

WORKDIR /app

# hadolint ignore=DL3013,DL3018
RUN \
    apk add --no-cache postgresql-libs && \
    apk add --no-cache --virtual .build-deps gcc g++ musl-dev postgresql-dev && \
    pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

EXPOSE 5000
CMD ["sh", "-c", "flask db upgrade && exec flask run --host=0.0.0.0"]
rotten commented 6 months ago

Hopefully others who run into this will discover this issue with the solution. We are good now. Thanks again.