Run as alpine docker image and server static files with gunicorn

This commit is contained in:
h4llow3En 2020-03-19 10:13:49 +01:00
parent 97e2593f72
commit cc931189e8
No known key found for this signature in database
GPG Key ID: E30BD90E953CCB30
6 changed files with 45 additions and 25 deletions

View File

@ -7,4 +7,12 @@ docker-compose*
.gitignore
README.md
LICENSE
.vscode
.vscode
.env
.env.template
.github
.idea
LICENSE.md
docs
nginx
update.sh

View File

@ -1,24 +1,23 @@
FROM python:3.8-slim-buster
RUN mkdir /Recipes
WORKDIR /Recipes
ADD . /Recipes/
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install -y \
python3 \
python3-pip \
postgresql-client \
gettext
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt
RUN apt-get autoremove -y
FROM python:3.8-alpine
RUN apk add --no-cache postgresql-libs gettext zlib libjpeg libxml2-dev libxslt-dev
ENV PYTHONUNBUFFERED 1
EXPOSE 8080
EXPOSE 8080
# Don't run container as root
RUN adduser -D recipes
RUN mkdir /opt/recipes
RUN chown recipes:recipes /opt/recipes
WORKDIR /opt/recipes
COPY --chown=recipes:recipes . ./
RUN pwd && ls -lha
RUN chmod +x boot.sh
RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev && \
python -m venv venv && \
venv/bin/pip install -r requirements.txt --no-cache-dir &&\
apk --purge del .build-deps
USER recipes
ENTRYPOINT ["/opt/recipes/boot.sh"]

9
boot.sh Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
source venv/bin/activate
echo "Updating database"
python3 manage.py migrate
python3 manage.py collectstatic --noinput
echo "Done"
exec gunicorn -b :8080 --access-logfile - --error-logfile - recipes.wsgi

View File

@ -15,9 +15,6 @@ services:
restart: always
env_file:
- ./.env
command: "gunicorn --bind 0.0.0.0:8080 recipes.wsgi"
volumes:
- .:/Recipes
depends_on:
- db_recipes
networks:

View File

@ -57,6 +57,9 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
@ -145,3 +148,6 @@ STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
# Serve static files with gzip
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

View File

@ -17,4 +17,5 @@ lxml
webdavclient3
python-dotenv
psycopg2-binary
whitenoise
gunicorn