Docker Container & Django Framework

Pawel Grajewski
3 min readJun 6, 2020

Django is one of the most effective tools for web developers who want to achieve many goals with small amount of time. Django environment support creating CMS with the django-cms package, custom shop with django-oscar, API for mobile app with REST framework, Django-channels for an async chat, and many other pre-build application for fast development. When we go deeper into the environment it is built from many modules and packages which one depends on another. When we think about building the whole environment with database periodic tasks like celery and in-memory database like Redis it started to be big chain which has all separate version and sometimes not easy to maintain with many developers working on the same version. A big step further was using a virtual environment which can keep a version of packages and with git repository create a close environment which can be replicated by many developers but when thinks goes more complicated some packages work good on Linux but worse in Windows the next step to keep everything close to the virtual machine which keep also operating system in which everything works. And in this point goes Docker with containers and concept to keep everything close in one place with the ability to plug it on the server as one thing and can be easy replicate and scale for traffic in the back-end.

Let’s start thinking about our project as a small service with a database for the starter.

The first thing that we need to do is install Docker on our computer and add a docker package for Visual Studio Code which helps us manage containers. The next step is to understand how it works. In the beginning, we will create dockerfile in the root directory of our project. This file will describe how our system environment will look like.

This file will in docker install python, create directory code where will copy file requirements.txt and using pip install dependencies define in requirements.txt file inside container next will copy root directory to code folder in the container.

Now create requirements.txt file:

Last thing is to create docker-compose.yml where we will define how to start our project with db:

Now we can create a project using docker-compose and Django commands:

docker-compose run web django-admin startproject composeexample .

This will generate content in local file system and in a container with 2-way data binding.Last adjust is to add in settings our database and we ready to go:

# setting.py

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

and run the project with commands:

docker-compose up

It will automatically use python manage.py runserver to serve our project in container with localhost:8000 port.

Example command when we want to migrate our database:

docker-compose run web python manage.py migrate

Links:

--

--