Posts Setup Django and Docker
Post
Cancel

Setup Django and Docker

Intro

It is possible to develop only with Docker (rather than a local system). Therefore a virtual environment is not necessary since dependencies are always installed in the container. Also, environment variables are fetched with docker-compose.yml, and then again django-environ is not really necessary. However I want to be able to keep both options available, since Docker might not always be available.

Full project available on GitHub: https://github.com/veglos/django-docker

Setup

  1. Create the project directory and it’s virtual environment.
1
2
3
4
5
mkdir django-celery
cd django-celery

python -m venv venv
venv\Scripts\activate.bat
  1. Initiate the git repository
1
git init
  1. Create the .local.env file for environment variables with the following content:
1
2
3
4
5
6
7
DEBUG=1
SECRET_KEY=django-insecure-se@p1rdv@wl1(8jb1f^#(m&)dzm=l+hdot7w!h*$bac19t&ro^
POSTGRES_DB=postgres_db
POSTGRES_USER=postgres_user
POSTGRES_PASSWORD=postgres_pwd
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
  1. Create the .docker.env file for environment variables with the following content:
1
2
3
4
5
6
7
DEBUG=1
SECRET_KEY=django-insecure-se@p1rdv@wl1(8jb1f^#(m&)dzm=l+hdot7w!h*$bac19t&ro^
POSTGRES_DB=postgres_db
POSTGRES_USER=postgres_user
POSTGRES_PASSWORD=postgres_pwd
POSTGRES_HOST=db
POSTGRES_PORT=5432

Notice the difference with .local.env on POSTGRES_HOST=db. This is because docker will reference the db service in the docker-compose.yml file.

  1. Create the .gitignore file with the following content:
/venv
.env
  1. Create the requirements.txt file with the following content:
Django==3.2.4
django-environ==0.4.5
psycopg2==2.9.1
  1. Install dependencies
1
pip install -r requirements.txt
  1. Create django project ‘myproject’ and the app ‘myapp’
1
2
django-admin startproject myproject .
python manage.py startapp myapp
  1. Modify myproject/settings.py with:
1
2
import os
import environ
1
2
3
4
# Load and read .local.env file
# OS environment variables take precedence over variables from .local.env
env = environ.Env()
env.read_env(os.path.join(BASE_DIR, '.local.env'))
1
2
3
4
5
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', False)
1
2
3
4
5
6
7
8
9
10
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': env("POSTGRES_DB"),
        'USER': env("POSTGRES_USER"),
        'PASSWORD': env("POSTGRES_PASSWORD"),
        'HOST': env("POSTGRES_HOST"),
        'PORT': env("POSTGRES_PORT"),
    }
}
  1. Create Docker file with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
FROM python:3

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED=1

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app

RUN pip install -r requirements.txt

COPY . /usr/src/app
  1. Create docker-compose.yml with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
version: "3.9"
   
services:

  db:
    image: postgres    
    env_file:
      - .docker.env
    ports:
      - "5432:5432"
    volumes:
      - type: volume
        source: app-database
        target: /var/lib/postgresql/data

  app:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/usr/src/app
    env_file:
      - .docker.env
    ports:
      - "8000:8000"
    depends_on:
      - db
volumes:
  app-database:
    name: app-database
  1. Lauch with docker
1
docker-compose up

Browse localhost:8000 to check if the project is up.

Close containers with

1
docker-compose down
  1. Launch from local with only the database in a container (rememeber to activate the virtual environment if you are using another terminal)
1
2
docker-compose up db
python manage.py runserver

Browse localhost:8000 to check if the project is up.

This post is licensed under CC BY 4.0 by the author.