From 53ffbbcf2ea9ec330e1ea68a9ccad7fd47b94059 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 11 Sep 2020 22:00:50 +0530 Subject: [PATCH 01/12] Multi stage Python build for Django --- .../compose/local/django/Dockerfile | 61 ++++++++++++++----- .../compose/production/django/Dockerfile | 58 ++++++++++++------ 2 files changed, 87 insertions(+), 32 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 5473f114..949235c0 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -1,23 +1,32 @@ +# Python build stage +FROM python:3.8-slim-buster as python-build-stage +ENV PYTHONDONTWRITEBYTECODE 1 + +# Requirements are installed here to ensure they will be cached. +COPY ./requirements /requirements + +RUN apt-get update && apt-get install --no-install-recommends -y \ + # dependencies for building Python packages + build-essential \ + # psycopg2 dependencies + libpq-dev \ + # Translations dependencies + gettext \ + # cleaning up unused files + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ + && rm -rf /var/lib/apt/lists/* \ + # create python dependency wheels + && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/production.txt \ + && rm -rf /requirements + + + +# Python 'run' stage FROM python:3.8-slim-buster ENV PYTHONUNBUFFERED 1 ENV PYTHONDONTWRITEBYTECODE 1 -RUN apt-get update \ - # dependencies for building Python packages - && apt-get install -y build-essential \ - # psycopg2 dependencies - && apt-get install -y libpq-dev \ - # Translations dependencies - && apt-get install -y gettext \ - # cleaning up unused files - && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* - -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements -RUN pip install -r /requirements/local.txt - COPY ./compose/production/django/entrypoint /entrypoint RUN sed -i 's/\r$//g' /entrypoint RUN chmod +x /entrypoint @@ -25,6 +34,7 @@ RUN chmod +x /entrypoint COPY ./compose/local/django/start /start RUN sed -i 's/\r$//g' /start RUN chmod +x /start + {% if cookiecutter.use_celery == "y" %} COPY ./compose/local/django/celery/worker/start /start-celeryworker RUN sed -i 's/\r$//g' /start-celeryworker @@ -38,6 +48,27 @@ COPY ./compose/local/django/celery/flower/start /start-flower RUN sed -i 's/\r$//g' /start-flower RUN chmod +x /start-flower {% endif %} + + +# installing required system dependencies +RUN apt-get update && apt-get install --no-install-recommends -y \ + # psycopg2 dependencies + libpq-dev \ + # Translations dependencies + gettext \ + # cleaning up unused files + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ + && rm -rf /var/lib/apt/lists/* + + +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels + +# install python dependencies +RUN pip install --no-cache /wheels/* + + + WORKDIR /app ENTRYPOINT ["/entrypoint"] diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 2e6a195b..9fd75f7e 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -7,31 +7,38 @@ RUN npm install && npm cache clean --force COPY . /app RUN npm run build -# Python build stage {%- endif %} + +# Python build stage +FROM python:3.8-slim-buster as python-build-stage + + +# Requirements are installed here to ensure they will be cached. +COPY ./requirements /requirements + +RUN apt-get update && apt-get install --no-install-recommends -y \ + # dependencies for building Python packages + build-essential \ + # psycopg2 dependencies + libpq-dev \ + # Translations dependencies + gettext \ + # cleaning up unused files + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ + && rm -rf /var/lib/apt/lists/* \ + # create python dependency wheels + && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/production.txt \ + && rm -rf /requirements + + +# Python 'run' stage FROM python:3.8-slim-buster ENV PYTHONUNBUFFERED 1 -RUN apt-get update \ - # dependencies for building Python packages - && apt-get install -y build-essential \ - # psycopg2 dependencies - && apt-get install -y libpq-dev \ - # Translations dependencies - && apt-get install -y gettext \ - # cleaning up unused files - && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* - RUN addgroup --system django \ && adduser --system --ingroup django django -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements -RUN pip install --no-cache-dir -r /requirements/production.txt \ - && rm -rf /requirements - COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint RUN sed -i 's/\r$//g' /entrypoint RUN chmod +x /entrypoint @@ -58,6 +65,23 @@ RUN sed -i 's/\r$//g' /start-flower RUN chmod +x /start-flower {%- endif %} +# installing required system dependencies +RUN apt-get update && apt-get install --no-install-recommends -y \ + # psycopg2 dependencies + libpq-dev \ + # Translations dependencies + gettext \ + # cleaning up unused files + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ + && rm -rf /var/lib/apt/lists/* + + +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels + +# install python dependencies +RUN pip install --no-cache /wheels/* + {%- if cookiecutter.js_task_runner == 'Gulp' %} COPY --from=client-builder --chown=django:django /app /app {% else %} From 070d4556f9147979aa81a0300b36fb8c561bea81 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 11 Sep 2020 22:04:44 +0530 Subject: [PATCH 02/12] Multi stage Python build for Django --- .../compose/production/traefik/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile index aa879052..26a653d5 100644 --- a/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile @@ -1,4 +1,4 @@ -FROM traefik:v2.2.11 +FROM traefik:v2.0 RUN mkdir -p /etc/traefik/acme \ && touch /etc/traefik/acme/acme.json \ && chmod 600 /etc/traefik/acme/acme.json From 07051bff33e489834f3d0b88954ddf3709cd74c7 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Sat, 12 Sep 2020 08:47:38 +0530 Subject: [PATCH 03/12] Fixed mistake of using production settings even for local/django image --- {{cookiecutter.project_slug}}/compose/local/django/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 949235c0..5c3e3f1a 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -16,7 +16,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* \ # create python dependency wheels - && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/production.txt \ + && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/local.txt \ && rm -rf /requirements From a795086b108c23d8e886762c3b9f06033e610352 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Sat, 12 Sep 2020 11:04:08 +0530 Subject: [PATCH 04/12] Removing using python wheels to build python dependecies due to issue resolving conflicts in package sub-dependencies. Ends up being much more trouble than it's worth. --- .../compose/local/django/Dockerfile | 11 +++++------ .../compose/production/django/Dockerfile | 15 +++++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 5c3e3f1a..a7cf54ae 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -15,8 +15,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* \ - # create python dependency wheels - && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/local.txt \ + # install python dependencies + && pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/local.txt \ && rm -rf /requirements @@ -61,11 +61,10 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && rm -rf /var/lib/apt/lists/* -# copy python dependency wheels from python-build-stage -COPY --from=python-build-stage /usr/src/app/wheels /wheels -# install python dependencies -RUN pip install --no-cache /wheels/* +# copy python dependencies from python-build-stage +COPY --from=build_stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ +COPY --from=build_stage /usr/local/bin/ /usr/local/bin/ diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 9fd75f7e..15708dcd 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -11,7 +11,7 @@ RUN npm run build # Python build stage FROM python:3.8-slim-buster as python-build-stage - +ENV PYTHONDONTWRITEBYTECODE 1 # Requirements are installed here to ensure they will be cached. COPY ./requirements /requirements @@ -26,14 +26,15 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* \ - # create python dependency wheels - && pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels -r /requirements/production.txt \ + # install python dependencies + && pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/production.txt \ && rm -rf /requirements # Python 'run' stage FROM python:3.8-slim-buster +ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 RUN addgroup --system django \ @@ -76,11 +77,9 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && rm -rf /var/lib/apt/lists/* -# copy python dependency wheels from python-build-stage -COPY --from=python-build-stage /usr/src/app/wheels /wheels - -# install python dependencies -RUN pip install --no-cache /wheels/* +# copy python dependencies from python-build-stage +COPY --from=build_stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ +COPY --from=build_stage /usr/local/bin/ /usr/local/bin/ {%- if cookiecutter.js_task_runner == 'Gulp' %} COPY --from=client-builder --chown=django:django /app /app From 17e577c92478664627be6ded8501545a5d5b2115 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Sat, 12 Sep 2020 11:11:19 +0530 Subject: [PATCH 05/12] Fixed Typo with python build stage name that was references incorrectly in the run stage --- {{cookiecutter.project_slug}}/compose/local/django/Dockerfile | 4 ++-- .../compose/production/django/Dockerfile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index a7cf54ae..11cc9978 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -63,8 +63,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ # copy python dependencies from python-build-stage -COPY --from=build_stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ -COPY --from=build_stage /usr/local/bin/ /usr/local/bin/ +COPY --from=python-build-stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ +COPY --from=python-build-stage /usr/local/bin/ /usr/local/bin/ diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 15708dcd..722f709f 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -78,8 +78,8 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ # copy python dependencies from python-build-stage -COPY --from=build_stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ -COPY --from=build_stage /usr/local/bin/ /usr/local/bin/ +COPY --from=python-build-stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ +COPY --from=python-build-stage /usr/local/bin/ /usr/local/bin/ {%- if cookiecutter.js_task_runner == 'Gulp' %} COPY --from=client-builder --chown=django:django /app /app From 079db28661a5a30066dc67da57b728741a04b986 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Mon, 14 Sep 2020 01:41:33 +0530 Subject: [PATCH 06/12] Changed when Requirements are copied and installed to improve incremental image build times. The extra image layer is inconsequential since python-build-stage layers are discarded anyway. --- .../compose/local/django/Dockerfile | 17 +++++++++-------- .../compose/production/django/Dockerfile | 16 ++++++++-------- .../compose/production/traefik/Dockerfile | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 11cc9978..645b0476 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -2,9 +2,6 @@ FROM python:3.8-slim-buster as python-build-stage ENV PYTHONDONTWRITEBYTECODE 1 -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements - RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ @@ -14,15 +11,19 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* \ - # install python dependencies - && pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/local.txt \ - && rm -rf /requirements + && rm -rf /var/lib/apt/lists/* + +# Requirements are installed here to ensure they will be cached. +COPY ./requirements /requirements + +# install python dependencies +RUN pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/local.txt \ + && rm -rf /requirements # Python 'run' stage -FROM python:3.8-slim-buster +FROM python:3.8-slim-buster as python-run-stage ENV PYTHONUNBUFFERED 1 ENV PYTHONDONTWRITEBYTECODE 1 diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 722f709f..cc46464b 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -13,9 +13,6 @@ RUN npm run build FROM python:3.8-slim-buster as python-build-stage ENV PYTHONDONTWRITEBYTECODE 1 -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements - RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ @@ -25,14 +22,17 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* \ - # install python dependencies - && pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/production.txt \ - && rm -rf /requirements + && rm -rf /var/lib/apt/lists/* +# Requirements are installed here to ensure they will be cached. +COPY ./requirements /requirements + +# install python dependencies +Run pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/production.txt \ + && rm -rf /requirements # Python 'run' stage -FROM python:3.8-slim-buster +FROM python:3.8-slim-buster as python-run-stage ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 diff --git a/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile index 26a653d5..aa879052 100644 --- a/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/traefik/Dockerfile @@ -1,4 +1,4 @@ -FROM traefik:v2.0 +FROM traefik:v2.2.11 RUN mkdir -p /etc/traefik/acme \ && touch /etc/traefik/acme/acme.json \ && chmod 600 /etc/traefik/acme/acme.json From 4a52845a269584ade6c3198c84705a95f1e6ef78 Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Wed, 16 Sep 2020 15:24:16 +0530 Subject: [PATCH 07/12] Replaced copying python package directories with wheel files which are then used to install all requirements. This way the generated images would be a lot more stable as well. Size increases by 2-3mb though --- .../compose/local/django/Dockerfile | 20 ++++++++--------- .../compose/production/django/Dockerfile | 22 ++++++++++++------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 645b0476..c34f8ad4 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -7,19 +7,18 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ build-essential \ # psycopg2 dependencies libpq-dev \ - # Translations dependencies - gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* + # Requirements are installed here to ensure they will be cached. COPY ./requirements /requirements -# install python dependencies -RUN pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/local.txt \ - && rm -rf /requirements - +# create python dependency wheels +RUN pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels \ + -r /requirements/local.txt \ + && rm -rf /requirements # Python 'run' stage @@ -62,11 +61,12 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && rm -rf /var/lib/apt/lists/* +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels -# copy python dependencies from python-build-stage -COPY --from=python-build-stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ -COPY --from=python-build-stage /usr/local/bin/ /usr/local/bin/ - +# use wheels to install python dependencies +RUN pip install --no-cache /wheels/* \ + && rm -rf /wheels WORKDIR /app diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index cc46464b..a2f432cc 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -11,25 +11,28 @@ RUN npm run build # Python build stage FROM python:3.8-slim-buster as python-build-stage + ENV PYTHONDONTWRITEBYTECODE 1 +ARG BUILD_ENVIRONMENT + RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ # psycopg2 dependencies libpq-dev \ - # Translations dependencies - gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* + # Requirements are installed here to ensure they will be cached. COPY ./requirements /requirements -# install python dependencies -Run pip install --no-cache-dir --use-feature=2020-resolver -r /requirements/production.txt \ - && rm -rf /requirements +# create python dependency wheels +RUN pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels \ + -r /requirements/production.txt \ + && rm -rf /requirements # Python 'run' stage FROM python:3.8-slim-buster as python-run-stage @@ -77,9 +80,12 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ && rm -rf /var/lib/apt/lists/* -# copy python dependencies from python-build-stage -COPY --from=python-build-stage /usr/local/lib/python3.8/site-packages/ /usr/local/lib/python3.8/site-packages/ -COPY --from=python-build-stage /usr/local/bin/ /usr/local/bin/ +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels + +# use wheels to install python dependencies +RUN pip install --no-cache /wheels/* \ + && rm -rf /wheels {%- if cookiecutter.js_task_runner == 'Gulp' %} COPY --from=client-builder --chown=django:django /app /app From ff98d8f517526f9211fcafb4eaf86d8f23ac7e1d Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 23 Feb 2021 20:34:41 +0000 Subject: [PATCH 08/12] Remove --use-feature=2020-resolver in pip commands They are now the default and cause a warning: WARNING: --use-feature=2020-resolver no longer has any effect, since it is now the default dependency resolver in pip. This will become an error in pip 21.0. --- {{cookiecutter.project_slug}}/compose/local/django/Dockerfile | 2 +- .../compose/production/django/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index c34f8ad4..25d70bf5 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -16,7 +16,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ COPY ./requirements /requirements # create python dependency wheels -RUN pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels \ +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels \ -r /requirements/local.txt \ && rm -rf /requirements diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index a2f432cc..f4e2b458 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -30,7 +30,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \ COPY ./requirements /requirements # create python dependency wheels -RUN pip wheel --no-cache-dir --no-deps --use-feature=2020-resolver --wheel-dir /usr/src/app/wheels \ +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels \ -r /requirements/production.txt \ && rm -rf /requirements From 483b636fcb74353baed3009be25ce7724bc5e644 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Tue, 23 Feb 2021 20:36:52 +0000 Subject: [PATCH 09/12] Only indent with spaces in django Dockerfile --- {{cookiecutter.project_slug}}/compose/local/django/Dockerfile | 2 +- .../compose/production/django/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 25d70bf5..0fc7b9c1 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -66,7 +66,7 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels # use wheels to install python dependencies RUN pip install --no-cache /wheels/* \ - && rm -rf /wheels + && rm -rf /wheels WORKDIR /app diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index f4e2b458..69b80aa0 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -85,7 +85,7 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels # use wheels to install python dependencies RUN pip install --no-cache /wheels/* \ - && rm -rf /wheels + && rm -rf /wheels {%- if cookiecutter.js_task_runner == 'Gulp' %} COPY --from=client-builder --chown=django:django /app /app From 15453df8245ffd31b9aceaecb9970342b26f814a Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Wed, 24 Feb 2021 10:58:28 +0530 Subject: [PATCH 10/12] Updated the local and production dockerfiles with more repeatability and easier to update in mind. --- .../compose/local/django/Dockerfile | 85 ++++++++------- .../compose/production/django/Dockerfile | 101 ++++++++++-------- 2 files changed, 103 insertions(+), 83 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index 0fc7b9c1..bdcd87ba 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -1,31 +1,57 @@ -# Python build stage -FROM python:3.8-slim-buster as python-build-stage -ENV PYTHONDONTWRITEBYTECODE 1 +ARG PYTHON_VERSION=3.8-slim-buster +# define an alias for the specfic python version used in this file. +FROM python:${PYTHON_VERSION} as python + +# Python build stage +FROM python as python-build-stage + +ARG BUILD_ENVIRONMENT=local + +# Install apt packages RUN apt-get update && apt-get install --no-install-recommends -y \ # dependencies for building Python packages build-essential \ + # psycopg2 dependencies + libpq-dev + +# Requirements are installed here to ensure they will be cached. +COPY ./requirements . + +# Create Python Dependency and Sub-Dependency Wheels. +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=local +ARG APP_HOME=/app + +ENV PYTHONUNBUFFERED 1 +ENV PYTHONDONTWRITEBYTECODE 1 +ENV BUILD_ENV ${BUILD_ENVIRONMENT} + +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 \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* +# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels/ -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements - -# create python dependency wheels -RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels \ - -r /requirements/local.txt \ - && rm -rf /requirements - - -# Python 'run' stage -FROM python:3.8-slim-buster as python-run-stage - -ENV PYTHONUNBUFFERED 1 -ENV PYTHONDONTWRITEBYTECODE 1 +# use wheels to install python dependencies +RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ + && rm -rf /wheels/ COPY ./compose/production/django/entrypoint /entrypoint RUN sed -i 's/\r$//g' /entrypoint @@ -49,26 +75,7 @@ RUN sed -i 's/\r$//g' /start-flower RUN chmod +x /start-flower {% endif %} +# copy application code to WORKDIR +COPY . ${APP_HOME} -# installing required system dependencies -RUN apt-get update && apt-get install --no-install-recommends -y \ - # psycopg2 dependencies - libpq-dev \ - # Translations dependencies - gettext \ - # cleaning up unused files - && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* - - -# copy python dependency wheels from python-build-stage -COPY --from=python-build-stage /usr/src/app/wheels /wheels - -# use wheels to install python dependencies -RUN pip install --no-cache /wheels/* \ - && rm -rf /wheels - - -WORKDIR /app - -ENTRYPOINT ["/entrypoint"] +ENTRYPOINT ["/entrypoint"] \ No newline at end of file diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 69b80aa0..8fa6e202 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -1,47 +1,75 @@ {% if cookiecutter.js_task_runner == 'Gulp' -%} FROM node:10-stretch-slim as client-builder -WORKDIR /app -COPY ./package.json /app +ARG APP_HOME=/app +WORKDIR ${APP_HOME} + +COPY ./package.json ${APP_HOME} RUN npm install && npm cache clean --force -COPY . /app +COPY . ${APP_HOME} RUN npm run build {%- endif %} +ARG PYTHON_VERSION=3.8-slim-buster + +# define an alias for the specfic python version used in this file. +FROM python:${PYTHON_VERSION} as python + # Python build stage -FROM python:3.8-slim-buster as python-build-stage - -ENV PYTHONDONTWRITEBYTECODE 1 -ARG BUILD_ENVIRONMENT +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 \ + # psycopg2 dependencies + libpq-dev + +# Requirements are installed here to ensure they will be cached. +COPY ./requirements . + +# Create Python Dependency and Sub-Dependency Wheels. +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} + +WORKDIR ${APP_HOME} + +RUN addgroup --system django \ + && adduser --system --ingroup django django + + +# Install required system dependencies +RUN apt-get update && apt-get install --no-install-recommends -y \ # psycopg2 dependencies libpq-dev \ + # Translations dependencies + gettext \ # cleaning up unused files && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ && rm -rf /var/lib/apt/lists/* +# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction +# copy python dependency wheels from python-build-stage +COPY --from=python-build-stage /usr/src/app/wheels /wheels/ -# Requirements are installed here to ensure they will be cached. -COPY ./requirements /requirements +# use wheels to install python dependencies +RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ + && rm -rf /wheels/ -# create python dependency wheels -RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels \ - -r /requirements/production.txt \ - && rm -rf /requirements - -# Python 'run' stage -FROM python:3.8-slim-buster as python-run-stage - -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -RUN addgroup --system django \ - && adduser --system --ingroup django django COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint RUN sed -i 's/\r$//g' /entrypoint @@ -69,32 +97,17 @@ RUN sed -i 's/\r$//g' /start-flower RUN chmod +x /start-flower {%- endif %} -# installing required system dependencies -RUN apt-get update && apt-get install --no-install-recommends -y \ - # psycopg2 dependencies - libpq-dev \ - # Translations dependencies - gettext \ - # cleaning up unused files - && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ - && rm -rf /var/lib/apt/lists/* - - -# copy python dependency wheels from python-build-stage -COPY --from=python-build-stage /usr/src/app/wheels /wheels - -# use wheels to install python dependencies -RUN pip install --no-cache /wheels/* \ - && rm -rf /wheels +# copy application code to WORKDIR {%- if cookiecutter.js_task_runner == 'Gulp' %} -COPY --from=client-builder --chown=django:django /app /app +COPY --from=client-builder --chown=django:django ${APP_HOME} ${APP_HOME} {% else %} -COPY --chown=django:django . /app +COPY --chown=django:django . ${APP_HOME} {%- endif %} +# make django owner of the WORKDIR directory as well. +RUN chown django:django ${APP_HOME} + USER django -WORKDIR /app - -ENTRYPOINT ["/entrypoint"] +ENTRYPOINT ["/entrypoint"] \ No newline at end of file From 2345c2c92d21b8274f0581864f0b656b9b147cca Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Wed, 24 Feb 2021 16:26:58 +0530 Subject: [PATCH 11/12] Update {{cookiecutter.project_slug}}/compose/production/django/Dockerfile Co-authored-by: Bruno Alla --- .../compose/production/django/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile index 8fa6e202..4da817df 100644 --- a/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/production/django/Dockerfile @@ -68,7 +68,7 @@ COPY --from=python-build-stage /usr/src/app/wheels /wheels/ # use wheels to install python dependencies RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \ - && rm -rf /wheels/ + && rm -rf /wheels/ COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint @@ -110,4 +110,4 @@ RUN chown django:django ${APP_HOME} USER django -ENTRYPOINT ["/entrypoint"] \ No newline at end of file +ENTRYPOINT ["/entrypoint"] From e6604f9a7d0211216c18f792a91a2366c3fac411 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Wed, 7 Apr 2021 21:12:36 +0100 Subject: [PATCH 12/12] Fix style --- {{cookiecutter.project_slug}}/compose/local/django/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile index bdcd87ba..f174bcef 100644 --- a/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile +++ b/{{cookiecutter.project_slug}}/compose/local/django/Dockerfile @@ -78,4 +78,4 @@ RUN chmod +x /start-flower # copy application code to WORKDIR COPY . ${APP_HOME} -ENTRYPOINT ["/entrypoint"] \ No newline at end of file +ENTRYPOINT ["/entrypoint"]