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.
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 |
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/
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/
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.
To create a project, create a project directory under Document.
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.
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
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.
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`.
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
.
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.
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
.
% 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.
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.
% 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.
% 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. 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.
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 the software from the official website of PyCharm. PyCharm official website
After downloading, put it in the application folder and installation is complete.
Choose File> Open and choose the root folder for your project.
When opened, the project will be displayed like this.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