Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi

Overview

environment

procedure

Directory structure

./WORKDIR
    |--- docker-compose.yml   # Docker-compose body
    |---.gitignore            #Not applicable to git (I will make it this time, use it when using git)
    |---nginx
         |---nginx.conf       #nginx config file
         |---uwsgi_params     #Parameters for uwsgi
    |---django
         |---Dockerfile       # Dockerfile
         |---requirements.txt #List to install with pip
         |---.env             #Environment variables for django
    |---mysql
         |---.env             #Environment variables for mysql
    |---sql
         |---init.sql         #At DB startup(First time only)Script to flow to

Create and edit files

docker-compose.yml


version: '3'

services:
  nginx:
    image: nginx:1.13
    ports:
      - "8000:8000"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
      - ./static:/static
    depends_on:
      - django

  mysql:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - "3306:3306"
    env_file:
      - ./mysql/.env
    volumes:
      - ./mysql:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d

  django:
    build: ./django
    command: uwsgi --socket :8001 --module app.wsgi --py-autoreload 1 --logto /tmp/mylog.log
    volumes:
      - ./src:/code
      - ./static:/static
    expose:
      - "8001"
    env_file:
      - ./django/.env
    depends_on:
      - mysql

.gitignore


mysql/*
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

django/requirements.txt


Django
uwsgi
PyMySQL

django/.env


MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=db
MYSQL_USER=bbt-user
MYSQL_PASSWORD=password

nginx/nginx.conf


user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

    upstream django_8001 {
        ip_hash;
        server django:8001;
    }

    server {
        listen      8000;
        server_name 127.0.0.1;
        charset     utf-8;

        location /static {
            alias /static;
        }

        location / {
            uwsgi_pass  django_8001;
            include     /etc/nginx/uwsgi_params;
        }
    }
    server_tokens off;
}
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

mysql/.env


MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=db
MYSQL_USER=bbt-user
MYSQL_PASSWORD=password
TZ='Asia/Tokyo'

sql/init.sql


GRANT ALL PRIVILEGES ON db.* TO 'user'@'%';

FLUSH PRIVILEGES;

Build

$ docker-compose run django django-admin.py startproject app .
./WORKDIR
    |--- docker-compose.yml
    |---.gitignore
    |---nginx
         |---nginx.conf
         |---uwsgi_params
    |---django
         |---Dockerfile
         |---requirements.txt
         |---.env
    |---src
         |---app
              |---__init.py__  #Meaning of being python (do not edit)
              |---asgi.py      #Do not use
              |---settings.py  #django main unit configuration file
              |---urls.py      #URL processing
              |---wsgi.py      #wsgi file
              |---manage.py    #Administrative commands
    |---mysql
         |---.env
         |--- ~~              #Multiple are created, but omitted because they are not edited
    |---sql
         |---init.sql
####add to
import os
import pymysql
STATIC_ROOT = '/static'
pymysql.install_as_MySQLdb()

####Fix
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ.get('MYSQL_DATABASE'),
        'USER': os.environ.get('MYSQL_USER'),
        'PASSWORD': os.environ.get('MYSQL_PASSWORD'),
        'HOST': 'mysql',
        'PORT': '3306',
    }
}
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
$ docker-compose up -d
$ docker-compose exec django ./manage.py makemigrations
$ docker-compose exec django ./manage.py migrate
$ docker-compose exec django ./manage.py collectstatic

Verification

reference

Recommended Posts

Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
Build Python + django + nginx + MySQL environment using docekr
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
Create Nginx + uWSGI + Python (Django) environment with docker
(Case) Django (DRF) + uWSGI + Nginx + MySQL docker-compose.yml sample
Build a Python virtual environment using venv (Django + MySQL ①)
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
[Linux Nginx] Command collection used for initial setting of Web server
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
[Django] Memorandum of environment construction procedure
Deploy Django in 3 minutes using docker-compose
Create a web application execution environment of Python3.4 + Nginx + uWSGI + Flask with haste using venv on Ubuntu 14.04 LTS
Consider the description of Dockerfile (Django + MySQL②)
WEB application development using Django [Initial settings]
CSS environment created in 10 minutes using Django
Create Python + uWSGI + Nginx environment with Docker
Deploy Django (Ubuntu 14.04 LTS + Nginx + uWSGI + Supervisor)
Python execution server construction (Python + uWSGI + Django + Nginx)
Consider the description of docker-compose.yml (Django + MySQL ③)
Build NGINX + NGINX Unit + MySQL environment with Docker