basic Docker support

This commit is contained in:
Your Name 2022-09-13 15:47:35 -04:00
parent deeb684ec0
commit 8fa1ba05a7
6 changed files with 125 additions and 10 deletions

23
Dockerfile Normal file
View file

@ -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" ]

View file

@ -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,

View file

@ -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
```

32
docker-compose.yml Normal file
View file

@ -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

13
docker/entrypoint.sh Executable file
View file

@ -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 "$@"

36
docker/start.sh Executable file
View file

@ -0,0 +1,36 @@
#!/bin/bash
cd /app
if [ $# -eq 0 ]; then
echo "Usage: start.sh <server|rq>"
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