A Boilerplate to Easily Dockerize and Deploy Django Apps

Django, PostgreSQL, Nginx, Docker, Docker Compose

Baysan
CodeX
6 min readAug 7, 2022

--

Hi folks! It’s been a while since I last could write. In this article, I will write about a repo I created to dockerize and easily deploy my Django apps.

Photo by Mohammad Rahmani on Unsplash

Introduction

This repo really helps me to deploy my apps. I use the repo as a boilerplate. To use it, you just need to create an empty Django project in the folder that you cloned by using the repo below. In this article, I’ll explain the files and logic behind the dockerizing process.

Makefile

We can say this is a recipe file to manage commands that we use to build or run the project. To use this file, we use make command. In this boilerplate, mostly the commands below will be used:

  • make collect
  • make migration
  • make runproxyversion

Django App Dockerfile

This is the Dockerfile which is located at the project’s root level. Basically, it creates an image to follow the steps below.

  • Pull 3.9 version of Python as a base image
  • Set the working directory as /app
  • Copy all the files at the same level with this Dockerfile into the WORKDIR
  • Upgrade the pip
  • Execute make install command which is being used to install the project dependencies
  • Expose 8000 port for the containers that will be created by using this image

Nginx Dockerfile

Under the nginx folder, our Nginx image is located. We will use this image to create a container to serve (publish) our Django app outside.

This image will be searching for nginx.conf file which has to be located in the same directory with this Dockerfile to copy into /etc/nginx/conf.d .

The above nginx.conf file helps us to create communication between our Django app and Nginx. We created an upstream with a custom name. The upstream will be listening to the service which is named webservice in the same network that this container running. Pay attention here, the webservice is a service name which is coming from docker-compose.yml . Then, Nginx will start to listen to port 80 and when a request came, it will direct it to the webservice . So, it will work like a reverse proxy.

Docker Compose File

Lastly, we need a composer which is docker-compose.yml to run and compose the containers that we will create by using the images above.

Here, we need to pay attention to volumes and networks. Because sometimes we can not serve our static files in production mode (which is DEBUG=False) or we can not create communication web app service and database service. To do that I created 2 volumes and 2 networks.

  • 1 network for communication between database service and web app service
  • 1 network for communication between web service and Nginx service
  • 1 volume for database service (to provide consistency)
  • 1 volume for static files from web app container to Nginx container (if we have media files, we will be in need to create a new one for media files).
Image by Author

I also set “80:80” under the ports in nginx_service to handle the request that comes from outside into the Django app.

Also, under the settings folder, there are 2 config_*.py files. If DEBUG environment variable is 1 given by docker-compose.yml file, the project will be in Debug mode. Otherwise, it will be in Production mode. The settings.py file will be like below.

Deploying On Production

To deploy our environment, I use Nginx on my prod server. I create a new reverse proxy on the prod server to direct requests to the Nginx container which is created in docker-compose.yml file.

I create a new file under /etc/nginx/sites-available folder and put the content below into the file. Here, I direct the request that comes to http://127.0.0.1:80 . We do that because we set 80:80 in our Nginx service. We can deploy more projects on the same server by changing these port numbers.

Lastly, we need to create a symbolic link for this file.

Image by Author

Finally

I was a bit nervous about these topics like how to create bridges between the containers, how to serve my static files, how to create a reverse proxy on my server etc. After I learned these topics, I started using them for every project. I wish this repo and this article will help you to develop, manage and deploy your projects. You can access the repo by using the link below.

Kind regards

--

--

Baysan
CodeX

Lifelong learner & Developer. I use technology that helps me. mebaysan.com