[PYTHON] Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)

Docker is convenient for local development environment

When building a development environment for Python + Django, you can change the version with pyenv etc., but it is troublesome when developing with a team or developing on another machine, so I want to make it using Docker that builds a virtual environment I think.

Docker is a platform that runs middleware and apps using container technology. It's a little different from virtual machines such as VMware. Anyway, it's convenient, so you should try it. It is also possible to build a web server such as wordpress locally.

Reference: https://knowledge.sakura.ad.jp/13265/

Also, I will use PyCharm for the integrated development environment on macOS.

environment

soft version
OS macOS Catalina version 10.15.4
Docker desktop 2.2.0.5
Docker Engine 19.03.8
Docker Compose 1.25.4
Nginx 1.16.1
uWSGI 2.0.18
MariaDB 10.4.12
Python 3.8.2
Django 3.0
PyCharm 2020.1

Install Docker for mac

Download to install Docker for mac from the official Docker website.

You'll need to sign in to Docker, so create an account here. https://hub.docker.com/

スクリーンショット 2020-04-11 16.15.13.png

When you start Docker for mac, you will be asked for your ID and password, so log in with the account you created earlier.

To use Docker commands, you need to start Docker for mac and sign in.

Reference: https://docs.docker.com/docker-for-mac/

Building an environment with Docker-compose

Docker needs to start up each container such as Nginx, CentOS, MariaDB (MySQL) and connect the containers. Docker-compose describes these configurations and launches multiple containers at once.

Creating a project

To create a project, create a project directory under Document.

Create a Docker-compose configuration file

Describe the configuration of the Docker container in a file.

docker-compose.yml


version: "3.7"

volumes:
  app.db.volume:

services:
  uwsgi:
    build: ./docker
    container_name: app.uwsgi
    command: uwsgi --socket :8081 --module userapp.wsgi --logto /code/userapp/uwsgi.log --py-autoreload 1
    volumes:
      - ./src:/code
      - ./static:/static
    expose:
      - "8081"
    depends_on:
      - db

  nginx:
    image: nginx:1.16
    container_name: app.nginx
    ports:
      - 8080:80
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
      - ./static:/static
    depends_on:
      - uwsgi

  db:
    image: mariadb:10.4
    container_name: app.db
    ports:
      - 3316:3306
    environment:
      MYSQL_ROOT_PASSWORD: dbtest5928
      TZ: 'Asia/Tokyo'
    volumes:
      - app.db.volume:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d

--version: "3.7" : Descriptive version of docker-compose --volumes:: Save the contents of the database.

Explanation of uWSGI container

docker-compose.yml


  uwsgi:
    build: ./docker
    container_name: app.uwsgi
    command: uwsgi --socket :8081 --module userapp.wsgi --logto /code/userapp/uwsgi.log --py-autoreload 1
    volumes:
      - ./src:/code
      - ./static:/static
    expose:
      - "8081"
    depends_on:
      - db

--ʻUwsgi: : Run python with uWSGI. --Start Django with the command: command. --Listen on port 8081. ----module userapp.wsgi` should have the same name as the project name (userapp) generated by Django. --Set log file --Enable auto reload

Nginx container description

docker-compose.yml


  nginx:
    image: nginx:1.16
    container_name: app.nginx
    ports:
      - 8080:80
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
      - ./static:/static
    depends_on:
      - uwsgi

--ports:: Access from outside is port 8080, access inside the container is port 80 --volumes:: Create the Nginx config file in ./nginx/conf.d later.

MariaDB container description

docker-compose.yml


  db:
    image: mariadb:10.4
    container_name: app.db
    ports:
      - 3316:3306
    environment:
      MYSQL_ROOT_PASSWORD: dbtest5928
      TZ: 'Asia/Tokyo'
    volumes:
      - app.db.volume:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d

--ports:: Access from outside is port 3316, access inside the container is port 3306 --ʻEnvironment: : Set root password and timezone --volumes:: The initial SQL for database construction will be created later in ./sql`.

Creating a Dockerfile

This is a script to start when creating a Docker image.

./docker/Dockerfile


FROM python 3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

--FROM python 3.8: Starts based on the python image. --Invalidation of the output buffer to the command line. It is better for debugging to disable the buffer because the result will be unintended due to the display with a time difference. --WORKDIR / code: Execution directory --RUN pip install -r requirements.txt: Install the libraries required for Docker by listing them in requirements.txt.

Create requirements.txt

Describes the library required to execute python when starting Docker.

text:./docker/requirements.txt


Django==3.0
uwsgi==2.0.18
PyMySQL==0.9

--Django is a framework --ʻUwsgi is a python execution server on the web --PyMySQL` is a library for connecting to MySQL (MariaDB) from python.

MariaDB initial script

Create a user and database for DB operations.

item value
User appuser
Password apppass
DB app

sql:./sql/init.sql


CREATE DATABASE IF NOT EXISTS app CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER IF NOT EXISTS 'appuser'@'%' IDENTIFIED BY 'apppass';
GRANT ALL PRIVILEGES ON app.* TO 'appuser'@'%';

FLUSH PRIVILEGES;

--Create a ʻapp database and set it so that it can be accessed by ʻappuser.

Nginx uWSGI config

% curl https://raw.githubusercontent.com/nginx/nginx/master/conf/uwsgi_params > ./nginx/uwsgi_params

You can download the uWSGI config for Nginx from here.

./nginx/uwsgi_params


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;

Contains the parameters required to run uWSGI. Please use it as it is, thinking that it is magical.

Nginx config

conf:nginx/conf.d/app.conf


upstream django {
        server 127.0.0.1:8081;
}       

server {
        listen 80;
        server_name localhost;
        charset utf-8;

        # Static file
        location /static {
                alias /static;
        }

        # Non media 
        location / {
                uwsgi_pass django;
                include /etc/nginx/uwsgi_params;
        }
}

--The uWSGI port is 8081, so set it to ʻupstream django. --listen 80: Access in container is port 80 --location / static: Images, JavaScript and CSS are returned in Nginx. --location /`: All other access is handled by uWSGI.

