Friday 17 June 2022

Compose and Django | containerize the legacy system using Docker


Ref:- docs.docker.com/samples/django/

sudo docker --version

Docker version 20.10.11, build 761974f


docker-compose --version

docker-compose version 1.29.1, build c34c88b2


mkdir ComposeDjango && cd ComposeDjango

ls

Dockerfile  docker-compose.yml  requirements.txt


cat Dockerfile 

# syntax=docker/dockerfile:1

FROM python:3

ENV PYTHONDONTWRITEBYTECODE=1

ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt /code/

RUN pip install -r requirements.txt

COPY . /code/


cat requirements.txt 

Django>=3.0,<4.0

psycopg2>=2.8


cat docker-compose.yml 

version: "3.9"

   

services:

  db:

    image: postgres

    volumes:

      - ./data/db:/var/lib/postgresql/data

    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"

    environment:

      - POSTGRES_NAME=postgres

      - POSTGRES_USER=postgres

      - POSTGRES_PASSWORD=postgres

    depends_on:

      - db


sudo docker-compose run web django-admin startproject composeexample .

<...>

ls -l

total 24

-rw-r--r-- 1 user user  189 Jun 17 12:25 Dockerfile

drwxr-xr-x 2 root     root     4096 Jun 17 12:32 composeexample

drwxr-xr-x 3 root     root     4096 Jun 17 12:32 data

-rw-r--r-- 1 user user  497 Jun 17 12:27 docker-compose.yml

-rwxr-xr-x 1 root     root      670 Jun 17 12:32 manage.py

-rw-r--r-- 1 user user   31 Jun 17 12:25 requirements.txt


sudo chown -R $USER:$USER composeexample manage.py


ls -l

total 24

-rw-r--r-- 1 user user  189 Jun 17 12:25 Dockerfile

drwxr-xr-x 2 user user 4096 Jun 17 12:32 composeexample

drwxr-xr-x 3 root     root     4096 Jun 17 12:32 data

-rw-r--r-- 1 user user  497 Jun 17 12:27 docker-compose.yml

-rwxr-xr-x 1 user user  670 Jun 17 12:32 manage.py

-rw-r--r-- 1 user user   31 Jun 17 12:25 requirements.txt


#to Connect the database Replace 'Database'

vim composeexample/settings.py

<...>

# Database

# https://docs.djangoproject.com/en/3.2/ref/settings/#databases


import os


[...]


DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.postgresql',

        'NAME': os.environ.get('POSTGRES_NAME'),

        'USER': os.environ.get('POSTGRES_USER'),

        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),

        'HOST': 'db',

        'PORT': 5432,

    }

}

<...>


sudo docker-compose up --build

Building web

Sending build context to Docker daemon  43.91MB

Step 1/7 : FROM python:3

 ---> 6bb8bdb609b6

Step 2/7 : ENV PYTHONDONTWRITEBYTECODE=1

 ---> Using cache

 ---> 8bcb2efe0ebb

Step 3/7 : ENV PYTHONUNBUFFERED=1

 ---> Using cache

 ---> 90141c60eb79

Step 4/7 : WORKDIR /code

 ---> Using cache

 ---> 7615300b689c

Step 5/7 : COPY requirements.txt /code/

 ---> Using cache

 ---> 5b520c29e26f

Step 6/7 : RUN pip install -r requirements.txt

 ---> Using cache

 ---> fee2bac5b31a

Step 7/7 : COPY . /code/

 ---> 760f6e271496

Successfully built 760f6e271496

Successfully tagged composedjango_web:latest

Starting composedjango_db_1 ... done

Recreating composedjango_web_1 ... done

Attaching to composedjango_db_1, composedjango_web_1

db_1   | 

db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization

db_1   | 

db_1   | 2022-06-17 13:10:47.108 UTC [1] LOG:  starting PostgreSQL 14.3 (Debian 14.3-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

db_1   | 2022-06-17 13:10:47.109 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432

db_1   | 2022-06-17 13:10:47.109 UTC [1] LOG:  listening on IPv6 address "::", port 5432

db_1   | 2022-06-17 13:10:47.116 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

db_1   | 2022-06-17 13:10:47.125 UTC [25] LOG:  database system was shut down at 2022-06-17 13:06:25 UTC

db_1   | 2022-06-17 13:10:47.131 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  | June 17, 2022 - 13:10:49

web_1  | Django version 3.2.13, using settings 'composeexample.settings'

web_1  | Starting development server at http://0.0.0.0:8000/

web_1  | Quit the server with CONTROL-C.

web_1  | [17/Jun/2022 13:12:31] "GET / HTTP/1.1" 200 10697

web_1  | [17/Jun/2022 13:12:31] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423


sudo docker ps

CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                       NAMES

63fea9598831   composedjango_web      "python manage.py ru…"   19 seconds ago   Up 18 seconds   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   composedjango_web_1

746d2b5aeb3d   postgres               "docker-entrypoint.s…"   38 minutes ago   Up 19 seconds   5432/tcp                                    composedjango_db_1


curl localhost:8000

or










#to stop Ctrl+C or from other terminal

sudo docker-compose down

Stopping composedjango_web_1 ... done

Stopping composedjango_db_1  ... done

Removing composedjango_web_1                ... done

Removing composedjango_web_run_f90adcbc769e ... done

Removing composedjango_db_1                 ... done

Removing network composedjango_default


#exit log

<...>

composedjango_web_1 exited with code 0

db_1   | 2022-06-17 13:16:55.734 UTC [1] LOG:  received fast shutdown request

db_1   | 2022-06-17 13:16:55.737 UTC [1] LOG:  aborting any active transactions

db_1   | 2022-06-17 13:16:55.739 UTC [1] LOG:  background worker "logical replication launcher" (PID 31) exited with exit code 1

db_1   | 2022-06-17 13:16:55.740 UTC [26] LOG:  shutting down

db_1   | 2022-06-17 13:16:55.756 UTC [1] LOG:  database system is shut down

composedjango_db_1 exited with code 0


No comments:

Post a Comment