Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Django in Production

You're reading from  Django in Production

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781804610480
Pages 348 pages
Edition 1st Edition
Languages
Author (1):
Arghya Saha Arghya Saha
Profile icon Arghya Saha

Table of Contents (21) Chapters

Preface 1. Part 1 – Using Django and DRF to Build Modern Web Application
2. Chapter 1: Setting Up Django with DRF 3. Chapter 2: Exploring Django ORM, Models, and Migrations 4. Chapter 3: Serializing Data with DRF 5. Chapter 4: Exploring Django Admin and Management Commands 6. Chapter 5: Mastering Django Authentication and Authorization 7. Part 2 – Using the Advanced Concepts of Django
8. Chapter 6: Caching, Logging, and Throttling 9. Chapter 7: Using Pagination, Django Signals, and Custom Middleware 10. Chapter 8: Using Celery with Django 11. Chapter 9: Writing Tests in Django 12. Chapter 10: Exploring Conventions in Django 13. Part 3 – Dockerizing and Setting Up a CI Pipeline for Django Application
14. Chapter 11: Dockerizing Django Applications 15. Chapter 12: Working with Git and CI Pipelines Using Django 16. Part 4 – Deploying and Monitoring Django Applications in Production
17. Chapter 13: Deploying Django in AWS 18. Chapter 14: Monitoring Django Application 19. Index 20. Other Books You May Enjoy

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 2024 Publisher: Packt ISBN-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.
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 ₹800/month. Cancel anytime}