Deploying Django with Github Actions and Docker
Discover the art of deploying your Django application to a VPS with ease, all thanks to the magic of Github Actions. This article will be your guiding light on the journey to hassle-free deployment. Let's get started!
In this article, you will learn how to deploy your Django application to your VPS using Github Actions.
If you want to, you can checkout one of my Django applications here, where I have implemented the steps described in this article.
The main idea is that every time you create a "release", Github will pick up newest release version and trigger a docker build for that release. The end result is quite amazing, because now you can simply run docker pull ghcr.io/<user>/repo:latest
on your VPS to get the latest version.
The article assumes a moderate level of understanding of docker.
The Setup
Before we begin working with Github actions, make sure you have the correct configuration setup. Personally, I create an env
folder in my project settings folder, as can be seen here. This folder can contain your production, developer and staging setup. In my my setup, I created a prod.py
config file for running in production. Database credentials and so on are fetched via environment variables in my docker-compose.yml
file.
Dockerfile
My Dockerfile
is structured as followed:
FROM python:3.10-slim-bullseye
ENV PYTHONUNBUFFERED 1
RUN apt update && apt upgrade -y && apt install -y \
git \
gcc \
musl-dev \
libffi-dev \
make \
libxslt-dev \
dos2unix \
unzip \
procps \
&& groupadd -r -g 997 burgarkartan \
&& useradd -m -s /usr/sbin/nologin -u 998 -g burgarkartan burgarkartan \
&& pip install --upgrade pip
USER burgarkartan
RUN mkdir -p /home/burgarkartan/project
WORKDIR /home/burgarkartan/project
COPY --chown=burgarkartan . /home/burgarkartan/project/
RUN pip install -r requirements.txt -U --user && echo "export PATH=$(python -c 'import site; print(site.USER_BASE + "/bin")'):$PATH" >> ~/.bashrc
As can be seen in the first RUN
step, I created a new user with the uid
of 998. This ID corresponds to the ID of the same user I created in my host, using useradd -r -M -s /usr/sbin/nologin burgarkartan
. Run id <username>
on your host to find the id. This is necessary to avoid any permission issues on the mapped files between the host and container.
The final folder structure on the host looks like:
root@Gambit:/var/www# ls -lh burgarkartan.se/
total 8.0K
-rw------- 1 root root 561 Aug 13 23:15 docker-compose.yml
drwxr-xr-x 5 burgarkartan burgarkartan 4096 Aug 13 14:06 static
The static
directory contains static files like CSS and JavaScript files which will be served by nginx.
A crucial step in my docker setup is the entrypoint.sh script. It's defined as the entrypoint and is used for running the Django application in either production or development.
Getting ready for Action
Once you are satisfied with your production configuration and your Dockerfile, it's time to set up Github actions. To do that, I suggest following the article below, it contains all you need.
You should have something that looks like this. The next step is create a release.
Release the kraken
Creating a release in Github is super easy, head over to the release tab and click Draft a new release.
Now you need to enter a new tag for this release. In the image below, you can see that my previous tag was v0.1.7, but now I want to create a new release so therefore I need to enter a new tag.
Once you are done, hit Publish release. Now, Github should trigger a docker build and start building your latest release for you. Head over to the actions page, you should see your latest release being built.
Running in production
Once the image has been built, you can start using it in your environment. Below you will find my production ready docker-compose.yml
, as you can see, the image points to ghcr.io/dubs3c/burgarkartan:v0.1.7
which was the version built in the previous screenshots.
version: '3'
services:
burgarkartan:
container_name: burgarkartan-prod
image: ghcr.io/dubs3c/burgarkartan:v0.1.7
command: bash entrypoint.sh prod
ports:
- "127.0.0.1:8001:8000"
volumes:
- "./static:/home/burgarkartan/project/assets"
env_file:
- /root/burgarkartan.se.env
- /root/burgarkartan.se.db.env
depends_on:
- postgres
postgres:
image: postgres:15.4
env_file:
- /root/burgarkartan.se.db.env
volumes:
- /var/lib/burgarkartan/postgresql/data:/var/lib/postgresql/data
Now it's simply a matter of running docker-compose up -d
, and your latest image built by Github should be downloaded.
The end
Hopefully this was useful and that you now know have an idea on how could deploy Django applications with Github. If you have any questions, don't hesitate to reach out on Twitter.