Rework on Docker image

This commit is contained in:
Yohann D'ANELLO 2020-06-21 20:27:42 +02:00
parent 518de596bb
commit b46854e479
6 changed files with 56 additions and 20 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
__pycache__
media
db.sqlite3

View File

@ -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"]

View File

@ -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):
"""

View File

@ -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

23
nginx_note.conf_docker Normal file
View File

@ -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/;
}
}

View File

@ -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')