Wednesday, 27 April 2022

Django4.04 with docker 101

Python 3.10.4

PIP 22.0.4


Docker 20.10.14

Docker Compose 1.29.2


*** Automation build/deploy trasvis-CI now requires payment *****


------------------------------------------------------

***Docker image info ***********************



Docker image python 3.10/alpine3.14 (https://github.com/docker-library/python/blob/f871d0435e7f35a693fa1b60aa159e6a1a4c6a2e/3.10/alpine3.14/Dockerfile)



Docker image django 4.04 (https://pypi.org/search/?q=django)



Docker compose file version 3 (https://docs.docker.com/compose/compose-file/compose-versioning/)



Docker compose flake8 4.0.1 (https://pypi.org/project/flake8/)


# build images specified in Dockerfile in prepareation for services specified in docker-compose.yml. docker-compose container is not created 

docker compose build 


# Docker compose will create volume based on root:root, in .yml add app: user: $UID:$GID

# then change the current directory mapped with volume : chown -R ${whoami}:${whoami} dir/


# Then either do export UID=${UID}

# export GID=${GID}

# or export them to ur bash_profile


---------------

python & django

# Run the service with command. this is one off and container is not created.

1) docker-compose run app sh -c "django-admin startproject app ."


use django admin to create a new project called app using WORKDIR specified in Dockerfile so it will be app/app


---------------------------------------------

manage.py is the main func for django to run everything



#### Start core app by running manage.py and pass in startapp and core. This will create core folder in the service and mapped to your directory app/ (so app/core)

2)docker-compose run app sh -c "python manage.py startapp core"


# core contains migrations, test.py need to be removed as we are gonna move to folder, also the views.py as we are not servering anything

# Create app/core/tests/ then app/core/tests/__init__.py 

# test is run using manage.py test

# https://docs.djangoproject.com/en/4.0/topics/testing/overview/


3) docker-compose run app sh -c "python manage.py test"


# **__init__.py  its like index to include the files in those folder and apporiate namespaces

# We create a test folder because we might have multiple tests




# need to make sure app/core is added to django main project app/app/settings.py -> INSTALLED_APPS list


####### model creation ###############


# https://docs.djangoproject.com/en/4.0/topics/db/models/






# migrations in django with specified app (core), this will do the database migrations create a migrations file in core/migrations for django to use to create user table in future

             <ng-container *ngIf="accServService.prodGridDataFetched$ | async">

4)docerk-compose run app sh -c "python manage.py makemigrations core"


# This creates  app/core/migrations/0001_initial.py

# Every time appp/core/models.py is updated, need to run makemigrations core command again to rebuild migrations file

# Check if test is passed python manage.py test 



#Flake8 is a Python library that wraps PyFlakes,

# pycodestyle and Ned Batchelder's McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors (like “library imported but unused” and “Undefined name”

https://simpleisbetterthancomplex.com/packages/2016/08/05/flake8.html#:~:text=Flake8%20is%20a%20Python%20library,and%20to%20check%20cyclomatic%20complexity.

5) docker-compose run app sh -c "python manage.py test && flake8"

######################################




--------------------------------------------

No comments:

Post a Comment