Open In App

How to Dockerize a Django Application?

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers(namespace). To understand this perspective in a detailed way let's do a quick comparison between the virtual machines and containers:

Imagine virtualization as a lock that both virtual machines (VMs) and containers can open, but each does so differently. VMs provide virtualization at the hardware level, using a hypervisor to simulate components like the CPU, memory, storage, and network. Containers, on the other hand, work at the operating system level. Here, a single OS kernel manages communication between the hardware and software, allowing the host OS to run multiple containers through a container engine.

When a user requests a specific service or application, the container engine retrieves an image from a repository that developers have prepared, and it’s ready to execute. This image is comprehensive, containing all the configurations, libraries, and system tools needed for the task. A key distinction is in how VMs and containers isolate resources: VMs are isolated from the physical server itself, while containers isolate individual processes within the OS, making them a lighter and more efficient way to run applications.

Virtual-machines-vs-Containers

Containers

A container is a compact, self-contained unit that holds an application along with everything it needs to run like code, libraries, and settings. By packaging all of this together, a container ensures that the application works the same way no matter where it’s run, whether on a developer’s laptop, a test server, or in a live environment.

Unlike virtual machines, containers don’t need a full operating system for each instance. Instead, they share the host OS kernel, making them efficient and quick to start. Managed by a tool like Docker containers allow applications to be easily deployed, scaled, and maintained, especially in cloud environments.

docker

Dockerize Django App

Here, we are dockerizing a simple user login and sign up Django project. The project we are using too dockerized is

Step 1: Sign up and Log in

To download source code use

$ git clone https://github.com/itsvinayak/user_login_and_register.git

Step 2: Create a dockerfile

The next step is to add Docker to it. So, create an empty file named Dockerfile and put this code inside it.

$ touch Dockerfile

Now edit it.

FROM python:3.6

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .

EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Step 3: Build docker image

Now we need to build our Docker image and run it. This can be done by the following command : to build image

$ sudo docker build -t image_name .

Step 4: Verify docker image

By using docker images command you can see all your images.

$ sudo docker images

Step 5: Run docker application

To run docker application use

$ sudo docker run -p 8000:8000 image_name

Best Practices for Dockerfile in Django Projects

Once you’ve got the basics of your Dockerfile in place, it’s crucial to follow best practices to make your Docker image as efficient, secure, and lightweight as possible. Here are some practical tips for improving your Django Dockerfile:

  • Start with a lightweight base image: Instead of using a full Python image, opt for a more minimal version, like python:3.9-slim. This includes only what’s necessary, reducing the size of your Docker image and speeding up build times.
  • Clean up after installing dependencies: After installing any packages, it’s important to clean up the unnecessary files to reduce the image size. For example:
RUN apt-get update && apt-get install -y --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
  • Consider multi-stage builds: If your Dockerfile needs to compile dependencies or use additional tools, multi-stage builds are a great way to exclude unnecessary layers from your final image. This keeps the resulting image smaller and more efficient.
  • Avoid hardcoding sensitive information: Never include sensitive data such as database credentials in your Dockerfile. Instead, use environment variables or secrets management tools to securely handle this kind of information.

Set up Django with Docker Compose

Docker Compose makes managing multiple containers, such as your Django app and database, a breeze. Instead of running each container separately docker compose lets you define and manage them in a single configuration file. Here’s how to set it up:

Add docker compose to manage multiple containers

  • To simplify the process of running your Django app with a database, create a docker-compose.yml file. Here’s an example configuration to run Django with PostgreSQL:
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app
ports:
- "8000:8000"
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: django_db

Working of Docker Compose File

  • The web service tells Docker to build the Django app using the Dockerfile in your current directory.
  • The command ensures that Django’s development server runs when the container starts.
  • The volumes setting allows you to make changes to your code locally, which will immediately be reflected inside the container.
  • The ports directive maps port 8000 on your local machine to port 8000 on the container, so you can access the app at localhost:8000.
  • The depends_on section ensures that the db service (PostgreSQL) is started before the Django app.

Running your application

To start both the Django app and the database containers, simply run:

docker-compose up
start-the-app

This will launch both services in the right order. To stop everything, run:

docker-compose down
stop-the-app
--

Using Docker Compose simplifies your development process by automatically managing dependencies, and it makes scaling or deploying your app easier with minimal changes to the configuration.

Conclusion

Dockerizing your Django app with Docker and Docker Compose simplifies development and deployment. It helps your app runs consistently across different environments, while Docker Compose manages multiple containers seamlessly. By following best practices, you increase performance, security, and speed up deployment, making maintenance and scaling easier.


Next Article
Practice Tags :

Similar Reads