Unverified Commit 56dc7ee7 authored by Ian Sillitoe's avatar Ian Sillitoe Committed by GitHub
Browse files

Merge pull request #1 from bienchen/master

Docker-Compose Deployment
parents 082f61f0 2773e4d9
FROM python:3.7.3-slim-stretch
ARG CATHSMAPI_CODEBASE=fromlocal
FROM python:3.7.4-slim-buster as cath_sm_api_basis
# To build it outside of docker-compose, in the toplevel directory of the repository,
# run:
......@@ -8,13 +9,44 @@ FROM python:3.7.3-slim-stretch
# where
# - "-t cathapi" just defines a name/ tag
#
# By default, the images is created using a local checkout of the Git repo. To
# let Docker check out the repo for you during build time, start the build
# command using `--build-arg CATHSMAPI_CODEBASE=fromgitrepo`.
#
# Be aware: some (secret) environment variables are passed by an .env file
# this is copied here from cathapi/Docker/cathapi/.env .
# Get API code inside the image
ENV SRC_DIR="/cathapi"
RUN /bin/mkdir $SRC_DIR
COPY ./ $SRC_DIR
# Get API code inside the image - we can either use a local checkout of the
# Git repository by using CATHSMAPI_CODEBASE=fromlocal or check out the Git repo
# during build time by CATHSMAPI_CODEBASE=fromgitrepo. Due to the use of the
# "ONBUILD" instruction, copying files from the Docker build context OR
# checkout of the Git repo is ONLY executed on what ever
# "FROM build_${CATHSMAPI_CODEBASE}" calls. That is, if you want to build
# containers and just have this Dockerfile lying around in your home directory,
# no files from your hard drive will be copied inside the image.
FROM cath_sm_api_basis as build_fromlocal
ONBUILD RUN /bin/mkdir $SRC_DIR
ONBUILD COPY ./ $SRC_DIR
FROM cath_sm_api_basis as build_fromgitrepo
ONBUILD RUN apt-get update && \
apt-get install -y --no-install-recommends \
git && \
/bin/rm -rf /var/lib/apt/lists/* && \
/usr/bin/git clone https://github.com/bienchen/cath-swissmodel-api.git \
/tmp/cath-swissmodel-api.git && \
if test -z "$CATHSMAPI_GITTAG"; then \
cd /tmp/cath-swissmodel-api.git/cathapi && \
git checkout $CATHSMAPI_GITTAG && \
cd /; \
fi && \
/bin/mv /tmp/cath-swissmodel-api.git/cathapi $SRC_DIR && \
/bin/rm -rf rm /tmp/cath-swissmodel-api.git && \
apt-get purge -y --auto-remove git
FROM build_${CATHSMAPI_CODEBASE}
WORKDIR $SRC_DIR
......@@ -47,8 +79,6 @@ RUN /bin/date | /usr/bin/md5sum > secret_key.txt
COPY Docker/cathapi/entrypoint.sh $SRC_DIR
COPY Docker/cathapi/.env $SRC_DIR
# Create dedicated user
RUN adduser --system --ingroup users cathapi && \
# Create volume mountpoint for the (still sqlite) database
......@@ -68,3 +98,5 @@ USER cathapi:users
# after every start of the container.
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1
# LocalWords: repo arg CATHSMAPI CODEBASE fromgitrepo fromlocal ONBUILD
......@@ -74,12 +74,15 @@ python3 manage.py collectstatic --noinput
# create root account for Django admin page
>&2 echo "Create superuser for the database"
python3 manage.py shell << END
python3 manage.py shell << END
import os
from django.contrib.auth.models import User
try:
User.objects.get(username='admin')
User.objects.get(username=os.environ['DJANGO_DB_ADMIN_USR'])
except User.DoesNotExist:
User.objects.create_superuser('admin', 'ad@m.in', 'admin')
User.objects.create_superuser(os.environ['DJANGO_DB_ADMIN_USR'],
os.environ['DJANGO_DB_ADMIN_ML'],
os.environ['DJANGO_DB_ADMIN_PW'])
except Exception as dexc:
if str(dexc) == 'UNIQUE constraint failed: auth_user.username':
pass
......
version: "3.3"
version: "3.5"
# Build all images:
#
......@@ -8,9 +8,46 @@ version: "3.3"
#
# $ docker-compose -f Docker/docker-compose.yml up
#
# Please note: Environment variables used by this Docker Compose setup are
# spawned from a '.env' file. This file needs to be in the same
# directory as the docker-compose command is run. Variables in this
# file are defined by '<VARIABLE NAME>=<VALUE>'. Variables needed
# are:
# POSTGRES_PASSWORD - password for the superuser of Postgres, not
# the password of the Django user accessing
# the database
# POSTGRES_DB - name of the database that will be created for
# the CATH-SM-API
# DJANGO_DB_USR - dedicated datbase user for the Django app,
# postgres will create it in the database on
# first startup, Django uses it to log into the
# database
# DJANGO_DB_PW - Password for the dedicated database user as
# scram-sha-256 hash. to generate a hash for a
# password, set up a mock-up PostgreSQL database,
# set encryption to scram-sha-256 and create a
# user with DJANGO_DB_USR and DJANGO_DB_CLR_PW.
# Then run
# `select usename,passwd from PG_SHADOW;`.
# DJANGO_DB_CLR_PW - Cleartext password for the dedicated
# database user
# DJANGO_DB_ADMIN_USR - the administrative user for Django, the
# one which would normally go into
# `manage.py createsuperuser`
# DJANGO_DB_ADMIN_PW - password for the Django superuser
# DJANGO_DB_ADMIN_ML - mail address for the Django superuser
# CATHSMAPI_CODEBASE - there is a default for that one, it
# switches between using a local checkout
# of the Git repository or fetch a new one
# to build images; 'fromlocal' (the default)
# uses the local checkout, 'fromgitrepo'
# clones the repo during build.
# CATHSMAPI_GITTAG - If CATHSMAPI_CODEBASE=fromgitrepo,
# determines the branch/ tag to be used from
# the Git repository
volumes:
django-sqlite-db:
db-store:
www-static:
www-logs:
......@@ -29,13 +66,21 @@ services:
build:
context: ../
dockerfile: Docker/cathapi/Dockerfile
args:
- CATHSMAPI_CODEBASE=$CATHSMAPI_CODEBASE
- CATHSMAPI_GITTAG=$CATHSMAPI_GITTAG
depends_on:
- cathapi-redis
- postgres
environment:
- CATHAPI_DEBUG=CONTAINER
- POSTGRES_DB=$POSTGRES_DB
- DJANGO_DB_ADMIN_USR=$DJANGO_DB_ADMIN_USR
- DJANGO_DB_ADMIN_PW=$DJANGO_DB_ADMIN_PW
- DJANGO_DB_ADMIN_ML=$DJANGO_DB_ADMIN_ML
- DJANGO_DB_USR=$DJANGO_DB_USR
- DJANGO_DB_CLR_PW=$DJANGO_DB_CLR_PW
volumes:
- django-sqlite-db:/cathapi-data
- www-static:/static
command: /cathapi/entrypoint.sh gunicorn cathapi.wsgi -b 0.0.0.0:8080
......@@ -43,6 +88,22 @@ services:
restart: always
build:
context: ./postgres/
# Increase /dev/shm as hinted by the Docker Library page for Postgres
# https://github.com/docker-library/docs/tree/master/postgres/
# "shm_size" needs Docker-Compose v.3.5, before this file was 3.3.
shm_size: '256MB'
environment:
- POSTGRES_PASSWORD=$POSTGRES_PASSWORD
- POSTGRES_DB=$POSTGRES_DB
- DJANGO_DB_USR=$DJANGO_DB_USR
- DJANGO_DB_PW=$DJANGO_DB_PW
- POSTGRES_INITDB_ARGS=--auth=scram-sha-256
volumes:
- db-store:/var/lib/postgresql/data/
entrypoint:
- docker-entrypoint.sh
- -c
- 'config_file=/postgresql.conf'
nginx:
restart: always
......@@ -61,8 +122,16 @@ services:
build:
context: ../
dockerfile: Docker/cathapi/Dockerfile
args:
- CATHSMAPI_CODEBASE=$CATHSMAPI_CODEBASE
- CATHSMAPI_GITTAG=$CATHSMAPI_GITTAG
depends_on:
- cathapi-redis
environment:
- CATHAPI_DEBUG=CONTAINER
- I_AM_CELERY=1
command: celery -A cathapi worker
# LocalWords: env DJANGO USR Django py createsuperuser CATHSMAPI CODEBASE
# LocalWords: fromlocal fromgitrepo repo GITTAG sha PostgreSQL CLR usename
# LocalWords: passwd
\set cathapiuser `echo "$DJANGO_DB_USR"`
\set cathapipw `echo "$DJANGO_DB_PW"`
CREATE USER :cathapiuser PASSWORD :'cathapipw' CREATEROLE LOGIN;
\set cathapidb `echo "$POSTGRES_DB"`
GRANT ALL PRIVILEGES ON DATABASE :cathapidb TO :cathapiuser;
......@@ -3,10 +3,8 @@ FROM postgres:11.4-alpine
# For more information check https://hub.docker.com/_/postgres and
# https://docs.docker.com/samples/library/postgres/
USER postgres
COPY 10-create-django-user.sql /docker-entrypoint-initdb.d/
COPY postgresql.conf /
# shm
# logs
# data
# get port from postgres Dockerfile
# connect django
\ No newline at end of file
USER postgres
client_encoding = 'UTF8'
listen_addresses = '*'
max_connections = 100
password_encryption = scram-sha-256
shared_buffers = 256MB
dynamic_shared_memory_type = posix
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'UTC'
datestyle = 'iso, mdy'
timezone = 'UTC'
default_transaction_isolation = 'read committed'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
default_text_search_config = 'pg_catalog.english'
......@@ -5,60 +5,36 @@ from decouple import config
from .base import *
# Bend the broker to the redis container
BROKER_URL = 'redis://cathapi-redis:6379'
CELERY_RESULT_BACKEND = 'redis://cathapi-redis:6379'
# These settings make sure any tasks run in testing
# are run locally with the 'test' database
CELERY_ALWAYS_EAGER = True
TEST_RUNNER = 'djcelery.contrib.test_runner.CeleryTestSuiteRunner'
IS_CELERY = config('I_AM_CELERY', default=False, cast=bool)
if not IS_CELERY:
# These settings are only relevant for Django instances
DEBUG = True
# Bend the Django cache to use the redis container
CACHES["default"]["LOCATION"] = "redis://cathapi-redis:6379/1"
'''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('PG_NAME', default='cathapi', cast=str),
'USER': config('PG_USER', default='cathapiuser', cast=str),
'PASSWORD': config('POSTGRES_PASSWORD', cast=str),
'HOST': config('PG_HOST', default='postgres', cast=str),
'PORT': config('PG_PORT', default=5432, cast=int),
}
}
'''
# local database config
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join('/cathapi-data', 'db.sqlite3'),
}
}
INSTALLED_APPS += [
# 'debug_toolbar',
]
STATICFILES_DIRS = [
os.path.join('static/'),
]
STATIC_ROOT = '/static'
#MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware', ]
# These settings are only relevant for Django instances
DEBUG = False
# Bend the Django cache to use the redis container
CACHES["default"]["LOCATION"] = "redis://cathapi-redis:6379/1"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('POSTGRES_DB', default='cathapi', cast=str),
'USER': config('DJANGO_DB_USR', default='cathapiuser', cast=str),
'PASSWORD': config('DJANGO_DB_CLR_PW', cast=str),
'HOST': config('PG_HOST', default='postgres', cast=str),
'PORT': config('PG_PORT', default=5432, cast=int),
}
}
STATICFILES_DIRS = [
os.path.join('static/'),
]
STATIC_ROOT = '/static'
......@@ -15,7 +15,7 @@ coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.4
DendroPy==4.4.0
Django>=2.2.4
Django==2.2.4
django-cors-headers==3.0.2
django-debug-toolbar==2.0
django-extensions==2.1.9
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment