Updating Django Source with Docker Deployments
While deploying docker multiple times, you may not want to copy over your Django source code every time you do a deployment.
Setting up supervisord
Luckily there is an easy way to manage this. Since you are working with Django, there is a good chance that you are also managing the processes (like uwsgi) with supervisord.
Here are some of the steps that you can take with supervisord
- Set up a new process in
supervisord - Do not allow it to
autorestartsince it will be a one-shot process - Call another script in any format to update the source code
- As an example, I use
bashto update my source code throughgit
- As an example, I use
Here’s a sample code:
[program:source-updater]
redirect_stderr = true
stdout_logfile = /shared/source_code_updater.log
directory = /ws/
command = /ws/source_code_updater.sh
autorestart=FalseUpdating the source code
Few things are important to note in a docker deployment:
- Not every commit needs to be deployed
- Filter your commits to only allow deployable code to be updated on
docker - Include regression, unit and system tests to be part of your build process
- Once everything has been confirmed to be working, tag your code so that you know it is worthy of going to docker
- Another way would be to manage this process through branches and merge only if everything passes
dockerdeployments would build off this merged branch or tagged version- This way even if you have made 10 commits while fixing a bug and are still in the process of fixing it, you know it won’t go to
dockerdeployment
With that idea, do a checkout and update the source code according to specific tag:
git checkout -f tags/your_tag_name
git pull origin tags/your_tag_nameTelling uwsgi about the updated source code
Once you have updated your source code, you need to re-load the project onto uwsgi so that nginx or apache can pick it up.
The simplest way to achieve it using the config parameter of uwsgi: --touch-reload. It will reload uWSGI if the specified file is modified/touched
Just remember to setup supervisord in your Dockerfile with this config parameter.
[program:app-uwsgi]
redirect_stderr = true
stdout_logfile = /var/shared/_uwsgi.log
command = /ws/ve_envs/rwv2/bin/uwsgi --touch-reload=/ws/wsgi.ini --ini /ws/wsgi.iniYou can choose any file. I choose uwsgi.ini because the contents never really need to change in it.