Dockerizing a Django + PostgreSQL project

In this guide I show you how to run your Django app in one Docker container and your PostgreSQL database in another and make them talk to each other. We are going to manage and run these two containers using Docker Compose.

👉 If you want to learn how to do this for a Django + MySQL app read this post.

I am assuming you already have a Django project locally that is using a local database and that your project has a requirements.txt file. You should also install Docker on your machine if you haven’t already.

Let’s dive right in:

The first step is to create a Dockerfile in the root of your project (this is where you have your requirements.txt and manage.py files).

FROM python:3.8
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

Let’s go through that line by line:

  • FROM python:3.8 is the base image your Docker image will be built upon. This will be pulled from DockerHub.
  • ENV PYTHONUNBUFFERED 1 ensures that the output Django writes to the terminal comes out in real time without being buffered somewhere. This makes your Docker logs useful and complete.
  • WORKDIR /app creates a new folder in your container called app which will be your project’s root inside the container. It then sets that as the work directory in which the subsequent commands will be executed in.
  • COPY requirements.txt /app/requirements.txt copies your local requirements.txt file to the Docker container.
  • RUN pip install -r requirements.txt will make sure you have all your project dependencies installed inside the container.
  • COPY . /app and finally it’s time to copy your project’s content into the Docker container.

Now create a new file in your local project root. This one is called docker-compose.yml and is used for configuring Docker Compose.

version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - ./db:/var/lib/postgresql/data/
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    depends_on:
      - db

This defines two services: db and web.

db uses an official PostgreSQL image from DockerHub. We then set a bunch of environment variables that are required for PostgreSQL. ./db:/var/lib/postgresql/data/ creates a local db folder in your project root so that database information can be saved if you destroy the db service.

The web service builds its image using the Dockerfile we created earlier. It runs the Django server and exposes port 8000 to your machine. We tell Docker Compose that this service is dependant on the db service.

No we need to update Django database settings to use the new PostgreSQL database:

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

Now all we have to do is to start everything up. In your project root run:

$ docker-compose up

This should build your images and start the containers. Open a browser and go to 0.0.0.0:8000. You should see your app. Now if you open a new terminal and run a docker ps you will see the web and db containers are running:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES
77a320233bf2        my_project_web      "python manage.py ru…"   4 minutes ago       Up 4 minutes        0.0.0.0:8000->8000/tcp       my_project_web_1
66324c20d91e        postgres            "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        5432/tcp                     my_project_db_1

If you want to stop Docker Compose, press Ctrl+C in the first terminal or run docker-compose stop in the project root folder in another terminal.

To run manage.py commands in the Django container (web):

$ docker-compose run --rm web python manage.py migrate

Subscribe via RSS