docker + docker-compose + django 3 + python 3 + mysql 5

Created: April 14, 2020, 12:26 a.m.
Update: Sept. 26, 2021, 9:08 a.m.
Download: media/download/myproject-docker.zip
A good and fast way to use all these tecnlogies

For a new project a new docker container!
Easy to use! Work's fine!

How to use all together?!

This is an easy and fast example to build a docker-compose environment for Django developer.
This is the basic text for Django developer how to use and run a Docker and docker-compose.
https://runnable.com/docker/python/docker-compose-with-django-apps

Enviroment

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"

ii  docker-ce		5:19.03.7~3-0~ubuntu-bionic		amd64        Docker: the open-source application container engine
ii  docker-ce-cli	5:19.03.7~3-0~ubuntu-bionic		amd64        Docker CLI: the open-source application container engine
rc  docker.io		18.09.7-0ubuntu1~18.04.4		amd64        Linux container runtime
$ = bash
https://www.docker.com/
https://docs.docker.com/compose/

Struct of files and folders

myproject-docker.zip have all and ready struct folders and files to use.
$ cd ~/Desktop
$ mkdir myproject-docker
$ cd myproject-docker
$ mkdir resource/mysql/db-data -p $ touch Dockerfile docker-compose.yml requirements.txt resource/mysql/Docker
~/Desktop/myproject-docker/
							docker-compose.yml	(file)
							Dockerfile			(file)
							requirements.txt	(file)
							resource/mysql		(folder)
							resource/mysql/Dockerfile	(file)
							resource/mysql/db-data		(folder)

To create these files follow the content

$ docker-compose.yml
In this file we set db and web containers

version: '2'
services:
  db:
    image: mysql:5.7
    restart: always
    build: ./source/mysql
    volumes:
      - ./resource/mysql/custom.cnf:/etc/my.cnf
      - ./resource/mysql/db-data:/var/lib/mysql
      - ./:/code
    ports:
      - "3306:3306"
    networks:
      vpcbr:
        ipv4_address: 173.24.0.2
  web:
    build: .
    command: /code/django_project/manage.py runserver 0.0.0.0:8000
    volumes:
      - ./:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    networks:
      vpcbr:
        ipv4_address: 173.24.0.3
  networks:
    vpcbr:
      driver: bridge
      ipam:
       config:
         - subnet: 173.24.0.0/16
           gateway: 173.24.0.1
volumes:
  db-data:
$ vim Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt ADD . /code/
$ vim requirements.txt
Python==3.9.2
Django==3.1.7
asgiref==3.3.1
mysqlclient==2.0.3

DB settings, file and folder

DB name		myprojectdb
DB user		myprojectuser
DB password	myprojectpass
ROOT passd	rootpass
Set Mysql Docker Server as UTF8
$ vim resource/mysql/custom.cnf
[client]                                                                            
default-character-set=utf8                                                          
                                                                                 
[mysql]                                                                             
default-character-set=utf8                                                          
                                                                                 
[mysqld]                                                                            
init_connect='SET collation_connection = utf8_unicode_ci'                           
init_connect='SET NAMES utf8'                                                       
character-set-server=utf8                                                           
collation-server=utf8_unicode_ci                                                    
skip-character-set-client-handshake 
$ vim resource/mysql/Dockerfile
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD rootpass
ENV MYSQL_USER myprojectuser
ENV MYSQL_PASSWORD myprojectpass
ENV MYSQL_DATABASE myprojectdb
EXPOSE 3306
CMD ["mysqld"]

Build Docker Containers after all settings

$ docker-compose build

Build DB Docker and to create DB for django_project

terminal 1, wait for all Docker process to go to terminal 2
$ docker-compose build db
$ docker-compose up db
terminal 2
# to check
$ docker-compose exec db mysql myprojectdb -u myprojectuser -p 
	myprojectpass
$ mysql> show tables;
empty result
terminal 1
$ ctrl+c to stop server
OR go to DB docker BASH and run the command line
$ docker-compose exec db /bin/bash
$ mysql myprojectdb -u root -p
$ exit to out of docker (ctrl+d)

Django Project

To create the project inside of Docker by one line command
terminal 1
$ cd myproject-docker
$ docker-compose run web django-admin.py startproject django_project
OR you can go to bash web Docker  and run the command line to create the project
$ docker-compose exec web /bin/bash
$ cd /code/
$ django-admin.py startproject django_project
$ exit to out of docker (ctrl+d)
django_project will be create as root:root permission, change to be able edit as USER
$ cd myproject-docker
$ sudo chown USER:GROUP django_project -R

Update Django settings.py: myproject-docker/django_project/django_project/settings.py

Add this line to allow IPs 
ALLOWED_HOSTS = ["0.0.0.0","127.0.0.1"]
Add this parameters to use Mysql Docker Server
DATABASES = {                                                                       
    'default': {                                                                 
        'ENGINE': 'django.db.backends.mysql',                                       
        'NAME': 'myprojectdb',                                                      
        'USER': 'myprojectuser',                                                    
        'PASSWORD': 'myprojectpass',                                                
        'HOST': '173.24.0.2',                                                        
        'PORT': '3306',                                                             
    }                                                                               
} 

Mysql and Django service up!

This command will read docker-compose.yaml and start web and db docker-compose service and start Dev Django Server,
is the same as ./manager.py runserver 0.0.0.0:8000
# terminal 1
$ docker-compose up  (will start DB and WEB docker-compose services)
# terminal 2 - migrate db tables
$ docker-compose exec web /bin/bash -c "/code/django_project/manage.py migrate"
# terminal 2 - create superuser
$ docker-compose exec web /bin/bash -c "/code/django_project/manage.py createsuperuser"
# open internet Browser
http://127.0.0.1:8000
OR
http://0.0.0.0:8000

TO import DB

export DB and copy DB file to import/db.sql
$ cd myproject-docker
$ mkdir import
# import db.sql to Mysql Docker
$ docker-compose exec db /bin/bash -c "mysql myprojectdb -u myprojectuser -p < /code/import/db.sql"

Check if DB is set UTF8

$ docker-compose exec db /bin/bash -c "mysql -u myprojectuser -p"
mysql> SHOW VARIABLES LIKE 'c%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| check_proxy_users        | OFF                        |
| collation_connection     | utf8_unicode_ci            |
| collation_database       | utf8_unicode_ci            |
| collation_server         | utf8_unicode_ci            |
| completion_type          | NO_CHAIN                   |
| concurrent_insert        | AUTO                       |
| connect_timeout          | 10                         |
| core_file                | OFF                        |
+--------------------------+----------------------------+
16 rows in set (0.00 sec)


Tiago de Souza Moraes - teago.futuria.com.br - 2021 // CSS by UIKIT CSS