Sat. Dec 21st, 2024

We will dockerize a Django web application in this post and for this we will consider the django application which we built in the previous post. In my previous post, I have shown you how to build a basic Django application, now lets dockerize the application.

Let’s see what we will discuss in this article in building a django web application.

Application, files and platforms:

  1. Django 3.0 application (from previous post)
  2. Dockerfile (to dockerize the application)
  3. Docker Hub (for storing repositories)

You can follow the Django 3.0 application which we will use here from the below link. 

How to build a basic Django 3.0 application

Once you have the Django application, go to the project directory “/djangoproject” and you should have “djangoapp” application in the “djangoproject” project.

Here’s the djangoproject repository from Github

https://github.com/amarnath091/djangoproject.git

The tree should look like this:

.
├── db.sqlite3
├── djangoapp1
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── admin.cpython-36.pyc
│   │   ├── models.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── views.cpython-36.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-36.pyc
│   ├── models.py
│   ├── templates
│   │   └── djangoapp1
│   │       └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── djangoproject
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

To dockerize any application, we need to create a requirements.txt file which will be mentioned in Dockerfile to install all dependencies in a single shot. 

To install all your dependencies you need to create a “requirements.txt” file and mention all the dependencies you have in there.

In requirements.txt

Django==3.0.5

(or) 

Best practice is to use the pip freeze command if you want dump all the dependencies into requirements.txt file. Make sure you are in the virtual environment and then enter

pip freeze > requirements.txt

In requirements.txt

asgiref==3.2.7
click==7.1.1
Django==3.0.5
Pillow==7.1.1
pytz==2019.3
sqlparse==0.3.1
svgwrite==1.4
Tree==0.2.4

Now all the dependencies will be in the requirements.txt file.

Next step is the Dockerfile.

Create Dockerfile:

Create a “Dockerfile” with no extension to the file name in the same djangoproject project directory.

Dockerfile is the heart of Docker as we can mention all that we need in this file and when you build and run, it goes through this Dockerfile and runs the procedures in sequence as mentioned in the file. It fails if something is not matching up properly. Mentioning the port numbers is where everyone makes a mistake so pay proper attention while defining the port numbers.

Let’s start writing the Dockerfile now:

Taking the image of ubuntu 18.04 as the OS on which we will be hosting our Django application. You can find other images from Docker Hub.

FROM ubuntu:18.04

Now we are installing and updating pip & python3 in the image.

RUN apt-get update -y && \
    apt-get install -y python3-pip python3-dev

Now copy the requirements.txt file which consists of the dependencies.

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

Let’s change current directory to /app in the image as we will be placing our app files.

WORKDIR /app

By this command we are installing all dependencies mentioned in requirements.txt

RUN pip3 install -r requirements.txt

This command copies all files from current local directory to /app in ubuntu.

COPY . /app

Exposing port number 8000

EXPOSE 8000

To run the application.

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

Your Dockerfile should look like this:

FROM ubuntu:18.04
RUN apt-get update -y && \
    apt-get install -y python3-pip python3-dev
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip3 install -r requirements.txt
COPY . /app
EXPOSE 8000
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

Once all of them are saved, you are now ready to build the image and run the container.

From the “djangoproject” directory in terminal, enter

docker build -t djangoproject .

You have now built the “djangoproject” image. To check your image, you can enter the command 

docker images 

Now lets run the container at port number 8000 in detached mode.

docker run --name djangoproject -p 8000:8000 -d djangoproject

That’s it! Now go to your browser and check your localhost and you should see the app running.

http://localhost:8000/

We have successfully built the image and we are now running on our localhost on port 8000.

Let’s push this to Docker Hub repository so it will be available to anyone and anywhere depending on your requirements.

Push to Docker Hub:

On you terminal, enter

docker login
docker image build -t <DOCKERHUB_USERNAME>/djangoproject:1.0 .
docker push <DOCKERHUB_USERNAME>/djangoproject:1.0

This should now be pushed into your Docker Hub repository.

You can access this image from anywhere just by pulling it by using the below commands

docker pull <DOCKERHUB_USERNAME>/djangoproject:1.0

And you can run it simply by using this command.

docker run -d -p 8000:8000 <DOCKERHUB_USERNAME>/djangoproject:1.0

Well, that’s one way of dockerizing a basic Django application and there are various other kinds using different images available from Dockerhub depending on your requirements.

Please leave your comments on this post and feel free to drop any suggestions on what you are looking for and I will try to post on them in the future articles.