Removed Whitenoise and added an Nginx proxy to serve static content

Whitenoise is not suitable for delivery of media. In order to deliver both static and media content Nginx was added as a service to the docker-compose setup. The CMS and Nginx containers share mounts for static and media directory access.
master
androiddrew 6 years ago
parent f54d4a6e73
commit 87b123f1d3

@ -3,27 +3,45 @@ version: '3.6'
services:
cms:
image: androiddrew/datasketch:latest
image: androiddrew/datasketch:0.1.3
command: gunicorn cms.wsgi:application --bind 0.0.0.0:5000 --workers 3
deploy:
replicas: 1
restart_policy:
condition: on-failure
ports:
- 8000:8000
secrets:
- datasketch_key
- pg_passwd_datasketch
environment:
- DJANGO_SETTINGS_MODULE=cms.settings.production
- DB_HOST=db
- DB_PORT=5432
- DB_USER=datasketch
volumes:
- datasketch-media-vol:/code/media
- datasketch-static-vol:/code/staticout
depends_on:
- db
networks:
- datasketch_net
nginx:
image: androiddrew/datasketch_nginx:0.1.0
deploy:
replicas: 1
restart_policy:
condition: on-failure
ports:
- 8000:80
depends_on:
- cms
networks:
- datasketch_net
volumes:
- datasketch-media-vol:/var/www/datasketch/media
- datasketch-static-vol:/var/www/datasketch/static
db:
image: postgres:10.5-alpine
deploy:
@ -62,6 +80,13 @@ volumes:
type: nfs
o: addr=nas1.androiddrew.com,rw
device: ":/volume1/expanse/datasketch_media"
datasketch-static-vol:
driver: local
driver_opts:
type: nfs
o: addr=nas1.androiddrew.com,rw
device: ":/volume1/expanse/datasketch_static"
secrets:
pg_passwd_datasketch:

@ -1,21 +1,38 @@
#FROM python:3.7-alpine
FROM python:3.7
LABEL maintainer="drew@androiddrew.comw"
# Set environmental variables
ENV PYTHONUNBUFFERED 1
ENV DJANGO_ENV production
ENV PYTHONDONTWRITEBYTECODE 1
# Set Working Directory
WORKDIR /code
# install psycopg2
#RUN apk update \
# && apk add --virtual build-deps gcc python3-dev musl-dev \
# && apk add postgresql-dev \
# && pip install psycopg2 \
# && apk del build-deps
# Install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /code/requirements.txt
RUN pip install -r /code/requirements.txt
RUN pip install gunicorn
# Copy entrypoint.sh
COPY ./entrypoint.sh /code/entrypoint.sh
# Copy code to image
COPY . /code/
WORKDIR /code/
RUN python manage.py collectstatic
RUN useradd wagtail && chown -R wagtail /code
RUN apt update && apt -y install netcat
RUN useradd wagtail
RUN chown -R wagtail /code
USER wagtail
EXPOSE 8000
CMD exec gunicorn cms.wsgi:application --bind 0.0.0.0:8000 --workers 3
# run entrypoint.sh
ENTRYPOINT ["/code/entrypoint.sh"]

@ -48,7 +48,6 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
@ -134,7 +133,7 @@ STATICFILES_DIRS = [os.path.join(PROJECT_DIR, "static")]
# See https://docs.djangoproject.com/en/2.1/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_ROOT = os.path.join(BASE_DIR, "staticout")
STATIC_URL = "/static/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

@ -18,12 +18,15 @@ DATABASES = {
"USER": "datasketch",
"PASSWORD": PG_PASSWD,
"HOST": os.environ.get("DB_HOST") if os.environ.get("DB_HOST") else "127.0.0.1",
"PORT": "5432",
"PORT": os.environ.get("DB_PORT") if os.environ.get("DB_PORT") else "5432",
}
}
# Set as the volume map for media on the nas
MEDIA_URL = "/code/media/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticout")
STATIC_URL = "/static/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
LOGGING = {
"version": 1,

@ -0,0 +1,17 @@
#!/bin/sh
echo "Waiting for PostgreSQL"
while ! nc -z $DB_HOST $DB_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
python manage.py flush --no-input
python manage.py migrate
python manage.py collectstatic --no-input
echo "Migratation and static file collection Successful"
exec "$@"

@ -1,4 +1,4 @@
Django>=2.1,<2.2
wagtail>=2.3,<2.4
psycopg2>=2.7.5
whitenoise>=4.1
psycopg2==2.7.5
gunicorn==19.9.0

@ -0,0 +1,4 @@
FROM nginx:1.15.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY prod.conf /etc/nginx/conf.d

@ -0,0 +1,26 @@
upstream wagtail_app {
server cms:5000;
}
server {
listen 80;
location / {
proxy_pass http://wagtail_app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
location /media/ {
alias /var/www/datasketch/media/;
}
location /static/ {
alias /var/www/datasketch/static/;
}
}
Loading…
Cancel
Save