Docker Compose with Django and Postgresql

deploy multi container with compose yml

Mazer
4 min readSep 13, 2021

docker compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Prerequisites:

Tutorial docker-compose with django project

1. Introduction

This is a tutorial about docker compose with django project. It just a basic of compose file where you need to know and starting point.

docker-compose

2. Tutorial and practice

Please follow the steps below about this tutorial

Step 1: Create project directory and Dockerfile

➜ mkdir compose-django

➜ cd compose-django

➜ compose-django ll

total 0

➜ compose-django nano Dockerfile

Dockerfile

# syntax=docker/dockerfile:1

FROM python:3

ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt /code/

RUN pip install -r requirements.txt

COPY . /code/

  • FROM python:3 : mean that we start container with python3 installed
  • ENV PYTHONBUFFERED=1 : mean that set env buffer for python = 1
  • WORKDIR /code : we set working directory to /code — mean that everything is working in /code directory
  • COPY requirements.txt /code : copy requirement to start django project installatation
  • RUN pip install -r requirements.txt : install requirement with pip
  • COPY . /code/ : copy current directory to /code in docker container

Step 2: Create requirements.txt for django

➜ compose-django touch requirements.txt

➜ compose-django ll

total 4.0K

-rw-rw-r — 1 kimly kimly 159 ៩ 13 09:58 Dockerfile

-rw-rw-r — 1 kimly kimly 0 ៩ 13 10:41 requirements.txt

➜ compose-django nano requirements.txt

Add this requirements to requirements.txt

Django>=3.0,<4.0

psycopg2-binary>=2.8

Step 3: Create docker compose file docker-compose.yml

$ nano docker-compose.yml

docker-compose.yml
  • version: “3.9” : tell docker-compose with version of docker to runing with our docker : https://docs.docker.com/compose/compose-file/compose-file-v3/
  • Service: tell docker how many server you want to up and run
  • web : name of first service
  • build . : build current directory from dockerfile
  • port — “5000:5000” : run in port 5000:5000
  • volumes: — . :/code : mount current directory to container dir /code, i.e: when you update anything in current directory it will update in containe too cuz it is already mounted
  • environment: FLASK_ENV: development : set in environment variable in container of flask to development
  • radis : name of second service
  • Image : run this service from image in docker hub registry

If you want set name repository to your compose while build you can add this:

add image name or repo

Step 4: Try to run django with compose file

Step 5: Change owner current directory to your current user

Step 6: Update connection to database

$ nano composedjango/settings.py

Database config

# settings.py

DATABASES = {

‘default’: {

‘ENGINE’: ‘django.db.backends.postgresql’,

‘NAME’: ‘postgres’,

‘USER’: ‘postgres’,

‘PASSWORD’: ‘postgres’,

‘HOST’: ‘db’,

‘PORT’: 5432,

}

}

Step 7: Up your container with command

➜ compose-django docker-compose up -d

Creating compose-django_db_1 … done

Creating compose-django_web_1 … done

while it is done, run this command to check your container running or not

➜ compose-django docker ps

list containers

it is status is up 4 seconds, mean that everything is working fine

Step 8 : Open your django application

visit at: http://localhost:8000 , you will see this

Troubleshooting

  • if you run it in server you need to check you ip server first that put your ip server in browser with port 8000
  • if you got this image below
not allow hosts

Example: Your IP is 192.168.32.78

You need to allow it in host file locate in composedjango/settings.py

Add your host

ALLOWED_HOSTS = [“192.168.32.78”]

#ALLOWED_HOSTS = [“*”] : mean all is allowed

More document: https://docs.docker.com/samples/django/

Well,That’s it and have a nice day. I hope this article will be another knowledge share to everyone and feed your brain.

#cambodiandevops#devopskh#kimlyvith#docker#docker-compose#django#postgresql#deploymentdjangowithpostgresql#sharingiscaring#sharingisgreat#sharingisimprove

--

--

Mazer

DevOps engineer at ABA Bank Cambodia — Be your own unique :)