From 64f926857062d193bbb32f4aaae01d18da07187c Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 13 Apr 2021 20:53:00 +0900 Subject: [PATCH] Support async upload in docker-compose.prod.yml --- app/Dockerfile.prod | 2 +- docker-compose.prod.yml | 31 +++++++++++++++++++++++++++++++ nginx/nginx.conf | 5 +++++ tools/prod-celery.sh | 12 ++++++++++++ tools/prod-django.sh | 29 +++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100755 tools/prod-celery.sh create mode 100755 tools/prod-django.sh diff --git a/app/Dockerfile.prod b/app/Dockerfile.prod index d21a29ba..a3b806eb 100644 --- a/app/Dockerfile.prod +++ b/app/Dockerfile.prod @@ -26,4 +26,4 @@ RUN apt-get update \ COPY tools/ /opt/bin/ -ENTRYPOINT [ "/opt/bin/run.sh" ] +ENTRYPOINT [ "/opt/bin/prod-django.sh" ] diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 59d481a3..a136bc79 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -7,10 +7,12 @@ services: dockerfile: app/Dockerfile.prod volumes: - static_volume:/app/staticfiles + - media:/app/media environment: ADMIN_USERNAME: "admin" ADMIN_PASSWORD: "password" ADMIN_EMAIL: "admin@example.com" + CELERY_BROKER_URL: "amqp://doccano:doccano@rabbitmq" DATABASE_URL: "postgres://doccano:doccano@postgres:5432/doccano?sslmode=disable" ALLOW_SIGNUP: "False" DEBUG: "False" @@ -20,6 +22,33 @@ services: - network-backend - network-frontend + celery: + build: + context: . + dockerfile: app/Dockerfile.prod + volumes: + - media:/app/media + entrypoint: ["/opt/bin/prod-celery.sh"] + environment: + PYTHONUNBUFFERED: "1" + CELERY_BROKER_URL: "amqp://doccano:doccano@rabbitmq" + DATABASE_URL: "postgres://doccano:doccano@postgres:5432/doccano?sslmode=disable" + depends_on: + - postgres + - rabbitmq + networks: + - network-backend + + rabbitmq: + image: rabbitmq:3.8 + environment: + RABBITMQ_DEFAULT_USER: "doccano" + RABBITMQ_DEFAULT_PASS: "doccano" + ports: + - 5672:5672 + networks: + - network-backend + nginx: build: context: . @@ -29,6 +58,7 @@ services: GOOGLE_TRACKING_ID: "" volumes: - static_volume:/static + - media:/media ports: - 80:80 depends_on: @@ -50,6 +80,7 @@ services: volumes: postgres_data: static_volume: + media: networks: network-backend: diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 22ac7d7f..2f0da887 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -47,6 +47,11 @@ server { autoindex on; alias /static/; } + + location /media/ { + autoindex on; + alias /media/; + } } server_tokens off; diff --git a/tools/prod-celery.sh b/tools/prod-celery.sh new file mode 100755 index 00000000..9d631467 --- /dev/null +++ b/tools/prod-celery.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -o errexit + +cd /app + +( + echo "Waiting for database" + python manage.py wait_for_db + + echo "Starting celery" + "celery" --app=app worker --loglevel=info --pool=solo +) diff --git a/tools/prod-django.sh b/tools/prod-django.sh new file mode 100755 index 00000000..8c8da596 --- /dev/null +++ b/tools/prod-django.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +set -o errexit + +echo "Making staticfiles" +static_dir=staticfiles +mkdir -p client/dist/static +if [[ ! -d $static_dir ]] || [[ -z $(ls -A $static_dir) ]]; then + echo "Executing collectstatic" + python manage.py collectstatic --noinput; +fi + +echo "Initializing database" +python manage.py wait_for_db +python manage.py migrate +python manage.py create_roles + +echo "Creating admin" +if [[ -n "${ADMIN_USERNAME}" ]] && [[ -n "${ADMIN_PASSWORD}" ]] && [[ -n "${ADMIN_EMAIL}" ]]; then + python manage.py create_admin \ + --username "${ADMIN_USERNAME}" \ + --password "${ADMIN_PASSWORD}" \ + --email "${ADMIN_EMAIL}" \ + --noinput \ + || true +fi + +echo "Starting django" +gunicorn --bind="0.0.0.0:${PORT:-8000}" --workers="${WORKERS:-4}" app.wsgi --timeout 300