Dockerizing Django

 

This is a step by step process that follows how to configure your django app with docker. 

Tools and Technologies:
  • python
  • django
  • docker
  • postgres

Mac and Windows users can install Docker Desktop which contains both Docker and Docker-Compose tools.

Linux users need to follow the instructions on Get Docker CE for Ubuntu and then Install Docker Compose separately.

You are good to go when you can successfully run:

docker-compose --version


Project Setup:

$ mkdir django-docker && cd django-docker
$ python3.8 -m venv env
$ source env/bin/activate
(env)pip install django psycopg2
(env)django-admin startproject main .

our Project structure will be  like this
└── app
├── django-docker
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
├── manage.py
└── requirements.txt

Docker Setup:

Now we will create a config files for docker to create and run.
  • create a Dockerfile and docker-compose.yaml file on our root directory.
└── app
├── django-docker
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
├── manage.py
└── Dockerfile
└── docker-compose.yaml
└── requirements.txt

open up your Dockerfile and write this following:

FROM python:3.8-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt /code/requirements.txt
RUN pip install -r requirements.txt
COPY . /code


Next, open up  docker-compose.yml file and write these codes.


version: "3.8"
services:
db:
image: "postgres:11"
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
restart: always
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
env_file:
- ./.env
depends_on:
- db

volumes:
postgres_data:


  • Now we will create a .env file on root directory:


SECRET_KEY=dafefeewfewfwef //get from setting.py file


  • open up setting.py file and change database config

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

  • Next we run docker command......

docker-compose up -d --build


  • so we see these successfully build in our cli....

  • Run this command to execute your service name web..
docker-compose exec web sh



  • Now we are inside the docker container, and migrate it......

python manage.py migrate
  • Yet, we are not finished, we will get error like this while migrating....

raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2
module: No module named 'psycopg2'

  • So to solve this problem we need to configure in our Dockerfile, open your Dockerfile and write the following code:


FROM python:3.8-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

# install psycopg2 dependencies
RUN apk update \
&& apk add --no-cache postgresql-dev gcc python3-dev musl-dev

# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt /code/requirements.txt
RUN pip install -r requirements.txt
COPY . /code




  • Next we run following commands for docker.

docker-compose down -v
docker-compose up -d --build
docker-compose exec web sh

  • Next run migrate

/code # python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
/code #

  • oh yeah you will migrate smoothly and go to your browser and type 127.0.0.1:8000 

  • Last but not least, if you are using pillow for image then you will face same error as postgres setup:

  • so to fix this we need to config in our Dockerfile  would be like this.
FROM python:3.8-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /code

# install psycopg2 dependencies
RUN apk update \
&& apk add --no-cache postgresql-dev gcc python3-dev musl-dev

# pillow dependencies
RUN apk add --no-cache jpeg-dev zlib-dev
RUN apk add --no-cache --virtual .build-deps build-base linux-headers \
&& pip install Pillow

# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt /code/requirements.txt
RUN pip install -r requirements.txt
COPY . /code


  • Then follow the same steps above and you are good to go.....


Hope you guys enjoyed it.... chao 👋🏾👋🏾👋🏾




Post a Comment

Previous Post Next Post