9

So, I often am working with projects where there will be multiple dockerized django application servers behind a loadbalancer, and frequently I will need to deploy to them. I frequently use Watchtower to do pull-based deploys. I build a new image, push it to dockerhub, and watchtower is responsible for pulling those images down to the servers and replacing the running containers. That all works pretty well.

I would like to start automating the running of django migrations. One way I could accomplish this would be to simply add a run of the manage.py migrate to the entrypoint, and have every container automatically attempt a migration when the container comes online. This would work, and it would avoid the hassle of needing to come up with a way to do a lockout or leader election; but without some sort of way to prevent multiple runs, there is a risk that multiple instances of the migration could run at the same time. If I went this route, is there any chance that multiple migrations running at the same time could cause problems? Should I be looking for some other way to kick off these migrations once and only once?

2
  • 4
    Did you find a answer? I'm working on a new deployment and it would simplify it to be able to run the migrate command in parallel. I've checked quickly the migrate command code and so far I haven't seen anything using a table lock or something like this. The documentation don't speak about it neither :/ Oct 30, 2020 at 13:49
  • 2
    Not sure. I ended up adding a tiny singleton server that just runs migrations and other one-off tasks on startup. It's the same image as the main application, but configured with a different set of environment variables. easier than worrying if the migrations were clean Jun 24, 2021 at 23:38

1 Answer 1

6

Running migrations at the same time in parallel is not safe. I've tested it by running the migrate command in parallel on a data migration, and Django will run the migration twice. It will add 2 rows to the django_migrations table. Check out this Google Groups post discussing the issue And this article about running migrations on container statup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.