Is it safe to allow multiple instances of a Django application to run the same database migration at the same time?
Scenario description
This is a setup where a multiple instances of a Django application are running behind a load balancer. When an updated version of the Docker container is available, each of the old Docker images are replaced with the new version.
If new Django migrations exist, they need to be run. This leads me to the question: is it safe to allow multiple containers to run the migration (python manage.py migrate
) at the same time?
I have two hypothesis about what the answer to this question might be.
migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.
Create the Django project by running the docker-compose run command as follows. $ sudo docker-compose run web django-admin startproject composeexample . This instructs Compose to run django-admin startproject composeexample in a container, using the web service’s image and configuration.
If you are running Docker on Linux, the files django-admin created are owned by root. This happens because the container runs as the root user. Change the ownership of the new files. $ sudo chown -R $USER:$USER.
When pulling your new images to production your migrations don't jive with what's in the persistent db in the django_tables in prod and you can't run migrations without erroring out. Anyone know how to make all this less painful. We've been using Docker in dev and production for over a year now.
All services will run in Docker containers. Use Docker to get the services up and running quickly without any hassle of configuring and debugging environment-related issues. Besides, make sure that you have Docker and Postgres up and running on your local machine. You could check if those 2 are running by:
No, it's not safe to run the migration in all containers at the same time, since you might end up applying the same migration twice.
There are two possible cases:
Applying the migration twice (e.g. adding a table column) violates a database constraint, so therefore only the first container that run the migration manages to finish the migration. In this case the other containers will die, although your orchestration system probably will restart them.
Applying the migration twice doesn't violate any constraint and therefore can be applied multiple times. In this case you can end up with duplicated data.
In any case, you should try to have only one container applying migrations at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With