|
1 | 1 | {% if cookiecutter.js_task_runner == 'Gulp' -%} |
2 | 2 | FROM node:10-stretch-slim as client-builder |
3 | 3 |
|
4 | | -WORKDIR /app |
5 | | -COPY ./package.json /app |
| 4 | +ARG APP_HOME=/app |
| 5 | +WORKDIR ${APP_HOME} |
| 6 | + |
| 7 | +COPY ./package.json ${APP_HOME} |
6 | 8 | RUN npm install && npm cache clean --force |
7 | | -COPY . /app |
| 9 | +COPY . ${APP_HOME} |
8 | 10 | RUN npm run build |
9 | 11 |
|
10 | | -# Python build stage |
11 | 12 | {%- endif %} |
12 | | -FROM python:3.8-slim-buster |
13 | 13 |
|
14 | | -ENV PYTHONUNBUFFERED 1 |
| 14 | +ARG PYTHON_VERSION=3.8-slim-buster |
| 15 | + |
| 16 | +# define an alias for the specfic python version used in this file. |
| 17 | +FROM python:${PYTHON_VERSION} as python |
15 | 18 |
|
16 | | -RUN apt-get update \ |
| 19 | +# Python build stage |
| 20 | +FROM python as python-build-stage |
| 21 | + |
| 22 | +ARG BUILD_ENVIRONMENT=production |
| 23 | + |
| 24 | +# Install apt packages |
| 25 | +RUN apt-get update && apt-get install --no-install-recommends -y \ |
17 | 26 | # dependencies for building Python packages |
18 | | - && apt-get install -y build-essential \ |
| 27 | + build-essential \ |
19 | 28 | # psycopg2 dependencies |
20 | | - && apt-get install -y libpq-dev \ |
| 29 | + libpq-dev |
| 30 | + |
| 31 | +# Requirements are installed here to ensure they will be cached. |
| 32 | +COPY ./requirements . |
| 33 | + |
| 34 | +# Create Python Dependency and Sub-Dependency Wheels. |
| 35 | +RUN pip wheel --wheel-dir /usr/src/app/wheels \ |
| 36 | + -r ${BUILD_ENVIRONMENT}.txt |
| 37 | + |
| 38 | + |
| 39 | +# Python 'run' stage |
| 40 | +FROM python as python-run-stage |
| 41 | + |
| 42 | +ARG BUILD_ENVIRONMENT=production |
| 43 | +ARG APP_HOME=/app |
| 44 | + |
| 45 | +ENV PYTHONUNBUFFERED 1 |
| 46 | +ENV PYTHONDONTWRITEBYTECODE 1 |
| 47 | +ENV BUILD_ENV ${BUILD_ENVIRONMENT} |
| 48 | + |
| 49 | +WORKDIR ${APP_HOME} |
| 50 | + |
| 51 | +RUN addgroup --system django \ |
| 52 | + && adduser --system --ingroup django django |
| 53 | + |
| 54 | + |
| 55 | +# Install required system dependencies |
| 56 | +RUN apt-get update && apt-get install --no-install-recommends -y \ |
| 57 | + # psycopg2 dependencies |
| 58 | + libpq-dev \ |
21 | 59 | # Translations dependencies |
22 | | - && apt-get install -y gettext \ |
| 60 | + gettext \ |
23 | 61 | # cleaning up unused files |
24 | 62 | && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ |
25 | 63 | && rm -rf /var/lib/apt/lists/* |
26 | 64 |
|
27 | | -RUN addgroup --system django \ |
28 | | - && adduser --system --ingroup django django |
| 65 | +# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction |
| 66 | +# copy python dependency wheels from python-build-stage |
| 67 | +COPY --from=python-build-stage /usr/src/app/wheels /wheels/ |
| 68 | + |
| 69 | +# use wheels to install python dependencies |
| 70 | +RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ |
| 71 | + && rm -rf /wheels/ |
29 | 72 |
|
30 | | -# Requirements are installed here to ensure they will be cached. |
31 | | -COPY ./requirements /requirements |
32 | | -RUN pip install --no-cache-dir -r /requirements/production.txt \ |
33 | | - && rm -rf /requirements |
34 | 73 |
|
35 | 74 | COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint |
36 | 75 | RUN sed -i 's/\r$//g' /entrypoint |
@@ -58,14 +97,17 @@ RUN sed -i 's/\r$//g' /start-flower |
58 | 97 | RUN chmod +x /start-flower |
59 | 98 | {%- endif %} |
60 | 99 |
|
| 100 | + |
| 101 | +# copy application code to WORKDIR |
61 | 102 | {%- if cookiecutter.js_task_runner == 'Gulp' %} |
62 | | -COPY --from=client-builder --chown=django:django /app /app |
| 103 | +COPY --from=client-builder --chown=django:django ${APP_HOME} ${APP_HOME} |
63 | 104 | {% else %} |
64 | | -COPY --chown=django:django . /app |
| 105 | +COPY --chown=django:django . ${APP_HOME} |
65 | 106 | {%- endif %} |
66 | 107 |
|
67 | | -USER django |
| 108 | +# make django owner of the WORKDIR directory as well. |
| 109 | +RUN chown django:django ${APP_HOME} |
68 | 110 |
|
69 | | -WORKDIR /app |
| 111 | +USER django |
70 | 112 |
|
71 | 113 | ENTRYPOINT ["/entrypoint"] |
0 commit comments