From f018f7b361efa3cc4e6077ac20532f7f5a2d4655 Mon Sep 17 00:00:00 2001
From: Michal Holub <holub.michal@gmail.com>
Date: Fri, 16 Mar 2018 13:07:43 +0100
Subject: [PATCH] Static files now served with nginx

static nginx part of docker. Supervisor used to control both gunicorn
and nginx
---
 Dockerfile            | 26 +++++++++++++++++++++++---
 conf/entrypoint.sh    |  9 +++++++++
 conf/nginx.conf       | 13 +++++++++++++
 conf/supervisor.conf  | 23 +++++++++++++++++++++++
 openlobby/settings.py |  6 ++----
 5 files changed, 70 insertions(+), 7 deletions(-)
 create mode 100644 conf/entrypoint.sh
 create mode 100644 conf/nginx.conf
 create mode 100644 conf/supervisor.conf

diff --git a/Dockerfile b/Dockerfile
index 430af53..ec15ae7 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,10 +3,30 @@ FROM python:3.6
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
-RUN pip install -r requirements.txt
-RUN pip install gunicorn
+
+RUN apt-get update && apt-get install -y --no-install-recommends \
+        nginx \
+        supervisor \
+    && apt-get clean \
+    && rm -rf /var/lib/apt/lists/* \
+    && apt-get autoremove -y --purge
+
+COPY ./requirements.txt /code/requirements.txt
+RUN pip install -r requirements.txt && \
+    pip install gunicorn && \
+    rm /etc/nginx/sites-enabled/default && \
+    echo "daemon off;" >> /etc/nginx/nginx.conf
+
 ADD . /code/
 
+RUN DATABASE_URL=none python manage.py collectstatic --noinput
+
+COPY conf/supervisor.conf /etc/supervisor/conf.d/supervisor.conf
+COPY conf/nginx.conf /etc/nginx/conf.d/openlobby-server.conf
+COPY conf/entrypoint.sh ./
+
+WORKDIR /code
+
 EXPOSE 8010
 
-CMD ["sh", "-c", "make migrate & gunicorn -w 4 -b 0.0.0.0:8010 --access-logfile - --error-logfile - --capture-output openlobby.wsgi"]
+ENTRYPOINT ["./entrypoint.sh"]
diff --git a/conf/entrypoint.sh b/conf/entrypoint.sh
new file mode 100644
index 0000000..1d11868
--- /dev/null
+++ b/conf/entrypoint.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+# migrate
+python manage.py migrate
+
+# Create log dirs and files
+mkdir -p $( dirname $(cat /etc/supervisor/supervisord.conf  | grep logfile= | grep "\.log" | sed s/.*logfile=// ) )
+touch $( cat /etc/supervisor/supervisord.conf  | grep logfile= | grep "\.log" | sed s/.*logfile=// )
+exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisor.conf --nodaemon
\ No newline at end of file
diff --git a/conf/nginx.conf b/conf/nginx.conf
new file mode 100644
index 0000000..9fb806a
--- /dev/null
+++ b/conf/nginx.conf
@@ -0,0 +1,13 @@
+server {
+    listen 80;
+    server_name 127.0.0.1;
+
+    location /static {
+        alias /code/static;
+    }
+
+    location / {
+    	proxy_pass http://0.0.0.0:8000;
+        proxy_set_header Host $http_host;
+    }
+}
diff --git a/conf/supervisor.conf b/conf/supervisor.conf
new file mode 100644
index 0000000..eaf08f1
--- /dev/null
+++ b/conf/supervisor.conf
@@ -0,0 +1,23 @@
+[program:app-gunicorn]
+command = gunicorn -w 4 -b 0.0.0.0:8010 --access-logfile - --error-logfile - --capture-output openlobby.wsgi
+priority=2
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+
+[program:nginx-app]
+command = /usr/sbin/nginx
+priority=3
+stdout_logfile=/dev/stdout
+stdout_logfile_maxbytes=0
+stderr_logfile=/dev/stderr
+stderr_logfile_maxbytes=0
+
+
+[supervisord]
+logfile = /var/log/supervisor/supervisord.log
+logfile_maxbytes = 10MB
+logfile_backups = 5
+loglevel = info
+pidfile = /var/run/supervisord.pid
\ No newline at end of file
diff --git a/openlobby/settings.py b/openlobby/settings.py
index d598d3d..ea44c18 100644
--- a/openlobby/settings.py
+++ b/openlobby/settings.py
@@ -8,7 +8,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 DEBUG = 'DEBUG' in os.environ
 
 # SECURITY WARNING: keep the secret key used in production secret!
-SECRET_KEY = os.environ.get('SECRET_KEY')
+SECRET_KEY = os.environ.get('SECRET_KEY','very-secret-key')
 if not SECRET_KEY:
     if DEBUG:
         SECRET_KEY = 'not-secret-at-all'
@@ -114,11 +114,9 @@ USE_L10N = True
 USE_TZ = True
 
 
-# Static files (CSS, JavaScript, Images)
-
+STATIC_ROOT = os.path.join(BASE_DIR, 'static')
 STATIC_URL = '/static/'
 
-
 LOGGING = {
     'version': 1,
     'handlers': {
-- 
GitLab