[PYTHON] (Case) Django (DRF) + uWSGI + Nginx + MySQL docker-compose.yml sample

Premise

Main files

version: '3.7'
services:
  app:
    container_name: sample-app
    image: sample-app
    build:
      context: ..
      dockerfile: ./docker/python/Dockerfile
    environment:
      DB_HOST: sample-db
      DB_PORT: 3306
    volumes:
      - sock:/var/run/uwsgi
      - static:/var/www/app/sample/static
      - ./log/python:/var/log/python
    depends_on:
      - db
    networks: 
      - sample
  server:
    container_name: sample-server
    image: nginx:1.17.3-alpine
    ports:
      - 8000:80
    volumes:
      - sock:/var/run/uwsgi
      - static:/var/www/app/sample/static
      - ./log/nginx:/var/log/nginx
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf-sample.d
    networks:
      - sample
  db:
    container_name: sample-db
    image: mysql:8.0.17
    ports:
      - 3316:3306
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_DATABASE=sample
      - TZ=Asia/Tokyo
    volumes:
      - data:/var/lib/mysql
      - ./mysql/conf.d/custom.cnf:/etc/mysql/conf.d/custom.cnf
    networks:
      - sample
volumes:
  data:
    driver: local
  sock:
    driver: local
  static:
    driver: local
networks:
  sample:

FROM python:3.7.4-alpine3.9

RUN apk add --update --no-cache \
      build-base \
      linux-headers \
      pcre pcre-dev \
      libffi-dev \
      openssl-dev \
      mariadb-connector-c-dev \
      mysql-client

RUN pip install --upgrade pip

RUN pip install \
      certifi==2019.9.11 \
      chardet==3.0.4 \
      Click==7.0 \
      Django==2.2.4 \
      django-nested-admin==3.2.4 \
      djangorestframework==3.10.2 \
      idna==2.8 \
      module==0.0.2 \      
      mysqlclient==1.4.4 \
      python-monkey-business==1.0.0 \
      pytz==2019.2 \
      PyYaml==5.1 \
      requests==2.22.0 \
      six==1.12.0 \
      sqlparse==0.3.0 \
      urllib3==1.25.3 \
      uwsgi

RUN apk add --update --no-cache tzdata \
 && cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
 && apk del tzdata

ENV APP_NAME=sample \
    ROOT_DIR=/var/www/app \
    SOCK_DIR=/var/run/uwsgi \
    CONF_DIR=docker/python \
    LOGS_DIR=/var/log/python \
    UWSGI_CONF_DIR=/etc/uwsgi/vassals

RUN mkdir -p ${ROOT_DIR}/${APP_NAME} ${SOCK_DIR} ${LOGS_DIR}

WORKDIR ${ROOT_DIR}
COPY ./web ${APP_NAME}
COPY ./${CONF_DIR}/uwsgi/api.ini ${UWSGI_CONF_DIR}/api.ini
COPY ./${CONF_DIR}/startup.sh ./

VOLUME ["${ROOT_DIR}", "${SOCK_DIR}"]
CMD ["/bin/sh", "/var/www/app/startup.sh"]

#!/bin/sh

APP_DIR=/var/www/app/sample

${APP_DIR}/manage.py migrate
${APP_DIR}/manage.py loaddata user.yaml

uwsgi --ini /etc/uwsgi/vassals/api.ini
upstream api {
  server unix:/var/run/uwsgi/uwsgi.sock;
}
 
server {
  listen      80;
  server_name localhost;
  charset     utf-8;
  client_max_body_size 200M;

  access_log /var/log/nginx/access.log main if=$log_ext if=$log_ua;
  error_log  /var/log/nginx/error.log;

  add_header Access-Control-Allow-Methods "OPTIONS, GET, POST, PUT, PATCH, DELETE";

  proxy_set_header 'Access-Control-Allow-Origin'  '*';
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_read_timeout 180;

  location / {
    try_files $uri @uwsgi;
  }

  # static 
  location /static/ {
    root /var/www/app/sample;
  }

  location @uwsgi {
    include    uwsgi_params;
    uwsgi_pass api;
  }
}
user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
  server_tokens off;
  include       /etc/nginx/mime.types;
  include       /etc/nginx/conf-sample.d/*.conf;
  default_type  application/octet-stream;

  access_log  /var/log/nginx/access.log  main if=$log_ua;

  sendfile        on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout  120;

  #gzip  on;

  index   index.html index.htm;

}
.
├── README.md
├── batch
├── deploy
├── docker
│   ├── docker-compose.yml
│   ├── log
│   │   ├── nginx
│   │   │   ├── access.log
│   │   │   └── error.log
│   │   └── python
│   │       ├── error.log
│   │       └── request.log
│   ├── mysql
│   │   └── conf.d
│   │       └── custom.cnf
│   ├── nginx
│   │   ├── conf.d
│   │   │   ├── 00-log.conf
│   │   │   └── api.conf
│   │   └── nginx.conf
│   └── python
│       ├── Dockerfile
│       ├── startup.sh
│       └── uwsgi
│           └── api.ini
├── pip.list
├── venv
└── web
    ├── README.md
    ├── apps
    │   ├── __init__.py
    │   ├── api
    │   │   ├── __init__.py
    │   │   ├── apps.py
    │   │   ├── urls.py
    │   │   └── versioned
    │   │       ├── __init__.py
    │   │       └── v1
    │   │           ├── __init__.py
    │   │           ├── permissions
    │   │           ├── serializers
    │   │           ├── urls.py
    │   │           ├── views
    │   │           └── viewsets
    │   ├── base
    │   │   ├── __init__.py
    │   │   ├── apps.py
    │   │   ├── fixtures
    │   │   ├── migrations
    │   │   └── models
    │   └── console
    │       ├── __init__.py
    │       ├── admin
    │       └── apps.py
    ├── config
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── locale
    ├── manage.py
    └── static

Supplement

Actually, I have customized various configurations on the Django side as well. (API version control, etc.) I was thinking of writing various things at once, but I'm lazy and I don't write at all. For the time being, I wrote an article around Docker. .. ..

If there is a voice saying "I want to see the description (setting) here", I may be happy to write it out

Recommended Posts

(Case) Django (DRF) + uWSGI + Nginx + MySQL docker-compose.yml sample
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
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 Python + django + nginx + MySQL environment using docekr
Create Nginx + uWSGI + Python (Django) environment with docker
Django + MySQL settings
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)