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?
You can use
docker exec
commandYou just have to log into your running docker container and run your commands.
docker-compose build -f path/to/docker-compose.yml
docker-compose up -f path/to/docker-compose.yml
docker ps
Now you are logged into, then go to the right folder :
cd path/to/django_app
And now, each time you edit your models, run in your container :
python manage.py makemigrations
andpython manage.py migrate
I also recommend you to use a docker-entrypoint for your django docker container file to run automatically :
Here is an example (
docker-entrypoint.sh
) :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
https://docs.docker.com/compose/reference/run/
I use these method:
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 servicedocker
will run migrations before runs the server; look thatmigration
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.