This post is to take notes about how I set up auto reload Django environment in docker.
I am learning Django recently, but I don't want to install the Django directly on my vps. As you know, the installation of Django will change the environment a lot, which is not friendly for maintenance. So I will try to keep every thing inside a container.
Unfortunately, the author of the docker image didn't make the Django automatically reload when changing the codes. That's why I did the following things.
With the help of Git hook, it is quite easy to realize it automatically. The strategy is shown as the following figure:
Every time when new commit is pushed to the git server, the git hook will deploy the source files to the mapped volume of the container, and also touch a
reload.ini file which tells the uwsgi to gracefully reload the codes without restart the container.
2. Deploy Django on docker
A new container called
django is created in the docker based on the image of
$ sudo docker run -d \ -p 80:80 \ -v /var/www/django/:/opt/django/app \ -e MODULE=myapp \ --name django \ mbentley/django-uwsgi-nginx
Note: You can use other image instead of
mbentley/django-uwsgi-nginx. But you need to find the
uwsgi.ini on your own. It can be found from their
3. Modify uwsgi setting
uwsgi won't automatically reload by default in this image. So it is necessary to change the settings in the
uwsgi.ini in the image.
Add the following line to
/opt/django/uwsgi.ini in the container:
[uwsgi] ... touch-reload = /opt/django/app/reload.ini ...
4. Setup git hooks for auto deployment
4.0. Install git server on the vps (Optional)
# Install git server on the Ubuntu $ sudo apt-get update $ sudo apt-get install -y git-all $ sudo useradd git $ sudo passwd git # enter password for the user: git
For security reasons, let the user:
git uses git-shell only
$ sudo vim /etc/passwd
Change the following line:
Enable the RSA authentication for the ssh
$ sudo vim /etc/ssh/sshd_config
Make sure the following lines look like this:
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
Upload your public key to the git server:
$ sudo cat id_rsa.pub >> /home/git/.ssh/authorized_keys
Now it's ready to create a new repository.
4.1. Init git repository on the git server
$ cd /home/git $ git init --bare django.git $ sudo chown -R git:git django.git
Now, a new reposity called
django.git is created.
4.2. Add hooks to the repository
To update the django app webfiles every time after successfully commited, the filename of the hook must be
# Create the hook $ sudo vim /home/git/django.git/hooks/post-receive
Add the following line to the file:
#!/bin/sh rm /var/www/django/* -rf git --work-tree=/var/www/django --git-dir=/home/git/django-lesson.git checkout -f touch /var/www/django/reload.ini
Make sure the git can write to the folder:
$ sudo chown -R git:git /var/www/django
Try to commit the
views.py to see if the page changes accordingly.
If not, here are some tips:
- Make sure that
touch-reload = /path/to/reload.iniis added under the section of
- Make sure the path of the
reload.iniis the same as indicated in both
- Make sure the git server is allowed to write the
reload.inito the directory.
- Make sure that the hook works fine.