Leveraging Docker Images to deploy your Django backend on Openshift
Openshift with Docker Images is the ultimate tool you need for automated deployment.
Openshift at CERN
Here at CERN, we have more than 13000 “central” sites and hundreds of web servers. Hence, to centralise the web hosting and to minimise the deployment hassle we use Openshift.
Why Openshift you say?
- Openshift provides a centralized web hosting environment for multiple applications.
- No need to maintain a Virtual Machine and its OS.
- You can switch the hosting platform.
- Automatic application deployment and rollback changes.
Being a container platform, Openshift uses Docker Images to deploy the applications.
Deploying a Django Application on Openshift
You have your Django backend ready and you want to deploy it on Openshift. Not so fast…
There are some steps involved in this process but the result you get is quite convenient.
Step 1: Install Docker
Step 2: Create a requirements.txt file
Create a requirements.txt file so that your Docker Image can install all the dependencies when creating the docker image.
pip freeze > requirements.txt
Step 3: Create a Docker File
A DockerFile is a text document that contains all the commands a user could call on the command line to assemble an image.
To create a DockerFile in your Django project, just add a file named ‘Dockerfile’ on the root directory of your project with no extension.
Example of a Django Dockerfile
# Install required packages
RUN yum -y install \
The complete file can be found here.
Step 4: Create a Docker Image
Building an image
$ docker build -t image-name:tag .
Running the image(here we are mapping port 8000)
$ docker run -it -p 8000:8000 image-name:tag
Now you have your docker image ready on your machine, you still need to deploy to a container registry.
Here at CERN, we use Gitlab Registry for our docker images.
Login to your Docker Registry
$ docker login gitlab-registry.example.com
Push your image to Gitlab
$ docker push image-name:tag gitlab-registry.example.com/your-repo-name
Step 5: Deploying the application on Openshift
Before the deployment part, you need to install the Openshift CLI.
Login to Openshift
$ oc login
OpenShift server [https://localhost:8443]:https://openshift.example.com
Authentication required for https://openshift.example.com(openshift)
Create a project if you don't have one
$ oc new-project <projectname>
oc new-app --docker-image="gitlab-registry.example.com/your-repo-name"
NOTE: You can do all of this through the Openshift UI.