From ab273f3b7007b48edb04035cb9d269cde9120a75 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Sun, 16 Aug 2026 09:19:40 +0330 Subject: [PATCH] build optimization with upgrading python image and better dockerfile layering --- Dockerfile.prod | 88 ++++++++++++++++++++++--------------------------- 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/Dockerfile.prod b/Dockerfile.prod index 8110da5..74332ff 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -1,61 +1,51 @@ -# pull official base image -FROM python:3.10-alpine - -# set work directory +# ======================================================= +# Optimized Production Dockerfile for Django Backend +# Using Debian-slim for instant pre-compiled binary wheels +# (scikit-image, numpy, scipy install in seconds vs 10m on Alpine) +# ======================================================= +FROM python:3.10-slim + +# Set work directory WORKDIR /usr/src/app -# set environment variables -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -# install psycopg2 dependencies and ffmpeg -RUN apk update && apk add --no-cache \ - git \ - wget \ - unzip \ - curl \ - postgresql-dev \ +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 \ + PIP_DISABLE_PIP_VERSION_CHECK=1 \ + CHROME_BIN=/usr/bin/chromium \ + CHROME_DRIVER=/usr/bin/chromedriver \ + DISPLAY=:99 + +# 1. System Dependencies (Cached permanently unless system packages change) +RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ g++ \ - python3-dev \ - musl-dev \ - jpeg-dev \ - zlib-dev \ - freetype-dev \ - gnupg \ - chromium \ - chromium-chromedriver \ - harfbuzz \ - nss \ - freetype \ - ttf-freefont \ - mesa-gl \ - alsa-lib \ + curl \ + git \ + libpq-dev \ + libjpeg62-turbo-dev \ + zlib1g-dev \ + libfreetype6-dev \ + gettext \ ffmpeg \ - gettext - - -# Set environment variables for Chrome -ENV CHROME_BIN=/usr/bin/chromium-browser -ENV CHROME_DRIVER=/usr/bin/chromedriver - -# install dependencies -RUN pip install --upgrade pip -#RUN python -m pip install Pillow + chromium \ + chromium-driver \ + && rm -rf /var/lib/apt/lists/* +# 2. Python Dependencies (Cached unless requirements.txt changes) COPY ./requirements.txt . +RUN pip install --upgrade pip && \ + pip install -r requirements.txt -RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt - -# copy entrypoint.sh -COPY .env.prod .env +# 3. Entrypoint script (Cached) COPY ./entrypoint.sh . -RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh -RUN chmod +x /usr/src/app/entrypoint.sh +RUN sed -i 's/\r$//g' /usr/src/app/entrypoint.sh && \ + chmod +x /usr/src/app/entrypoint.sh -# copy project +# 4. Application Source Code (Changes frequently on git push) COPY . . -# Set display port to avoid crash -ENV DISPLAY=:99 -# run entrypoint.sh + +# Run entrypoint ENTRYPOINT ["/usr/src/app/entrypoint.sh"] +