From 805e2911f8605e64e7ab464a2d564b4f0603523a Mon Sep 17 00:00:00 2001 From: Jan Koppe Date: Sat, 25 Apr 2020 10:22:22 +0200 Subject: [PATCH] run nginx and gunicorn with supervisord in container --- Dockerfile | 11 ++++++----- docker-compose.dev.yaml | 4 ++-- docker/nginx.conf | 21 +++++++++++++++++++++ docker/supervisord.conf | 33 +++++++++++++++++++++++++++++++++ start.sh | 32 +++++++++----------------------- 5 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 docker/nginx.conf create mode 100644 docker/supervisord.conf diff --git a/Dockerfile b/Dockerfile index 273604b..1600354 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,23 +4,24 @@ WORKDIR /app # set env ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 -EXPOSE 8000 -RUN apk update \ - && apk add postgresql-dev gcc python3-dev musl-dev gettext postgresql-client +# install required packages +RUN apk add --no-cache postgresql-dev gcc python3-dev musl-dev gettext postgresql-client nginx supervisor # install dependencies ADD ./requirements.txt . RUN pip install -r requirements.txt +# add supervisor and nginx configs +ADD ./docker/nginx.conf /etc/nginx/nginx.conf +ADD ./docker/supervisord.conf /etc/supervisord.conf + # add user RUN addgroup -S portier && adduser -S portier -G portier # add code ADD --chown=portier:portier . /app - RUN ./manage.py collectstatic --noinput --link RUN ./manage.py compilemessages -USER portier CMD ["/app/start.sh", "migrate_start"] diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index 324fb32..ff2c6a5 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -1,10 +1,10 @@ version: '2.4' services: - web: + app: build: . ports: - - 8000:8000 + - 80:80 depends_on: - postgres - redis diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..f2b3680 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,21 @@ +user nginx; +worker_processes auto; +events { + worker_connections 1024; +} +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + sendfile on; + tcp_nodelay on; + server { + listen 80 default_server; + listen [::]:80 default_server; + location / { + proxy_pass http://127.0.0.1:8000; + } + location /static { + alias /app/staticfiles; + } + } +} diff --git a/docker/supervisord.conf b/docker/supervisord.conf new file mode 100644 index 0000000..06d6eae --- /dev/null +++ b/docker/supervisord.conf @@ -0,0 +1,33 @@ +[supervisord] +logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log) +logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) +logfile_backups=10 ; (num of main logfile rotation backups;default 10) +loglevel=info ; (log level;default info; others: debug,warn,trace) +nodaemon=true ; (start in foreground if true;default false) +user=root + +[program:gunicorn] +directory=/app +command=/usr/local/bin/gunicorn -w 4 --bind 127.0.0.1:8000 portier.wsgi +autostart=true +autorestart=true +priority=5 +stdout_events_enabled=true +stderr_events_enabled=true +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +user=portier + +[program:nginx] +command=/usr/sbin/nginx -g 'pid /tmp/nginx.pid; daemon off;' +autostart=true +autorestart=true +priority=10 +stdout_events_enabled=true +stderr_events_enabled=true +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 diff --git a/start.sh b/start.sh index 333b161..f591351 100755 --- a/start.sh +++ b/start.sh @@ -1,10 +1,5 @@ #!/bin/sh -migrate() { - python manage.py makemigrations - python manage.py migrate -} - wait_for_redis() { echo "Trying to connect to redis at ${REDIS_HOST:-redis}:${REDIS_PORT:-6379} ..." while ! nc -z ${REDIS_HOST:-redis} ${REDIS_PORT:-6379}; @@ -23,21 +18,12 @@ wait_for_database() { echo "Successfully connected to database. Continuing."; } -case $1 in - "create_superuser" ) - wait_for_redis - wait_for_database - python manage.py createsuperuser --no-input --username "${ADMIN_USER:-admin}" --email "${ADMIN_EMAIL:-post@chaoswest.tv}" - ;; - "migrate_start" ) - wait_for_redis - wait_for_database - migrate - gunicorn -w 4 --bind 0.0.0.0:${EXPOSE_PORT:-8000} portier.wsgi - ;; - "only_start" ) - wait_for_redis - wait_for_database - gunicorn -w 4 --bind 0.0.0.0:${EXPOSE_PORT:-8000} portier.wsgi - ;; -esac +migrate() { + python manage.py makemigrations + python manage.py migrate +} + +wait_for_redis +wait_for_database +migrate +supervisord -n -c /etc/supervisord.conf