Skip to content

Commit

Permalink
fix: collect static files during container build
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Jul 13, 2021
1 parent 3a9e939 commit 6c15a96
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 55 deletions.
27 changes: 12 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ FROM python:${PYTHON_VERSION} as python

# Python build stage
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=production

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential wget \
# psycopg2 dependencies
libpq-dev

# Requirements are installed here to ensure they will be cached.
Expand All @@ -32,26 +29,21 @@ COPY ./requirements .
RUN pip wheel --wheel-dir /usr/src/app/wheels \
-r ${BUILD_ENVIRONMENT}.txt


# Python 'run' stage
FROM python as python-run-stage
ARG BUILD_ENVIRONMENT=production
ARG APP_HOME=/app
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
ENV DJANGO_SETTINGS_MODULE config.settings.production
WORKDIR ${APP_HOME}


# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
# wget
wget \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -63,12 +55,17 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels/
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
&& rm -rf /wheels/


COPY ./entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

# copy application code to WORKDIR
COPY --from=client-builder ${APP_HOME} ${APP_HOME}

ENTRYPOINT ["/entrypoint"]
COPY ./prepare /prepare
RUN sed -i 's/\r$//g' /prepare
RUN chmod +x /prepare
RUN /prepare

COPY ./start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
RUN /start

ENTRYPOINT ["/start"]
1 change: 1 addition & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ steps:
'--region', 'europe-west1',
'--platform', 'managed',
'--allow-unauthenticated',
'--min-instances', '1',
'--add-cloudsql-instances', '${_CLOUDSQL_INSTANCE_CONNECTION_NAME}',
'--set-env-vars', 'GOOGLE_CLOUD_PROJECT=$PROJECT_ID,SETTINGS_NAME=mle_django_settings'
]
Expand Down
1 change: 0 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"ATOMIC_REQUESTS": True,
}
}
print(f"databases: {DATABASES}")

# URLS
# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
# django-compressor
# ------------------------------------------------------------------------------
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_ENABLED
COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", default=True)
COMPRESS_ENABLED = True
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_URL
COMPRESS_URL = STATIC_URL # noqa F405
# https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE
Expand Down
38 changes: 0 additions & 38 deletions entrypoint

This file was deleted.

18 changes: 18 additions & 0 deletions pepfar_mle/common/migrations/0002_auto_20210713_0231.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.12 on 2021-07-12 23:31

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='facilityattachment',
name='content_type',
field=models.CharField(choices=[('image/png', 'PNG'), ('image/jpeg', 'JPEG'), ('application/pdf', 'PDF'), ('application/vnd.ms-excel', 'xlsx'), ('application/msword', 'doc'), ('application/vnd.openxmlformats-officedocument.wordprocessingml.document.docx', 'docx'), ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xlsx'), ('text/plain', 'text')], max_length=100),
),
]
11 changes: 11 additions & 0 deletions prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

python manage.py collectstatic --noinput
>&2 echo 'Collected static files...'

python manage.py compress
>&2 echo 'Compressed static assets...'
13 changes: 13 additions & 0 deletions start
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

python manage.py migrate
>&2 echo 'Ran database migrations...'

python manage.py createcachetable
>&2 echo 'Created the cache table...'

/usr/local/bin/gunicorn config.asgi --bind 0.0.0.0:$PORT --chdir=/app -k uvicorn.workers.UvicornWorker

0 comments on commit 6c15a96

Please sign in to comment.