How do you perform Django database migrations when using Docker-Compose?

后端 未结 7 1095
抹茶落季
抹茶落季 2020-11-28 19:21

I have set up a Docker Django/PostgreSQL app closely following the Django Quick Start instructions on the Docker site.

The first time I run Django\'s manage.py migr

7条回答
  •  生来不讨喜
    2020-11-28 19:43

    I know this is old, and maybe I am missing something here (if so, please enlighten me!), but why not just add the commands to your start.sh script, run by Docker to fire up your instance? It will take only a few extra seconds.

    N.B. I set the DJANGO_SETTINGS_MODULE variable to make sure the correct database is used, as I use different databases for development and production (although I know this is not 'best practice').

    This solved it for me:

    #!/bin/bash
    # Migrate the database first
    echo "Migrating the database before starting the server"
    export DJANGO_SETTINGS_MODULE="edatool.settings.production"
    python manage.py makemigrations
    python manage.py migrate
    # Start Gunicorn processes
    echo "Starting Gunicorn."
    exec gunicorn edatool.wsgi:application \
        --bind 0.0.0.0:8000 \
        --workers 3
    

提交回复
热议问题