Django Application: Dockerised

Pranav S Khodanpur (anushku)
2 min readApr 24, 2024

Django, a high-level Python web framework, is known for its simplicity and versatility in building web applications.

Docker, on the other hand, offers a standardized environment for deploying and managing applications in containers.

Combining Django with Docker can streamline the development and deployment process, ensuring consistency across different environments.

In this guide, we’ll walk through the steps to setup and dockerize a Django application.

Prerequisites

Before we begin, ensure you have the following installed:

  • Python 3
  • Docker
  • Docker extension for Visual Studio Code (optional)

Step 1: Setting Up the Django Application

First, let’s create a Django project and app:

$ python3 -m venv django-env
$ source django-env/bin/activate
$ pip install django
$ django-admin startproject django_super_project
$ cd django_super_project
$ python manage.py startapp django_super_app
$ python manage.py migrate
$ python manage.py createsuperuser

Step 2: Running the Django Application Locally

Start the Django development server:

$ python manage.py runserver

Now, you can access the Django application at http://localhost:8000.

Step 3: Dockerizing the Django Application

Automatic Setup (Using Visual Studio Code Docker Extension)

If you’re using Visual Studio Code with the Docker extension installed, follow these steps:

  1. Press Cmd+Shift+P and search for Docker.
  2. Select “Docker: Add Docker Files to Workspace”.
  3. Choose the application type (Python: Django).
  4. Add the port (e.g., 8000) and choose whether to add a Docker Compose file.

Manual Setup

If you prefer manual configuration, follow these steps:

1. Create a docker-compose.yml file in the django_super_project directory:

version: '3.4'

services:
djangosuperproject:
image: djangosuperproject
build:
context: .
dockerfile: ./Dockerfile
ports:
- 8000:8000

2. Create a Dockerfile:

FROM python:3.12-slim

EXPOSE 8000

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app

RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "django_super_project.wsgi"]

3. Create a requirements.txt file with Django and Gunicorn:

django==5.0.1
gunicorn==21.2.0

Step 4: Building and Running the Docker Container

Navigate to the directory containing the docker-compose.yml file and run:

$ docker-compose up --build

Now, you can access your Dockerized Django application at http://localhost:8000.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response