diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..cc9bf6e8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# syntax=docker/dockerfile:1 +FROM python:3.8-slim +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +RUN apt-get update \ + && apt-get install -y --no-install-recommends build-essential libpq-dev git \ + && rm -rf /var/lib/apt/lists/* +COPY requirements.txt /tmp/requirements.txt +RUN pip install --no-cache-dir -r /tmp/requirements.txt \ + && rm -rf /tmp/requirements.txt \ + && useradd -U app_user \ + && install -d -m 0755 -o app_user -g app_user /app/static + +ENV DJANGO_SETTINGS_MODULE=neodb.dev +WORKDIR /app +USER app_user:app_user +COPY --chown=app_user:app_user . . +RUN chmod +x docker/*.sh + +# Section 6- Docker Run Checks and Configurations +ENTRYPOINT [ "docker/entrypoint.sh" ] + +CMD [ "docker/start.sh", "server" ] \ No newline at end of file diff --git a/boofilsic/settings.py b/boofilsic/settings.py index a1301846..6393f4a0 100644 --- a/boofilsic/settings.py +++ b/boofilsic/settings.py @@ -112,10 +112,10 @@ if DEBUG: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'test', - 'USER': 'donotban', - 'PASSWORD': 'donotbansilvousplait', - 'HOST': '172.18.116.29', + 'NAME': os.environ.get('DB_NAME', 'test'), + 'USER': os.environ.get('DB_USER', 'donotban'), + 'PASSWORD': os.environ.get('DB_PASSWORD', 'donotbansilvousplait'), + 'HOST': os.environ.get('DB_HOST', '172.18.116.29'), 'OPTIONS': { 'client_encoding': 'UTF8', # 'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_DEFAULT, @@ -219,9 +219,11 @@ SITE_INFO = { } # Mastodon configs -CLIENT_NAME = 'NiceDB' -APP_WEBSITE = 'https://nicedb.org' -REDIRECT_URIS = "https://nicedb.org/users/OAuth2_login/\nhttps://www.nicedb.org/users/OAuth2_login/" +CLIENT_NAME = os.environ.get('APP_NAME', 'NiceDB') +SITE_INFO['site_name'] = os.environ.get('APP_NAME', 'NiceDB') +APP_WEBSITE = os.environ.get('APP_URL', 'https://nicedb.org') +REDIRECT_URIS = APP_WEBSITE + "/users/OAuth2_login/" + # Path to save report related images, ends with slash REPORT_MEDIA_PATH_ROOT = 'report/' @@ -317,21 +319,23 @@ if DEBUG: # https://django-debug-toolbar.readthedocs.io/en/latest/ # maybe benchmarking before deployment +REDIS_HOST = os.environ.get('REDIS_HOST', '127.0.0.1') + RQ_QUEUES = { 'mastodon': { - 'HOST': 'localhost', + 'HOST': REDIS_HOST, 'PORT': 6379, 'DB': 0, 'DEFAULT_TIMEOUT': -1, }, 'export': { - 'HOST': 'localhost', + 'HOST': REDIS_HOST, 'PORT': 6379, 'DB': 0, 'DEFAULT_TIMEOUT': -1, }, 'doufen': { - 'HOST': 'localhost', + 'HOST': REDIS_HOST, 'PORT': 6379, 'DB': 0, 'DEFAULT_TIMEOUT': -1, diff --git a/doc/GUIDE.md b/doc/GUIDE.md index 7f0325f7..055ca2bb 100644 --- a/doc/GUIDE.md +++ b/doc/GUIDE.md @@ -97,3 +97,10 @@ Requeue failed jobs ``` rq requeue --all --queue doufen ``` + +Run in Docker +``` +docker-compose build +docker-compose up db && docker exec -it app_db_1 psql -U postgres postgres -c 'CREATE EXTENSION hstore WITH SCHEMA public;' # first time only +docker-compose up +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..86f8c09f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3' + +services: + redis: + image: redis:alpine + + db: + image: postgres:14-alpine + volumes: + - /tmp/data/db:/var/lib/postgresql/data + environment: + - POSTGRES_DB=postgres + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + + web: + build: . + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/code + ports: + - "8000:8000" + environment: + - DB_HOST=db + - DB_NAME=postgres + - DB_USER=postgres + - DB_PASSWORD=postgres + - REDIS_HOST=redis + - DJANGO_SETTINGS_MODULE=neodb.dev + depends_on: + - db + - redis diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 00000000..5feb0354 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +python manage.py collectstatic --noinput +python manage.py makemigrations users books movies games music sync mastodon management collection +python manage.py makemigrations +python manage.py migrate users +python manage.py migrate + +exec "$@" diff --git a/docker/start.sh b/docker/start.sh new file mode 100755 index 00000000..a54a76c7 --- /dev/null +++ b/docker/start.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +cd /app + +if [ $# -eq 0 ]; then + echo "Usage: start.sh " + exit 1 +fi + +PROCESS_TYPE=$1 + +if [ "$PROCESS_TYPE" = "server" ]; then + if [ "$DJANGO_DEBUG" = "true" ]; then + gunicorn \ + --reload \ + --bind 0.0.0.0:8000 \ + --workers 2 \ + --worker-class eventlet \ + --log-level DEBUG \ + --access-logfile "-" \ + --error-logfile "-" \ + boofilsic.wsgi + else + gunicorn \ + --bind 0.0.0.0:8000 \ + --workers 2 \ + --worker-class eventlet \ + --log-level DEBUG \ + --access-logfile "-" \ + --error-logfile "-" \ + boofilsic.wsgi + fi +elif [ "$PROCESS_TYPE" = "rq" ]; then + rqworker --with-scheduler doufen export mastodon +fi +