Skip to content

Commit

Permalink
各種設定ファイルを追加。
Browse files Browse the repository at this point in the history
  • Loading branch information
nas user committed May 4, 2021
1 parent e6c17b7 commit c4ca518
Show file tree
Hide file tree
Showing 33 changed files with 1,147 additions and 0 deletions.
69 changes: 69 additions & 0 deletions django/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
FROM alpine:3.12.3
ARG TZ=Asia/Tokyo

LABEL maintainer="user"
LABEL description="build django"

ENV PYTHONUNBUFFERED 1
ENV PYTHONIOENCODING utf-8
ENV SRC_ROOT_PATH /code

# copy python libraries to root directory
COPY ./requirements.txt /

# Install
RUN apk --no-cache update \
&& apk add --no-cache bash tzdata gettext pcre-dev mysql-client bind-tools libuuid syslog-ng \
mariadb-dev mariadb-connector-c-dev libsodium libxml2-dev supervisor \
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
\
# install temporary libraries
\
&& apk add --no-cache --virtual .build-deps \
gcc musl-dev libffi-dev g++ libgcc libstdc++ libxslt-dev python3-dev \
libc-dev linux-headers openssl-dev curl shadow cargo rust \
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
\
# install python3
\
&& apk add --no-cache python3 \
\
# install pip
\
&& python3 -m ensurepip \
&& rm -r /usr/lib/python*/ensurepip \
&& pip3 install --upgrade pip setuptools \
\
# create symbolic link
\
&& ln -sf /usr/bin/python3 /usr/bin/python \
&& ln -sf /usr/bin/pip3 /usr/bin/pip \
\
# install python libraries
\
&& pip install -r /requirements.txt \
\
# create user
\
&& useradd -s /sbin/nologin -M -d /dev/null nginx \
\
# delete temporary libraries
\
&& apk --purge del .build-deps \
&& mkdir -p ${SRC_ROOT_PATH} \
&& mkdir -p /data \
&& echo -n > /var/log/uwsgi.log \
&& echo -n > /var/log/daphne.log \
&& rm -rf /root/.cache /var/cache/apk/* /tmp/*

EXPOSE 8081
# change work directory
WORKDIR ${SRC_ROOT_PATH}
# add supervisor configuration
COPY ./supervisord /data/supervisord
# add shell script
COPY ./start.sh /start.sh
RUN chmod 777 /start.sh

CMD ["/start.sh"]
52 changes: 52 additions & 0 deletions django/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Django
## Run makemigrations and migrate
Migrations are how Django stores changes to your models. To do this, from the command line, run the following command, where "app-name" is a Django's application name.

```bash
python manage.py makemigrations app-name
# ex.
# python manage.py makemigrations sns
```

By running makemigrations, you're telling Django that you've made some changes to your models and that you'd like the chages to be stored as a migration.

There's a command that will run the migrations for you and manage your database schema automatically - that's called migrate.
Now, run migrate to create your model tables in your database.

```bash
python manage.py migrate
```

Please remember the tree-step guid to making model changes:
1. Change your models (in models.py).
1. Run `python manage.py makemigrations app-name` to create migrations for those changes in your application.
1. Run `python manage.py migrate` to apply those changes to the database.

## Create superuser account
To create superuser account, let's run following command, where `DJANGO_SUPERUSER_NAME`, `DJANGO_SUPERUSER_EMAIL`, and `DJANGO_SUPERUSER_PASSWORD` are environment variables
defined by `env_file/django/.env`.

```bash
python manage.py custom_createsuperuser \
--username ${DJANGO_SUPERUSER_NAME} \
--email ${DJANGO_SUPERUSER_EMAIL} \
--password ${DJANGO_SUPERUSER_PASSWORD}
```

## Create multilingual localization messages
```bash
django-admin.py makemessages -l ja
# edit .po files
django-admin.py compilemessages
```

## Set Time Zone for AXES
In MySQL container, let's run following command to set time zone.

```bash
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -u root -proot_password # Must not set space between "-p" and "root_password".
#
# please wait for few minutes ...
#
mysql -u root -proot_password -e "flush tables;" mysql # if this command succeeded, no message is displayed.
```
11 changes: 11 additions & 0 deletions django/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Django==3.1.8
pytz==2021.1
sqlparse==0.3.0
mysqlclient==2.0.3
requests==2.25.1
uWSGI==2.0.19.1
django-import-export==2.5.0
django-filter==2.4.0
django-axes==5.13.0
django-bootstrap-breadcrumbs==0.9.2
django-markdownx==3.0.1
20 changes: 20 additions & 0 deletions django/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Waiting for MySQL database to be ready ...
db_cmd="mysql -h ${DB_HOST} -u ${MYSQL_USER} "-p${MYSQL_PASSWORD}""
counter=1

while ! ${db_cmd} -e "show databases;" > /dev/null 2>&1; do
sleep 1
counter=$(expr ${counter} + 1)
done
echo "[Django]" $(date "+%Y/%m/%d-%H:%M:%S") MySQL database ready! "(${counter}sec)"

# update permission
if [ -e /media ]; then
chmod 777 /media
fi

# start supervisor
echo "[supervisord]" $(date "+%Y/%m/%d-%H:%M:%S") start
exec /usr/bin/supervisord -c /data/supervisord/supervisord.conf
6 changes: 6 additions & 0 deletions django/supervisord/conf.supervisord/syslog_ng.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[program:syslog-ng]
command=/usr/sbin/syslog-ng --foreground --no-caps
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
9 changes: 9 additions & 0 deletions django/supervisord/conf.supervisord/uwsgi.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[program:uwsgi]
# Project directory
directory=/code
# Application
command=uwsgi --ini /uwsgi.ini
user=root
redirect_stderr=true
autostart=true
autorestart=true
20 changes: 20 additions & 0 deletions django/supervisord/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[supervisord]
nodaemon=true
user=root
logfile=/dev/stdout
pidfile=/var/run/supervisord.pid
logfile_maxbytes=0
loglevel=info

[unix_http_server]
file=/var/run/supervisord.sock

; rpc interface for supervisorctl
[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisord.sock

[include]
files=/data/supervisord/conf.supervisord/*.conf
22 changes: 22 additions & 0 deletions django/uwsgi.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[uwsgi]
user = nginx
uid = nginx
gid = nginx
project = manager
base = /code

chdir = %(base)
wsgi-file = %(base)/%(project)/wsgi.py
logger = syslog:uwsgi
module = %(project).wsgi:application
master = true
enable-threads = true
thunder-lock = true
max-requests = 1024
processes = 2
threads = 4
vacuum = true
socket = :8081
close-on-exec = true
die-on-term = true
py-autoreload = 1
116 changes: 116 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
version: '3.4'

services:
# Setup reverse proxy
nginx:
# Build target
build:
context: ./nginx
# Dockerfile
dockerfile: Dockerfile
# image name
image: custom_nginx
restart: always
# Container name
container_name: nginx
# Setup port
ports :
- "443:443"
env_file:
- env_file/nginx/.env
environment:
DEVELOP_MODE: "TRUE"
volumes:
- certs:/etc/letsencrypt
- ./staticfiles/static:/static:ro
- ./staticfiles/media:/media
- ./nginx/uwsgi_params:/etc/nginx/uwsgi_params:ro
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./syslog-ng.conf:/etc/syslog-ng/conf.d/syslog-ng-extra.conf:ro
- ./nginx/default.template:/etc/nginx/template/default.template:ro
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
depends_on:
- django
networks:
- frontend_bridge

django:
# Build target
build:
context: ./django
# Dockerfile
dockerfile: Dockerfile
# Image name
image: custom_django
# Container name
container_name: django
restart: always
# Setup environment variables
env_file:
- env_file/django/.env # for django
- env_file/mysql/.env # for mysql root information
environment:
DB_HOST: mysql
# Relationship config file to container directory
volumes:
- ./staticfiles/media:/media
- ./django/src:/code
- ./django/uwsgi.ini:/uwsgi.ini:ro
- ./syslog-ng.conf:/etc/syslog-ng/conf.d/syslog-ng-extra.conf:ro
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
depends_on:
- mysql
# Setup port
expose:
- "8081"
networks:
- frontend_bridge
- backend_bridge

# database
mysql:
# Build target
build:
context: ./mysql
# Dockerfile
dockerfile: Dockerfile
# Image name
image: custom_mysql.utf8mb4_jp
# Container name
container_name: mysql
restart: always
# Setup environment variables
env_file:
- env_file/mysql/.env # for mysql root information
# Relationship config file to container directory
volumes:
- database:/var/lib/mysql
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
# Setup port
expose:
- "3306"
networks:
- backend_bridge

# Setup network
networks:
frontend_bridge:
name: frontend_network
backend_bridge:
name: backend_network

volumes:
database:
certs:
14 changes: 14 additions & 0 deletions env_file/django/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create .env file
An example follows:

```bash
DJANGO_SECRET_KEY=abcdefghijklmnopqrstuvwxyz0123456789
[email protected]
DJANGO_EMAIL_PASSWORD=password
DJANGO_SUPERUSER_NAME=superuser
[email protected]
DJANGO_SUPERUSER_PASSWORD=superuserpassword
DJANGO_DEBUG_FLAG=True
DJANGO_WWW_VHOST=www.example.com
DJANGO_MEDIA_ROOT=/storage
```
11 changes: 11 additions & 0 deletions env_file/mysql/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Create .env file
An example follows:

```bash
MYSQL_ROOT_PASSWORD=rootpassowrd
MYSQL_DATABASE=database
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_CHARSET=utf8mb4
MYSQL_COLLATION=utf8mb4_unicode_ci
```
14 changes: 14 additions & 0 deletions env_file/nginx/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create .evn file
An example follows:

```bash
MYDNSJP_MASTER_ID=masterid
MYDNSJP_PASSWORD=password
[email protected]
BASE_DOMAIN_NAME=example.com
VHOST_NAME=www.example.com
SSL_CERT_PATH=/etc/nginx/default_certs/default.crt
SSL_CERTKEY_PATH=/etc/nginx/default_certs/default.key
SSL_STAPLING_VERIFY=off
SSL_TRUSTED_CERTIFICATE_PATH=/etc/nginx/default_certs/default.crt
```
Loading

0 comments on commit c4ca518

Please sign in to comment.