Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

Shaping the future of IT, one connection at a time.

Create account Log in
Roshan Thapa
Roshan Thapa

Posted on • Updated on

Dockerizing your Django- Application!

Image description
Okay, we’ll be required to follow these simple steps:

  1. Create a Dockerfile
  2. Install python dependencies (we can use requirements.txt)
  3. Create a docker-compose.yml file
  4. Modify python settings.py file to add PostgreSQL DB Config.

Let’s start doing then …

Create a Dockerfile

In your Django project’s root directory, create a file with the name Dockerfile and add the following content into that

FROM python:3
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/
Enter fullscreen mode Exit fullscreen mode

In this Dockerfile, we are starting with a Python 3 parent image(You can modify according to your project requirements). Then we’re adding a new code directory where your project code will be copied and used by the container. We are modifying the parent image further by installing the Python dependencies defined in the requirements.txt file.
Add required libraries in requirements.txt
Other than the dependencies you already have in your existing requirements.txt file, you must have these two libraries in it:

Docker Compose Configurations

In your Django project’s root directory, create a file with the name docker-compose.yml and add the following configuration into that

version: '3'

    build: .
    command: python manage.py runserver
      - .:/code
       - "8000:8000"
      - postgres
    image: postgres
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
Enter fullscreen mode Exit fullscreen mode

This configuration defines two services: The web service of your app and the postgres service of your database. You can modify the configuration fields anytime, here’s a manual for working with docker-compose file.

Add DB Settings in Django settings.py File

Edit the database configurations in your project’s settings.py file by replacing the DATABASES = ... with the following:

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME':  'postgres',
        'USER':  'postgres',
        'PASSWORD':  'postgres',
        'HOST': 'postgres',
        'PORT': '5432',
Enter fullscreen mode Exit fullscreen mode

We have made all the required changes. Now it’s time to test your application.

Run the project using docker-compose Command

Open the terminal, make sure you are in the root directory of your project.
Run the command …
sudo docker-compose up --build
You’ll be seeing lots of build logs and at the end something like …
web_1 | Django version 2.2.5, using settings 'mydemoapp.settings'
web_1 | Starting development server at
web_1 | Quit the server with CONTROL-C.

That means, your Django app is running at the port 8000 on your Docker host. Go to http://localhost:8000 on a web browser to see the Django app home page.
Hurray …! You just finished the setup of a Django app that uses Postgresql DB using a Docker Host.

Discussion (3)

roshan_thapa profile image
Roshan Thapa Author

Goto project location and You can use the following code to generate a requirements.txt file automatically:
$ pip freeze > requirements.txt
for python3
$ pip3 freeze > requirements.txt

bkpandey profile image
Balkrishna Pandey

Little bit confused on requirements.txt file. Are we supposed to create this file with following modules?

cat <<EOF > requirements.txt
Enter fullscreen mode Exit fullscreen mode
bkpandey profile image
Balkrishna Pandey

Finally figure it out,

so I have to create requirements.txt file using following content

Enter fullscreen mode Exit fullscreen mode

But as soon as I login I am seeing following error message.

Image description

ProgrammingError at /api-auth/login/
relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
Request Method: POST
Request URL:    http://localhost:8000/api-auth/login/
Django Version: 4.1.1
Exception Type: ProgrammingError
Exception Value:    
relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
Exception Location: /usr/local/lib/python3.10/site-packages/django/db/backends/utils.py, line 89, in _execute
Raised during:  django.contrib.auth.views.LoginView
Python Executable:  /usr/local/bin/python
Python Version: 3.10.7
Python Path:    
Server time:    Fri, 30 Sep 2022 14:58:49 +0000
Enter fullscreen mode Exit fullscreen mode