Learn Docker with Easy Implementation in Python/Django Application

Saad Ali
4 min readAug 14, 2022

--

So first and foremost, What is Docker, and why do we need it?

Before moving to what is Docker? Let's first figure out why we need Docker:

Let's say you build a web application and you are ready to deploy it. After deployment, you realized it was not working as it was supposed to. Maybe due to some missing dependencies (that are required to run your application) or maybe it was developed in windows and someone else is trying to access it on a different operating system that may not support some dependencies.

So, Docker is a platform that helps us to solve all the above problems. It containerizes your application in an isolated environment along with all its dependencies. So anyone can access your application through docker easily. Docker creates an image of your app that contains all details of your application.

Let's Dockerize our Django App

Docker Container

We aim to build a Docker Container for our app and then try to access it using docker commands.

Firstly create Dockerfile in your app directory and paste the below code

FROM python:3.9-buster
WORKDIR /code
COPY . /code
RUN pip install -r requirements.txt
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

Let's break it down line by line:

python:3.9-buster is a base image that we use to create our custom image. A base image is a starting point for the image that we finally want to create.

In the next line, we are creating a directory in our container with the name code. Afterward, we are putting all app code (represented by . ) into the /code directory of the container. Then, we are running a command that installs all the dependencies of our app into a container and finally launches an app at 0.0.0.0:8000

Disclaimer: 
if your app does not contain requirements.txt file, run the below command
pip freeze > requirements.txt

After Dockerfile, run the below command to create a container with the name “myapp”. Docker will create a container myapp according to the commands we have given in Dockerfile

docker build -t myapp . 
  • t is short for tag name (we are telling docker that we want to give the name of our container as well otherwise docker will create a just id as default)
  • full stop at the end of the line tells docker to find Dockerfile in the current directory

After building your container you will see the following output in your terminal:

Successfully built d4874d020432
Successfully tagged myapp:latest

Now to run your container you can execute the below command

docker run d4874d020432
(In your case, Container ID will be different)

You can see your running containers using the docker ps command. To view all the containers, you can use the docker ps -all command.

The docker run command will throw an error because you haven’t configured your database with docker. You need to create a separate container for your database and connect it with your app, that can be done by creating a network.

Networking in Docker

docker network create mynetwork

After creating a network with the name “mynetwork”, we are going to make separate containers for our app and database inside that network

Network

Let’s first build our database inside a network

docker run --network=mynetwork --name=mydb -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root -e POSTGRES_DB=postgres -h=mydbhost -p 5431:5432 -d postgres

The above command will create a PostgreSQL database inside a container and associate it with your network.

Note: 
By default, Django works with mysql. You can use mysql as well instead of postgresql

Tags:

  • name: Image name
  • -e: Environment variables (Database credentials)
  • -h: hostname
  • -p: port (on what port is our DB accessible to the outside container)
  • -d: detached mode (command will run in the background and will not disturb the main thread)
  • Postgres: base image

Before building an app, we need to configure the database in the settings.py file of our project

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'mydbhost',
'PORT': '5432',
}
}

Now we are ready to build and run our app

docker build -t myapp .docker run --network=mynetwork -p 8000:8000 -e POSTGRES_NAME=postgres -e POSTGRES_USER=root -e POSTGRES_PASSWORD=root myapp

It will create our app container and connect it to our network. Now our app and database can easily communicate. That’s it?

NO! whatever changes we will make in our database either creating a new table or adding a new record will be lost whenever we rebuild our app. To solve that problem we need to use Docker volumes

Docker Volumes help us with persisting data either generated by or used by the docker container.

We will use Docker Volumes with Docker Compose in the next article. Hope you like this article. Feel free to share your feedback in the comment section :)

--

--

Saad Ali

Software Engineer. Love to work in Python, Django and Rails