Project launch and initial build

Build a Django project

% docker-compose build

The image defined by Docker-compose will be downloaded and built.

% docker-compose run uwsgi django-admin.py startproject userapp . 

Build the Djanago project. Don't forget the last ".".

% tree
.
├── docker
│   ├── Dockerfile
│   └── requirements.txt
├── docker-compose.yml
├── nginx
│   ├── conf.d
│   │   └── app.conf
│   └── uwsgi_params
├── sql
│   └── init.sql
├── src
│   └── userapp
│       ├── manage.py
│       └── userapp
│           ├── __init__.py
│           ├── asgi.py
│           ├── settings.py
│           ├── urls.py
│           └── wsgi.py
└── static

Here's the project directory where your Django project builds successfully.

Run Django

% docker-compose up -d
Starting app.db ... done
Starting app.uwsgi ... done
Starting app.nginx ... done

--The -d option runs in the daemon (background).

http://127.0.0.1:8080 I will try to access. download.png If you see a screen like this, you are successful.

% docker-compose down
Stopping app.nginx ... done
Stopping app.uwsgi ... done
Stopping app.db    ... done
Removing userapp_uwsgi_run_ec11989e36c3 ... done
Removing app.nginx                      ... done
Removing app.uwsgi                      ... done
Removing app.db                         ... done
Removing network userapp_default

When exiting, this command will bring it down.

% docker-compose ps  
Name   Command   State   Ports
------------------------------

Execute the command to hack the operating status of Docker, and if nothing is displayed, down is successful.

Installation of integrated development environment PyCharm

PyCharm from JetBeans is useful for developing Django and Python. If you're developing a web app, the paid Professional version, which can also edit Javascript, CSS, and HTML, is useful, but the free Community version is sufficient at first.

Download and install PyCharm

Download the software from the official website of PyCharm. PyCharm official website

After downloading, put it in the application folder and installation is complete.

Load project

Choose File> Open and choose the root folder for your project.

スクリーンショット 2020-04-18 12.51.44.png When opened, the project will be displayed like this.

Connecting Docker's Python container to PyCharm

Professional version is required to run from PyCharm using Docker's Python container. If you are developing light for the time being, I think it is okay to use PyCharm as an editor.

that's all.

reference: https://nmmmk.hatenablog.com/entry/2018/05/01/101126 https://qiita.com/NickelCreate/items/bed3dc9d088b57127ba7

Recommended Posts

Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Build Django + NGINX + PostgreSQL development environment with Docker
Build the fastest Django development environment with docker-compose
Create Nginx + uWSGI + Python (Django) environment with docker
Django development environment construction memo
QGIS3 Python plugin development environment construction with VSCode (macOS)
django project development environment construction
Initial setting of environment using Docker-compose + Django + MySQL + Nginx + uwsgi
[For beginners] Django -Development environment construction-
[Python3] Development environment construction << Windows edition >>
Python development environment construction on macOS
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
Create Python + uWSGI + Nginx environment with Docker
Python3 + venv + VSCode + macOS development environment construction
Python execution server construction (Python + uWSGI + Django + Nginx)
django environment construction
[Python] Build a Django development environment with Docker
Build a Django development environment with Doker Toolbox
From 0 to Django development environment construction to basic operation
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Development digest with Django
Django project environment construction
Python development environment construction
Web application made with Python3.4 + Django (Part.1 Environment construction)
Python environment with docker-compose
python2.7 development environment construction
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Remote debug Django environment created with docker-compose with VS Code
Build a development environment with Poetry Django Docker Pycharm
[Memo] Django development environment
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Minimal website environment with django
API with Flask + uWSGI + Nginx
CI environment construction ~ Python edition ~
ML environment construction with Miniconda
Docker + Django + React environment construction
Django environment development on Windows 10
[MEMO] [Development environment construction] Python
Environment construction, Build -Go edition-
[MEMO] [Development environment construction] wine
Test Driven Development with Django Part 3
Build FastAPI + uvicorn + nginx with docker-compose
Mac + Eclipse (PyDev) + Django environment construction
Test Driven Development with Django Part 4
Get started with Python! ~ ① Environment construction ~
Test Driven Development with Django Part 6
ruby environment construction with aws EC2
[Django] Memorandum of environment construction procedure
Easy Jupyter environment construction with Cloud9
Test Driven Development with Django Part 2
[MEMO] [Development environment construction] Jupyter Notebook
Automate environment construction with Shell Script
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Using Chainer with CentOS7 [Environment construction]
Emacs Python development environment construction memo
Ubuntu Desktop 20.04 development environment construction memo