How do you perform Django database migrations when

2019-01-30 03:41发布

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 migrate, using the command sudo docker-compose run web python manage.py migrate, it works as expected. The database is built inside the Docker PostgreSQL container just fine.

Changes made to the Django app itself are likewise reflected in the Docker Django container, the moment I save them. It's great!

But if I then change a model in Django, and try to update the Postgres database to match the model, no changes are detected so no migration happens no matter how many times I run makemigrations or migrate again.

Basically, every time I change the Django model, I have to delete the Docker containers (using sudo docker-compose rm) and start afresh with a new migration.

I'm still trying to get my head around Docker, and there's an awful lot I don't understand about how it works, but this one is driving me nuts. Why doesn't migrate see my changes? What am I doing wrong?

4条回答
手持菜刀,她持情操
2楼-- · 2019-01-30 04:18

You can use docker exec command

docker exec -it container_id python manage.py migrate
查看更多
Animai°情兽
3楼-- · 2019-01-30 04:22

You just have to log into your running docker container and run your commands.

  1. Build your stack : docker-compose build -f path/to/docker-compose.yml
  2. Launch your stack : docker-compose up -f path/to/docker-compose.yml
  3. Display docker running containers : docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
3fcc49196a84        ex_nginx          "nginx -g 'daemon off"   3 days ago          Up 32 seconds       0.0.0.0:80->80/tcp, 443/tcp   ex_nginx_1
66175bfd6ae6        ex_webapp         "/docker-entrypoint.s"   3 days ago          Up 32 seconds       0.0.0.0:32768->8000/tcp       ex_webapp_1
# postgres docker container ...
  1. Get the CONTAINER ID of you django app and log into :
docker exec -t -i 66175bfd6ae6 bash
  1. Now you are logged into, then go to the right folder : cd path/to/django_app

  2. And now, each time you edit your models, run in your container : python manage.py makemigrations and python manage.py migrate

I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :

  • collecstatic
  • migrate
  • runserver or start it with gunicorn or uWSGI

Here is an example (docker-entrypoint.sh) :

#!/bin/bash

# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput

# Apply database migrations
echo "Apply database migrations"
python manage.py migrate

# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
查看更多
别忘想泡老子
4楼-- · 2019-01-30 04:22

Threads a little old. But there another method not listed so far.

Have your stack running then fire off a one shot docker-compose run command. E.g

#assume django in container named web
docker-compose run web python3 manage.py migrate

https://docs.docker.com/compose/reference/run/

查看更多
forever°为你锁心
5楼-- · 2019-01-30 04:37

I use these method:

services:
  web:
    build: .
    image: uzman
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "3000:3000"
      - "8000:8000"
    volumes:
      - .:/code
    depends_on:
      - migration
      - db
  migration:
    image: uzman
    command: python manage.py migrate --noinput
    volumes:
      - .:/code
    depends_on:
      - db

Using docker hierarchy we made, the service migration runs after set up the database and before to run the main service. Now when you run your service docker will run migrations before runs the server; look that migration server is applied over the same image that web server, it means that all migrations will be taken from your project, avoiding problems.

You avoid made entry point or whatever other thing with this way.

查看更多
登录 后发表回答