Skip to content

Commit

Permalink
Run as alpine docker image and server static files with gunicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
h4llow3En committed Mar 19, 2020
1 parent 97e2593 commit cc93118
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
10 changes: 9 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ docker-compose*
.gitignore
README.md
LICENSE
.vscode
.vscode
.env
.env.template
.github
.idea
LICENSE.md
docs
nginx
update.sh
37 changes: 18 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
FROM python:3.8-slim-buster
FROM python:3.8-alpine

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 apk add --no-cache postgresql-libs gettext zlib libjpeg libxml2-dev libxslt-dev
ENV PYTHONUNBUFFERED 1
EXPOSE 8080

RUN pip3 install -r requirements.txt
# Don't run container as root
RUN adduser -D recipes

RUN apt-get autoremove -y
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

ENV PYTHONUNBUFFERED 1
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

EXPOSE 8080
USER recipes
ENTRYPOINT ["/opt/recipes/boot.sh"]
9 changes: 9 additions & 0 deletions boot.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 0 additions & 3 deletions docs/docker/plain/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions recipes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
]

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',
Expand Down Expand Up @@ -145,3 +148,6 @@

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")

# Serve static files with gzip
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ lxml
webdavclient3
python-dotenv
psycopg2-binary
whitenoise
gunicorn

0 comments on commit cc93118

Please sign in to comment.