From b46854e479dac2e2e9d60a1f554cd9ea14e9ef29 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 21 Jun 2020 20:27:42 +0200 Subject: [PATCH] Rework on Docker image --- .dockerignore | 3 +++ Dockerfile | 32 +++++++++++++++++--------------- apps/note/views.py | 2 +- entrypoint.sh | 10 ++++++++-- nginx_note.conf_docker | 23 +++++++++++++++++++++++ note_kfet/middlewares.py | 6 ++++-- 6 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 .dockerignore create mode 100644 nginx_note.conf_docker diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..efc2616a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +__pycache__ +media +db.sqlite3 diff --git a/Dockerfile b/Dockerfile index 80f2c773..60acc6e3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,25 +1,27 @@ -FROM python:3-buster +FROM python:3-alpine ENV PYTHONUNBUFFERED 1 +# Install LaTeX requirements +RUN apk add --no-cache gettext texlive texmf-dist-latexextra texmf-dist-fontsextra nginx gcc libc-dev libffi-dev postgresql-dev libxml2-dev libxslt-dev jpeg-dev + +RUN apk add --no-cache bash + RUN mkdir /code WORKDIR /code - -RUN apt update && \ - apt install -y gettext nginx uwsgi uwsgi-plugin-python3 && \ - rm -rf /var/lib/apt/lists/* - -# Install LaTeX requirements -RUN apt update && \ - apt install -y texlive-latex-extra texlive-fonts-extra texlive-lang-french && \ - rm -rf /var/lib/apt/lists/* +COPY requirements /code/requirements +RUN pip install gunicorn ptpython --no-cache-dir +RUN pip install -r requirements/base.txt -r requirements/cas.txt -r requirements/production.txt --no-cache-dir COPY . /code/ -# Comment what is not needed -RUN pip install -r requirements/base.txt -RUN pip install -r requirements/cas.txt -RUN pip install -r requirements/production.txt +# Configure nginx +RUN mkdir /run/nginx +RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log +RUN ln -sf /code/nginx_note.conf_docker /etc/nginx/conf.d/nginx_note.conf +RUN rm /etc/nginx/conf.d/default.conf ENTRYPOINT ["/code/entrypoint.sh"] -EXPOSE 8000 +EXPOSE 80 + +CMD ["./manage.py", "shell_plus", "--ptpython"] diff --git a/apps/note/views.py b/apps/note/views.py index 6c0ec1e2..81d58441 100644 --- a/apps/note/views.py +++ b/apps/note/views.py @@ -121,7 +121,7 @@ class ConsoView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): table_class = HistoryTable def get_queryset(self, **kwargs): - return super().get_queryset(**kwargs).order_by("-created_at", "-id").all()[:20] + return super().get_queryset(**kwargs).order_by("-created_at", "-id")[:20] def get_context_data(self, **kwargs): """ diff --git a/entrypoint.sh b/entrypoint.sh index 4d0177e8..09cbc3c8 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later @@ -15,4 +15,10 @@ python manage.py compilemessages python manage.py makemigrations python manage.py migrate -python manage.py runserver 0.0.0.0:8000 +nginx + +if [ "$DJANGO_APP_STAGE" = "prod" ]; then + gunicorn -b 0.0.0.0:8000 --workers=2 --threads=4 --worker-class=gthread note_kfet.wsgi --access-logfile '-' --error-logfile '-'; +else + python manage.py runserver 0.0.0.0:8000; +fi diff --git a/nginx_note.conf_docker b/nginx_note.conf_docker new file mode 100644 index 00000000..c40eb59e --- /dev/null +++ b/nginx_note.conf_docker @@ -0,0 +1,23 @@ +upstream note { + server 127.0.0.1:8000; +} + +server { + listen 80; + server_name note; + + location / { + proxy_pass http://note; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static { + alias /code/static/; + } + + location /media { + alias /code/media/; + } +} diff --git a/note_kfet/middlewares.py b/note_kfet/middlewares.py index fff824c5..8987816d 100644 --- a/note_kfet/middlewares.py +++ b/note_kfet/middlewares.py @@ -50,8 +50,10 @@ class SessionMiddleware(object): def __call__(self, request): user = request.user - if 'HTTP_X_FORWARDED_FOR' in request.META: - ip = request.META.get('HTTP_X_FORWARDED_FOR') + if 'HTTP_X_REAL_IP' in request.META: + ip = request.META.get('HTTP_X_REAL_IP') + elif 'HTTP_X_FORWARDED_FOR' in request.META: + ip = request.META.get('HTTP_X_FORWARDED_FOR').split(', ')[0] else: ip = request.META.get('REMOTE_ADDR')