How do I run migrations in Dockerized Django?

血红的双手。 提交于 2019-12-06 08:22:10

There are many ways how to achieve this.

1) Run ./manage.py migrate before you start your app (uwsgi, runserver,...) in bash script

Dockerfile

FROM debian:latest

...

# entrypoint, must be executable file chmod +x entrypoint.sh
COPY entrypoint.sh /home/docker/entrypoint.sh

# what happens when I start the container
CMD ["/home/docker/entrypoint.sh"]

entrypoint.sh

#!/bin/bash

./manage.py collectstatic --noinput
# i commit my migration files to git so i dont need to run it on server
# ./manage.py makemigrations app_name
./manage.py migrate

# here it start nginx and the uwsgi
supervisord -c /etc/supervisor/supervisord.conf -n

2) If you have a lot of migration files and you dont want any downtime, you could run the migrate command from seperate docker-compose service

docker-compose.yml

version: '3.3'

services:  

  # starts the supervisor (uwsgi + nginx)
  web:
    build: .
    ports: ["80:80"]

  # this service will use same image, and once the migration is done it will be stopped
  web_migrations:
    build: .
    command: ./manage.py migrate

I solved this by doing:

docker-compose exec web /usr/local/bin/python manage.py makemigrations todo

and then :

docker-compose exec web /usr/local/bin/python manage.py migrate 

I got it from this issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!