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:
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
The tree should look like this:
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.
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
Now all the dependencies will be in the requirements.txt file.
Next step is the 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.
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.
Let’s change current directory to /app in the image as we will be placing our app files.
By this command we are installing all dependencies mentioned in requirements.txt
This command copies all files from current local directory to /app in ubuntu.
Exposing port number 8000
To run the application.
Your Dockerfile should look like this:
COPY ./requirements.txt /app/requirements.txt
RUN pip3 install -r requirements.txt
COPY . /app
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
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.
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.
On you terminal, enter
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.
Recent updates on DevOpsPal
Follow us of social for latest updates.
Links to get started.