From b61ac547bca6c3c35ca58d6e86e8f0d0a53a2c3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Thu, 16 Apr 2020 16:26:15 +0200
Subject: [PATCH] Dockerization update

---
 .dockerignore               |  3 +++
 Dockerfile                  | 32 ++++++++++++--------------------
 README.md                   | 11 +++++++----
 gunicorn.conf.py            |  5 +++++
 majak/wsgi.py               |  2 +-
 requirements/production.in  |  1 +
 requirements/production.txt | 10 ++++++++++
 run.sh                      | 10 ++++++++++
 8 files changed, 49 insertions(+), 25 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 gunicorn.conf.py
 create mode 100644 requirements/production.in
 create mode 100644 requirements/production.txt
 create mode 100644 run.sh

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 00000000..b570aefc
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,3 @@
+.git
+.venv
+.envrc
diff --git a/Dockerfile b/Dockerfile
index 7d964303..3f80ae5d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,27 +1,19 @@
-# Use an official Python runtime as a parent image
-FROM python:3.7
-LABEL maintainer="hello@wagtail.io"
+FROM python:3.8-slim
 
-# Set environment varibles
-ENV PYTHONUNBUFFERED 1
-ENV DJANGO_ENV dev
+RUN mkdir /app
+WORKDIR /app
 
-COPY ./requirements.txt /code/requirements.txt
-RUN pip install --upgrade pip
-# Install any needed packages specified in requirements.txt
-RUN pip install -r /code/requirements.txt
-RUN pip install gunicorn
+COPY requirements requirements/
+RUN pip install -r requirements/base.txt -r requirements/production.txt
 
-# Copy the current directory contents into the container at /code/
-COPY . /code/
-# Set the working directory to /code/
-WORKDIR /code/
+COPY . .
 
-RUN python manage.py migrate
+RUN useradd app
+RUN chown -R app /app
+USER app
 
-RUN useradd wagtail
-RUN chown -R wagtail /code
-USER wagtail
+ENV DJANGO_SETTINGS_MODULE "majak.settings.production"
 
 EXPOSE 8000
-CMD exec gunicorn majak.wsgi:application --bind 0.0.0.0:8000 --workers 3
+
+CMD ["bash", "run.sh"]
diff --git a/README.md b/README.md
index e5ea509c..6e1547a7 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,9 @@ Maják je CMS pro Pirátské weby. Postavený je na [Wagtail](https://wagtail.io
 
 ## Vývoj
 
-Pro vývoj je definován pomocný `Makefile` pro časté akce.
+Pro vývoj je definován pomocný `Makefile` pro časté akce. Pro nápovědu zavolej:
+
+    $ make help
 
 ### Lokální instalace a spuštění
 
@@ -69,11 +71,11 @@ Při změně modelů vygeneruj migrace pomocí:
 
 #### Spuštění development serveru
 
-Django development server na portu `8003` se spustí příkazem:
+Django development server na portu `8006` se spustí příkazem:
 
     $ make run
 
-Poté můžete otevřít web na adrese [http://localhost:8003](http://localhost:8003)
+Poté můžete otevřít web na adrese [http://localhost:8006](http://localhost:8006)
 
 #### Django shell
 
@@ -114,5 +116,6 @@ K upgrade se používají [pip-tools](https://github.com/jazzband/pip-tools) (`p
 
     $ cd requirements/
     $ pip-compile -U base.in
+    $ pip-compile -U production.in
 
-TĂ­m se vygenerujĂ­ `base.txt`.
+TĂ­m se vygenerujĂ­ `base.txt` a `production.txt`.
diff --git a/gunicorn.conf.py b/gunicorn.conf.py
new file mode 100644
index 00000000..5fbe95a6
--- /dev/null
+++ b/gunicorn.conf.py
@@ -0,0 +1,5 @@
+bind = "0.0.0.0:8000"
+accesslog = "-"
+workers = 4
+max_requests = 1000
+max_requests_jitter = 10
diff --git a/majak/wsgi.py b/majak/wsgi.py
index 08f897e3..53341b67 100644
--- a/majak/wsgi.py
+++ b/majak/wsgi.py
@@ -11,6 +11,6 @@ import os
 
 from django.core.wsgi import get_wsgi_application
 
-os.environ.setdefault("DJANGO_SETTINGS_MODULE", "majak.settings.dev")
+os.environ.setdefault("DJANGO_SETTINGS_MODULE", "majak.settings.production")
 
 application = get_wsgi_application()
diff --git a/requirements/production.in b/requirements/production.in
new file mode 100644
index 00000000..8f22dccf
--- /dev/null
+++ b/requirements/production.in
@@ -0,0 +1 @@
+gunicorn
diff --git a/requirements/production.txt b/requirements/production.txt
new file mode 100644
index 00000000..dc632d0b
--- /dev/null
+++ b/requirements/production.txt
@@ -0,0 +1,10 @@
+#
+# This file is autogenerated by pip-compile
+# To update, run:
+#
+#    pip-compile production.in
+#
+gunicorn==20.0.4          # via -r production.in
+
+# The following packages are considered to be unsafe in a requirements file:
+# setuptools
diff --git a/run.sh b/run.sh
new file mode 100644
index 00000000..b37b5eaf
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+# exit on error
+set -e
+
+# migrate database
+python manage.py migrate
+
+# start webserver
+exec gunicorn -c gunicorn.conf.py majak.wsgi
-- 
GitLab