From cc931189e87079919ac332d04ea9b63d4428f0c0 Mon Sep 17 00:00:00 2001 From: h4llow3En Date: Thu, 19 Mar 2020 10:13:49 +0100 Subject: [PATCH] Run as alpine docker image and server static files with gunicorn --- .dockerignore | 10 ++++++- Dockerfile | 41 ++++++++++++++-------------- boot.sh | 9 ++++++ docs/docker/plain/docker-compose.yml | 3 -- recipes/settings.py | 6 ++++ requirements.txt | 1 + 6 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 boot.sh diff --git a/.dockerignore b/.dockerignore index 6975847e..668c490d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,4 +7,12 @@ docker-compose* .gitignore README.md LICENSE -.vscode \ No newline at end of file +.vscode +.env +.env.template +.github +.idea +LICENSE.md +docs +nginx +update.sh \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f2d9c0aa..3f465dfb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ No newline at end of file +# 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"] \ No newline at end of file diff --git a/boot.sh b/boot.sh new file mode 100644 index 00000000..ffab2425 --- /dev/null +++ b/boot.sh @@ -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 \ No newline at end of file diff --git a/docs/docker/plain/docker-compose.yml b/docs/docker/plain/docker-compose.yml index 30f32c48..cdde569b 100644 --- a/docs/docker/plain/docker-compose.yml +++ b/docs/docker/plain/docker-compose.yml @@ -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: diff --git a/recipes/settings.py b/recipes/settings.py index 1c1532a6..ab58c511 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -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' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1e99bb04..d87ec433 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,5 @@ lxml webdavclient3 python-dotenv psycopg2-binary +whitenoise gunicorn