Containerizing Django web app in Red Hat OpenShift

How to create Django web app in Red Hat OpenShift Container Platform


Django is one of the best web development framework and very popular in developing database driven web applications. There are many commercial apps written using Django, to name a few, Instagram, Spotify, Eventbrite, Pinterest, and etc. Django is also very popular in open-source echo-system. OpenStack (private cloud) GUI dashboard named Horizon is also written in Django in open source community. In a nut shell, Django encourages rapid development, clean, and pragmatic design. It is built by experienced developers, it removes much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. Best of all…It’s free and open source.

In this article, I’ll be setting up Django web application in Red Hat OpenShift platform using Dockerfile available at git repository. Below is the git repo which I’ll be using to provision container image.

Above git repo contains the following main components:

1- Dockerfile

2- Application code

3- Requirements.txt

The Dockerfile

Below are the contents of Dockerfile included in this example:

# pull the official base image
FROM python:3.8.3-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app
RUN pip install -r requirements.txt
# copy project
COPY . /usr/src/app
CMD ["python", "", "runserver", ""]
  • FROM python:3.8.3-alpine →This is the base image from which the Docker container will be created.
  • WORKDIR /usr/src/app → This will set the working directory inside the container to /usr/src/app.
  • ENV PYTHONDONTWRITEBYTECODE 1 → This is optional. it prevents Python from copying pyc files to the container.
  • ENV PYTHONUNBUFFERED 1 → this ensures that Python output is logged to the terminal, making it possible to monitor Django logs in realtime.
  • RUN pip install --upgrade pip → This command will install and upgrades the pip version that is in the container. pip is required to install other requirements for the django application
  • COPY ./requirements.txt /usr/src/app → This will copy the requirements.txt file into the work directory inside the container.
  • RUN pip install -r requirements.txt → This installs all the required modules for the django_todo application to run in the container. anything in requirements.txt file is equivalent to running pip install <package name> . Requirements.txt makes it easy to install all the dependent packages
  • COPY . /usr/src/app → This copies all the project source code to the working directory in the container. The code is needed to run the django app inside the container.
  • EXPOSE 8000 → exposes port 8000 for access from other applications.
  • CMD ["python", "", "runserver", ""] → sets the executable commands in the container. This is equivalent to running the following command: python runserver 8000

Lauch application

Now, you can login to OpenShift either using oc login or exporting the kubeconfig file. You can also use OpenShift GUI, however, this example is using CLI for provisioning.

Run the following command to provision the application in OpenShift.

[root@e26-linuxjb ocp-install2]# oc new-app

This will create image build config for image and create an image out of this config and launch the POD.

Run the following command to verify the build configs.

# oc get buildconfig

verify if the build is completed successfully.

[root@e26-linuxjb ocp-install2]# oc get builds
djangotodo-1 Docker Git@89b91a3 Complete 40 minutes ago 22s

Run the following command to see if the POD is ready.

[root@e26-linuxjb ocp-install2]# oc get pods
djangotodo-1-build 0/1 Completed 0 41m
djangotodo-8f66c4754-gzhj2 1/1 Running 0 30m

Once the POD is ready, expose the service to the outside world by executing the following command.

# oc expose service/djangotodo[root@e26-linuxjb ocp-install2]# oc get routes
djangotodo djangotodo 8000-tcp None
[root@e26-linuxjb ocp-install2]#

On your browser visit Following figure will confirm if your Django app is running from the container.


With this example, you can start modernizing your web application using one of the best web framework i.e. Django. This example sets the stage for developing cloud-native applications and also creating micro-services to support this Django based web app. By defining triggers in your buildconfigs, you can implement simple CI/CD pipeline for your app.



Innovative and transformative cross domain cloud solution architect @Microsoft (& xCisco). Helping companies to digitally transform!!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afzal Muhammad

Innovative and transformative cross domain cloud solution architect @Microsoft (& xCisco). Helping companies to digitally transform!!!