Create your first Django Dockerised App

Hi, in this article, I will walk you through how to dockerize your Django Web Applications. Dockerising a Django app can be as easy as writing your first code (Hello world). But, before we start Dockering Our Django application, we need to understand what Dockerisng means.

What is a Docker?

Docker makes it possible to build, run and distribute applications in a controlled environment.

This ability allows developers to package and ship software and its dependencies to distribute as containers.

With Docker as a containerization platform, your application will always run the same way, regardless of the host or different environment.

Why should you Dockerise?

Containerization of applications has become efficient to increase productivity in Software and Web Development processes.

As applications keep getting extra-ordinarily better and complicated, Developers want to take advantage of using Docker and want to know more about this valuable tool.

A Docker container lets you wrap up all your software or web application needs in a single file called a docker image. Your software will run and be tested conveniently on any other machine when shared with another recipient as long as the recipient has Docker.

Setting up Docker on your Machine

The first step to set up Docker is to install Docker on your local machine. Click any of these links.

  1. For Windows, click here for installation instructions for your operating system.
  2. For MacOS, click here for installation instructions for your operating system.

This tutorial assumes that you already have Python and Django installed on your local machine and are proficient in Python and Django Framework.

Django App

Let's start by creating a project, our Django project. Run the code below on your terminal to create this project.

django-admin startproject dockerapp

Next, navigate into your project folder and create an app named sampleapp with the following line of code.

python startapp sampleapp

Add this app to your INSTALLED_APPS, and let's get started with dockerizing.

Creating a Dockerfile

'A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using Docker build, users can create an automated build that executes several command-line instructions in succession. This page describes the commands you can use in a Dockerfile .'
Source: Docker Docs

In simple terms, a Dockerfile contains set-up instructions for your software or application.

A Dockerfile contains some of the directives below.
FROM: this directive sets the base image from which it will build the Docker container.
COPY: this directive copies files from the file system into the container.
RUN: this directive executes commands in the container.
CMD: directive sets the executable commands within the container.
WORKDIR: this directive sets the working directory in the image created.

Create a file in the root project directory with Dockerfile(no extension), and add the following codes.

# pull the official base image
FROM python:3

# set environment variables

# set work directory

ADD . /app

COPY . /requirements.txt /app/requirements.txt

# install dependencies
RUN pip install -r requirements.txt

# copy project
COPY . /app


CMD ["python", "", "runserver", ""]

FROM python:3 sets the base image from which it will create the Docker container.

WORKDIR /app sets the working directory inside the container to /app.

ENV PYTHONUNBUFFERED 1 ensures that Python output is reported to the terminal, allowing real-time monitoring of Django logs.

COPY ./requirements.txt /app/requirements.txt copies the requirements.txt file to the container's work directory.

RUN pip install -r requirements.txt installs all the required modules for our application to run in the container.

EXPOSE 8000 allows port 8000 to be accessible from other applications.

CMD ["python", "", "runserver", ""] sets the container's executable commands.

Building the Docker image

To generate a Docker image from the Dockerfile, we created to run the command below.

docker build --tag dockerapp:latest

Creating and running the Docker Container

To generate a Docker container from the Docker image we created above, run the command below.

docker run --name dockerapp -d -p 8000:8000 dockerapp:latest

--name sets the name of the Docker container.
-d This command causes the image to run in detached mode. This image can run in the background.
-p 8000:8000 binds the Docker container's port 8000 to localhost's port 8000.
dockerapp: latest provides the image that it will use to create the Docker container.

To see if the dockerapp application is running in the container, go to localhost on port 8000 in your browser.


You have successfully dockerized your first Django application.


Docker is an excellent tool for packaging applications with the required dependencies to run locally and in production. Docker playground is an online playground that you can test various Docker commands without installing Docker locally on your computer.

Thanks For Reading.