build Django/PostgreSQL application with Docker Compose

STEP1: install docker and docker compose
1. sudo apt-get install docker-ce docker-ce-cli containerd.io
2. sudo groupadd docker
3. ubuntu@ubuntu2004:~$ sudo usermod -aG docker $USER (you should be able to run docker with user ubuntu now)
4. sudo curl -L “https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
5. sudo chmod +x /usr/local/bin/docker-compose

STEP2: create Django_Compose folder and files:
Dockerfile
requirements.txt
docker-compose.yml

ubuntu@ubuntu2004:~/Django_Compose$ cat Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

ubuntu@ubuntu2004:~/Django_Compose$ cat requirements.txt
Django>=3.0,<4.0
psycopg2-binary>=2.8

ubuntu@ubuntu2004:~/Django_Compose$ cat docker-compose.yml
version: "3.9"

services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

STEP3: Create a Django project
sudo docker-compose run web django-admin startproject composeexample .
sudo chown -R $USER:$USER .

STEP4: edit composeexample/settings.py
Replace the DATABASES = … with the following:
and also update ALLOWED_HOSTS = [‘*’]

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

STEP5 : start docker compose with command “docker-compose up”

ubuntu@ubuntu2004:~/Django_Compose$ docker-compose up
Starting django_compose_db_1 ... done
Starting django_compose_web_1 ... done
Attaching to django_compose_db_1, django_compose_web_1
db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2021-03-11 17:34:58.762 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2021-03-11 17:34:58.762 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2021-03-11 17:34:58.763 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2021-03-11 17:34:58.765 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2021-03-11 17:34:58.770 UTC [25] LOG:  database system was shut down at 2021-03-11 17:25:27 UTC
db_1   | 2021-03-11 17:34:58.775 UTC [1] LOG:  database system is ready to accept connections
web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | March 11, 2021 - 17:35:00
web_1  | Django version 3.1.7, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/

Draw the system architecture diagrams with python

Stop spending time manually tweaking misaligned arrows in visio, now you can create beautiful architecture diagrams with Python diagrams library!

  1. installation on Ubuntu
    sudo apt-get install python3-pydot graphviz
    sudo pip3 install diagrams

2. get sample code diagram.py from https://diagrams.mingrammer.com/docs/getting-started/examples

from diagrams import Cluster, Diagram
from diagrams.onprem.analytics import Spark
from diagrams.onprem.compute import Server
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.aggregator import Fluentd
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.network import Nginx
from diagrams.onprem.queue import Kafka

with Diagram("Advanced Web Service with On-Premise", show=False):
    ingress = Nginx("ingress")

    metrics = Prometheus("metric")
    metrics << Grafana("monitoring")

    with Cluster("Service Cluster"):
        grpcsvc = [
            Server("grpc1"),
            Server("grpc2"),
            Server("grpc3")]

    with Cluster("Sessions HA"):
        master = Redis("session")
        master - Redis("replica") << metrics
        grpcsvc >> master

    with Cluster("Database HA"):
        master = PostgreSQL("users")
        master - PostgreSQL("slave") << metrics
        grpcsvc >> master

    aggregator = Fluentd("logging")
    aggregator >> Kafka("stream") >> Spark("analytics")

    ingress >> grpcsvc >> aggregator

3. run it with python3 diagram.py, you will get advanced_web_service_with_on-premise.png in same folder