Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Dockerfile #317

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion {{cookiecutter.project_dirname}}/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ data
docker-compose*
Dockerfile*
htmlcov
k8s
Makefile
media
media_test
requirements/*.in
static
terraform
27 changes: 18 additions & 9 deletions {{cookiecutter.project_dirname}}/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,39 @@ FROM python:3.12-slim-bookworm AS base
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="backend" stage="base"
ARG DEBIAN_FRONTEND=noninteractive
ARG USER=appuser
ENV APPUSER=$USER LANG=C.UTF-8 LC_ALL=C.UTF-8 PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 WORKDIR=/app
ENV APPUSER=$USER \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
VIRTUAL_ENV=/opt/venv \
WORKDIR=/app
WORKDIR $WORKDIR
RUN useradd --skel /dev/null --create-home $APPUSER
RUN chown $APPUSER:$APPUSER $WORKDIR
ENV PATH="/home/${APPUSER}/.local/bin:${PATH}"
ARG PACKAGES_PATH=/home/${APPUSER}/.local/lib/python3.12/site-packages
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"
ARG PACKAGES_PATH=${VIRTUAL_ENV}/lib/python3.12/site-packages
RUN apt-get update \
&& apt-get install --assume-yes --no-install-recommends \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
COPY --chown=$APPUSER ./requirements/base.txt requirements/base.txt
RUN python3 -m venv $VIRTUAL_ENV \
&& chown -R $APPUSER:$APPUSER $VIRTUAL_ENV
RUN apt-get update \
&& apt-get install --assume-yes --no-install-recommends \
gcc \
libc6-dev \
libpq-dev \
&& su $APPUSER -c "python3 -m pip install --user --no-cache-dir -r requirements/base.txt" \
&& su $APPUSER -c "python3 -m pip install --no-cache-dir -r requirements/base.txt" \
&& find ${PACKAGES_PATH} -regex '^.*/locale/.*/*.\(mo\|po\)$' -not -path '*/en*' -not -path '*/it*' -delete || true \
&& apt-get purge --assume-yes --auto-remove \
gcc \
libc6-dev \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --chown=$APPUSER ./requirements/common.txt requirements/common.txt
RUN su $APPUSER -c "python3 -m pip install --user --no-cache-dir -r requirements/common.txt" \
RUN su $APPUSER -c "python3 -m pip install --no-cache-dir -r requirements/common.txt" \
&& find ${PACKAGES_PATH} -regex '^.*/locale/.*/*.\(mo\|po\)$' -not -path '*/en*' -not -path '*/it*' -delete || true

FROM base AS test
Expand All @@ -36,7 +44,7 @@ LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="backend
ENV DJANGO_CONFIGURATION=Testing
USER $APPUSER
COPY --chown=$APPUSER ./requirements/test.txt requirements/test.txt
RUN python3 -m pip install --user --no-cache-dir -r requirements/test.txt
RUN python3 -m pip install --no-cache-dir -r requirements/test.txt
COPY --chown=$APPUSER . .
CMD ./scripts/test.sh

Expand All @@ -45,9 +53,9 @@ FROM base AS remote
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="backend" stage="remote"
ENV DJANGO_CONFIGURATION=Remote INTERNAL_SERVICE_PORT={{ cookiecutter.internal_service_port }}
USER $APPUSER
ARG PACKAGES_PATH=/home/${APPUSER}/.local/lib/python3.12/site-packages
ARG PACKAGES_PATH=${VIRTUAL_ENV}/lib/python3.12/site-packages
COPY --chown=$APPUSER ./requirements/remote.txt requirements/remote.txt
RUN python3 -m pip install --user --no-cache-dir -r requirements/remote.txt \
RUN python3 -m pip install --no-cache-dir -r requirements/remote.txt \
&& find ${PACKAGES_PATH}/boto*/data/* -maxdepth 0 -type d -not -name s3* -exec rm -rf {} \; || true
COPY --chown=$APPUSER . .
RUN DJANGO_SECRET_KEY=build python3 -m manage collectstatic --clear --link --noinput
Expand All @@ -71,7 +79,8 @@ RUN apt-get update \
postgresql-client
USER $APPUSER
COPY --chown=$APPUSER ./requirements/local.txt requirements/local.txt
RUN python3 -m pip install --user --no-cache-dir -r requirements/local.txt
RUN python3 -m pip install --no-cache-dir -r requirements/local.txt \
&& find ${PACKAGES_PATH} -regex '^.*/locale/.*/*.\(mo\|po\)$' -not -path '*/en*' -not -path '*/it*' -delete || true
COPY --chown=$APPUSER . .
RUN DJANGO_SECRET_KEY=build python3 -m manage collectstatic --clear --link --noinput
ENTRYPOINT ["./scripts/entrypoint.sh"]
Expand Down