Skip to content

Commit bae051e

Browse files
committed
Improve Dockerfile
Use VIRTUAL_ENV into Dockerfile instead of user system path Improve local image size optimization
1 parent 7982a0b commit bae051e

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

{{cookiecutter.project_dirname}}/Dockerfile

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,39 @@ FROM python:3.12-slim-bookworm AS base
33
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="backend" stage="base"
44
ARG DEBIAN_FRONTEND=noninteractive
55
ARG USER=appuser
6-
ENV APPUSER=$USER LANG=C.UTF-8 LC_ALL=C.UTF-8 PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1 WORKDIR=/app
6+
ENV APPUSER=$USER \
7+
LANG=C.UTF-8 \
8+
LC_ALL=C.UTF-8 \
9+
PYTHONUNBUFFERED=1 \
10+
PYTHONDONTWRITEBYTECODE=1 \
11+
VIRTUAL_ENV=/opt/venv \
12+
WORKDIR=/app
713
WORKDIR $WORKDIR
814
RUN useradd --skel /dev/null --create-home $APPUSER
915
RUN chown $APPUSER:$APPUSER $WORKDIR
10-
ENV PATH="/home/${APPUSER}/.local/bin:${PATH}"
11-
ARG PACKAGES_PATH=/home/${APPUSER}/.local/lib/python3.12/site-packages
16+
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"
17+
ARG PACKAGES_PATH=${VIRTUAL_ENV}/lib/python3.12/site-packages
1218
RUN apt-get update \
1319
&& apt-get install --assume-yes --no-install-recommends \
1420
libpq5 \
1521
&& rm -rf /var/lib/apt/lists/*
1622
COPY --chown=$APPUSER ./requirements/base.txt requirements/base.txt
23+
RUN python3 -m venv $VIRTUAL_ENV \
24+
&& chown -R $APPUSER:$APPUSER $VIRTUAL_ENV
1725
RUN apt-get update \
1826
&& apt-get install --assume-yes --no-install-recommends \
1927
gcc \
2028
libc6-dev \
2129
libpq-dev \
22-
&& su $APPUSER -c "python3 -m pip install --user --no-cache-dir -r requirements/base.txt" \
30+
&& su $APPUSER -c "python3 -m pip install --no-cache-dir -r requirements/base.txt" \
2331
&& find ${PACKAGES_PATH} -regex '^.*/locale/.*/*.\(mo\|po\)$' -not -path '*/en*' -not -path '*/it*' -delete || true \
2432
&& apt-get purge --assume-yes --auto-remove \
2533
gcc \
2634
libc6-dev \
2735
libpq-dev \
2836
&& rm -rf /var/lib/apt/lists/*
2937
COPY --chown=$APPUSER ./requirements/common.txt requirements/common.txt
30-
RUN su $APPUSER -c "python3 -m pip install --user --no-cache-dir -r requirements/common.txt" \
38+
RUN su $APPUSER -c "python3 -m pip install --no-cache-dir -r requirements/common.txt" \
3139
&& find ${PACKAGES_PATH} -regex '^.*/locale/.*/*.\(mo\|po\)$' -not -path '*/en*' -not -path '*/it*' -delete || true
3240

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

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

0 commit comments

Comments
 (0)