Create and run container of Django

Hi Readers!

Greetings!

Hope you are reading the blog and gaining some interesting knowledge.

Today I am going to tell you to create a docker container of django and how to run it.

Though this blog is mainly targeting students, newcomers , today’s target audience is intermediate.

So lets start.

Prerequisite – you should have installed rpm based linux(red hat, fedora) and on that os, you should have docker package installed.

Step one:

Write a dockerfile for setting up django.

Create a file ‘Dockerfile’ in any folder and write following commands in to it.

FROM centos:centos7
MAINTAINER <your email id>

RUN yum -y install epel-release
RUN yum -y install python-pip python-django git sqlite; yum clean all

EXPOSE 8000

CMD [ “/bin/bash” ]

Save the file.

Step two:

Build the docker image using this Dockerfile

command:

docker build -t django:centos7anand .

(Note: we mentioned . ‘dot’ here as we are running the command from the location where Dockerfile is saved.)

run the ‘docker images’ command and you should see the newly created image

$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_django latest 2892fe55292c 45 hours ago 409MB

Step three:

docker run -d -p 8000:8000 django:centos7anand

then you should see the container running , you can check using docker ps -a command.

Step four:

Start the container using following command:

docker start <container id of the container which we have created in above step>

Step Five: Check/test whether django is successfully installed in the container

command:

docker run django:centos7anand django-admin –version

Your output should be like :

]# docker run django:centos7anand django-admin –version
1.11.27

Congratulations you have successfully containerised the django package. 🙂

Stay tuned for next interesting and helpful knowledge for this blog.