Reader small image

You're reading from  Django in Production

Product typeBook
Published inApr 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781804610480
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Arghya Saha
Arghya Saha
author image
Arghya Saha

Arghya (argo) Saha, is a software developer with 8+ years of experience and has been working with Django since 2015. Apart from Django, he is proficient in JavaScript, ReactJS, Node.js, Postgres, AWS, and several other technologies. He has worked with multiple start-ups, such as Postman and HealthifyMe, among others, to build applications at scale. He currently works at Abnormal Security as a senior Site Reliability Engineer to explore his passion in the infrastructure domain. In his spare time, he writes tech blogs. He is also an adventurous person who has done multiple Himalayan treks and is an endurance athlete with multiple marathons and triathlons under his belt.
Read more about Arghya Saha

Right arrow

Dockerizing Django Applications

In previous chapters, we discussed how to write application code for a Django application. In the next few chapters, we will explore the different tools that we can use to improve our development experience, deploy our code to a production system that is scalable, and monitor our production system.

Let us get started with improving our development experience. In Chapter 1, while setting up our local development for the Django project, we mentioned Docker. We will learn how to use Docker to develop our Django application. In this chapter, we shall primarily focus on Dockerizing our Django application locally with all the services running on our local system using containers.

In the previous chapters of this book, we used different remote services for Postgres and Redis while working with our Django project locally so that we did not get blocked by any installation steps. In order to ensure that everyone can easily follow all the instructions given...

Technical requirements

In this chapter, we shall discuss how to use Docker with a Django application. We shall not be getting into the details of Docker and containers. We expect you to have a foundational understanding of Docker. The following is expected from you for this chapter:

  • An understanding of containers and Docker
  • An understanding of how to install Docker on your local system
  • Familiarity with accessing Docker development environments

Since we shall not be focusing on the basics and details of Docker, if you are not familiar with the requirements mentioned, please read/revise the concepts of Docker from these books/videos:

  • Docker Deep Dive, Second Edition – By Nigel Poulton
  • Docker for Developers – By Richard Bullington-McGuire, Andrew K. Dennis, and Michael Schwartz from Packt Publishing
  • The Ultimate Docker Container Book, Third Edition – By Dr. Gabriel N. Schenker from Packt Publishing
  • A Developer’s Essential...

Learning the basics of Docker

Docker is an open source containerization platform that allows developers to build, deploy, and manage containerized applications. Before we get into the concepts of Docker, let us learn a few concepts that will build the foundation:

  • Virtualization – The concept of virtualization has been around since the 1970s. It is the process of running virtual instances of different operating systems/software in a virtual layer that is abstracted from the actual hardware.

    Consider an analogy of 10 bungalows in an area, where each bungalow has its own amenities that cannot be shared with the neighbors. All the owners live in their bungalows and are limited by the area allocated to them. They can do anything in their bungalow but do not have access to their neighbor’s bungalow. Figure 11.1 shows an example of what virtualization means using a virtual machine.

Figure 11.1: Concept of Virtual machine using virtualization

Figure 11.1: Concept of Virtual machine using virtualization...

Working with the requirements.txt file

Different projects can have different dependency needs to run. That’s why we use the requirements.txt file to capture all the third-party packages installed and used in a project. But as projects grow, we can realize that some of the dependency packages are useful only while we are developing locally, some are useful in our CI pipeline, and some are used only for production. Hence it is important that we break our requirements.txt file into multiple parts.

We need to split our requirements.txt file into multiple files that would be used for different purposes. This is common in large projects and is always better to implement as soon as we create a new project. This ensures that only the actually used packages are shipped to production.

Here is a structure seen in most large projects:

  • requirements-base.txt – This file contains all the common third-party packages that are used in all the environments. For example, Django...

Creating a Dockerfile for a Django project

A Dockerfile is a text document that has all the instructions needed to create a Docker image. We can consider a Dockerfile as a recipe that we can follow to cook any item. Let us take the boilerplate Dockerfile example we would need for our Django project. Create a /myblog/backend/Dockerfile file with the following content:

# Pull the official base image
FROM python:3.11
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Install different Linux packages.
RUN apt-get update \
&& apt-get install gcc postgresql postgresql-contrib libpq-dev python3-dev netcat-traditional -y \
&& apt-get clean
# set working directory and install packages using pip.
WORKDIR /app
COPY ./requirements ./requirements
RUN pip install -r requirements/requirements-local.txt
# Copy the Application code
COPY . /app/
COPY ./entrypoint.sh /app/entrypoint.sh
# Give permission to entrypoint.sh file as executable.
RUN chmod...

Composing services using docker-compose.yaml

Now let us explore how to create a docker-compose.yaml file that orchestrates our Django project, Celery, Celery Beat, Postgres, and Redis services together. Add the following code to the /myblog/docker-compose.yml file:

version: "3.8"
services:
  postgresql_db:
    image: postgres:16.1
    volumes:
      - ~/volumes/proj/dip/postgres/:/var/lib/postgresql/data/
    ports:
      - 5432:5432
    env_file:
      - ./.env
    environment:
      - POSTGRES_USER=${DB_USERNAME}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
  redis_db:
    image: redis:7.2
    container_name...

Summary

In this chapter, we had a high-level overview of how to set up a Django application using Docker. We took a boilerplate template that had Postgres, Redis, Django, Celery, and Celery Beat to run the service locally using Docker. We have not deep-dived into the topics because Docker itself is a vast topic and it is expected that you have some understanding of Docker so that you can use this chapter as a reference to set up your project.

In the next chapter, we shall focus on how we can implement a CI pipeline using GitHub Actions, setting up Git hooks, and using version control with different branching strategies. We shall also discuss a few points on how to set up a code review process.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Django in Production
Published in: Apr 2024Publisher: PacktISBN-13: 9781804610480
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Arghya Saha

Arghya (argo) Saha, is a software developer with 8+ years of experience and has been working with Django since 2015. Apart from Django, he is proficient in JavaScript, ReactJS, Node.js, Postgres, AWS, and several other technologies. He has worked with multiple start-ups, such as Postman and HealthifyMe, among others, to build applications at scale. He currently works at Abnormal Security as a senior Site Reliability Engineer to explore his passion in the infrastructure domain. In his spare time, he writes tech blogs. He is also an adventurous person who has done multiple Himalayan treks and is an endurance athlete with multiple marathons and triathlons under his belt.
Read more about Arghya Saha