Docker zing a Python Django Web App on Ubuntu

Dockerizing a Python Django Web Application

Gautamankul
4 min readApr 28, 2022
Docker zing a Python Django Web App on Ubuntu

Hey Developer! Today we’re learn dockize a Django application on ubuntu It’s worked all Linux distro …
This article covered a simple hello world Django application In which we talk about Docker

1.Ubuntu (any linux Distro)
2.Python Pip, the package manager
3.Python2 and Python3
4.Docker
5.Django

What is Docker
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

What is Use of Docker
When building web applications, you have probably reached a point where you want to run your application in a fashion that is closer to your production environment. Docker allows you to set up your application runtime in such a way that it runs in exactly the same manner as it will in production, on the same operating system, with the same environment variables, and any other configuration and setup you require.

Means, Docker facilitate to Developer to work with from another existing project own system Docker container holds the entire package of application with own resources. like below example.

Now we are going for Docker installation in Ubuntu for that follow official link . Docker Installation Link

Check Docker version in your system like $ docker --version if it show like this Docker version 20.10.7, build f0df350
mean docker installed your system

1.Create Django Project in Pip Environment

Let us, make a directory and install pipenv in that directory

Now we Activate our Environment .

Now We are create a Django project and Django app..

Now We are going to runserver for all set..

It’s look like this.

At Now we are configure file and render hello world on web page let get start..

2.Change in hello-project urls.py:

Now we can set the URL route for the pages app. Since we want our message to appear
on the homepage we’ll use the empty string ‘’ . Don’t forget to add the include import
on the second line as well

Note:We are create a file in pages (app directory) which name should be urls.py.

4.Change in page urls.py:
Within your text editor import path on the top line, add the home_page_view , and then
set its route to again be the empty string of ‘’ . Note that we also provide an optional
name, home , for this route which is a best practice.

Here we are done for configure and runserver and see like this page.

Notice: Now are exit the Django virtual environment for docker configuration for quit pipenv.

Let’s create our first Dockerfile to see all of this theory in action.

Note: Here Dockerfile name should be D in Capital letter. And Project file structure must be like following image.

Description:

Dockerfiles are read from top-to-bottom when an image is created. The first instruc-
tion must be the FROM command which lets us import a base image to use for our
image, in this case Python 3.9.
Then we use the ENV command to set two environment variables:
* PYTHONUNBUFFERED ensures our console output looks familiar and is not buffered
by Docker, which we don’t want
* PYTHONDONTWRITEBYTECODE means Python will not try to write .pyc files which we
also do not desire

Next we use WORKDIR to set a default work directory path within our image called code
which is where we will store our code.

For our dependencies we are using Pipenv so we copy over both the Pipfile and
Pipfile.lock files into a /code/ directory in Docker and so on…..

Note:
Our image instructions are now done so let’s build the image using the command
docker build . The period, . , indicates the current directory is where to execute the command.

Moving on we now need to create a docker-compose.yml file to control how to run the
container that will be built based upon our Dockerfile image.

The final step is to run our Docker container using the command docker-compose up .
This command will result in another long stream of output code on the command line.

To confirm it actually worked, go back to http://127.0.0.1:8000/ in your web browser.
Refresh the page and the “Hello, World” page should still appear. because this application are running locally your’s system.

Stop the container with Control+c (press the “Control” and “c” button at the same
time) and additionally type
docker-compose down . Docker containers take up a lot of
memory so it's a good idea to stop them in this way when you're done using them.
Containers are meant to be stateless which is why we use volumes to copy our code
over locally where it can be saved.

Great Developer’s we are create Django application custom image and now we can upload this project image on Docker Hub

and fetch it and worked without any package installation error own your system locally.
Thank You!!

--

--

Gautamankul

A Software Developer Engineer & Entrepreneur and along with i am a Buddhist.