Skip to content

Commit 69f43da

Browse files
authored
Merge pull request #2815 from arnav13081994/django-multi
2 parents 7cd62c1 + e6604f9 commit 69f43da

2 files changed

Lines changed: 110 additions & 30 deletions

File tree

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
1-
FROM python:3.8-slim-buster
1+
ARG PYTHON_VERSION=3.8-slim-buster
2+
3+
# define an alias for the specfic python version used in this file.
4+
FROM python:${PYTHON_VERSION} as python
5+
6+
# Python build stage
7+
FROM python as python-build-stage
8+
9+
ARG BUILD_ENVIRONMENT=local
10+
11+
# Install apt packages
12+
RUN apt-get update && apt-get install --no-install-recommends -y \
13+
# dependencies for building Python packages
14+
build-essential \
15+
# psycopg2 dependencies
16+
libpq-dev
17+
18+
# Requirements are installed here to ensure they will be cached.
19+
COPY ./requirements .
20+
21+
# Create Python Dependency and Sub-Dependency Wheels.
22+
RUN pip wheel --wheel-dir /usr/src/app/wheels \
23+
-r ${BUILD_ENVIRONMENT}.txt
24+
25+
26+
# Python 'run' stage
27+
FROM python as python-run-stage
28+
29+
ARG BUILD_ENVIRONMENT=local
30+
ARG APP_HOME=/app
231

332
ENV PYTHONUNBUFFERED 1
433
ENV PYTHONDONTWRITEBYTECODE 1
34+
ENV BUILD_ENV ${BUILD_ENVIRONMENT}
535

6-
RUN apt-get update \
7-
# dependencies for building Python packages
8-
&& apt-get install -y build-essential \
36+
WORKDIR ${APP_HOME}
37+
38+
# Install required system dependencies
39+
RUN apt-get update && apt-get install --no-install-recommends -y \
940
# psycopg2 dependencies
10-
&& apt-get install -y libpq-dev \
41+
libpq-dev \
1142
# Translations dependencies
12-
&& apt-get install -y gettext \
43+
gettext \
1344
# cleaning up unused files
1445
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
1546
&& rm -rf /var/lib/apt/lists/*
1647

17-
# Requirements are installed here to ensure they will be cached.
18-
COPY ./requirements /requirements
19-
RUN pip install -r /requirements/local.txt
48+
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
49+
# copy python dependency wheels from python-build-stage
50+
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
51+
52+
# use wheels to install python dependencies
53+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
54+
&& rm -rf /wheels/
2055

2156
COPY ./compose/production/django/entrypoint /entrypoint
2257
RUN sed -i 's/\r$//g' /entrypoint
@@ -25,6 +60,7 @@ RUN chmod +x /entrypoint
2560
COPY ./compose/local/django/start /start
2661
RUN sed -i 's/\r$//g' /start
2762
RUN chmod +x /start
63+
2864
{% if cookiecutter.use_celery == "y" %}
2965
COPY ./compose/local/django/celery/worker/start /start-celeryworker
3066
RUN sed -i 's/\r$//g' /start-celeryworker
@@ -38,6 +74,8 @@ COPY ./compose/local/django/celery/flower/start /start-flower
3874
RUN sed -i 's/\r$//g' /start-flower
3975
RUN chmod +x /start-flower
4076
{% endif %}
41-
WORKDIR /app
77+
78+
# copy application code to WORKDIR
79+
COPY . ${APP_HOME}
4280

4381
ENTRYPOINT ["/entrypoint"]
Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,75 @@
11
{% if cookiecutter.js_task_runner == 'Gulp' -%}
22
FROM node:10-stretch-slim as client-builder
33

4-
WORKDIR /app
5-
COPY ./package.json /app
4+
ARG APP_HOME=/app
5+
WORKDIR ${APP_HOME}
6+
7+
COPY ./package.json ${APP_HOME}
68
RUN npm install && npm cache clean --force
7-
COPY . /app
9+
COPY . ${APP_HOME}
810
RUN npm run build
911

10-
# Python build stage
1112
{%- endif %}
12-
FROM python:3.8-slim-buster
1313

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
1518

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 \
1726
# dependencies for building Python packages
18-
&& apt-get install -y build-essential \
27+
build-essential \
1928
# 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 \
2159
# Translations dependencies
22-
&& apt-get install -y gettext \
60+
gettext \
2361
# cleaning up unused files
2462
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
2563
&& rm -rf /var/lib/apt/lists/*
2664

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/
2972

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
3473

3574
COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
3675
RUN sed -i 's/\r$//g' /entrypoint
@@ -58,14 +97,17 @@ RUN sed -i 's/\r$//g' /start-flower
5897
RUN chmod +x /start-flower
5998
{%- endif %}
6099

100+
101+
# copy application code to WORKDIR
61102
{%- 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}
63104
{% else %}
64-
COPY --chown=django:django . /app
105+
COPY --chown=django:django . ${APP_HOME}
65106
{%- endif %}
66107

67-
USER django
108+
# make django owner of the WORKDIR directory as well.
109+
RUN chown django:django ${APP_HOME}
68110

69-
WORKDIR /app
111+
USER django
70112

71113
ENTRYPOINT ["/entrypoint"]

0 commit comments

Comments
 (0)