Skip to content
Closed
Changes from all 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
13 changes: 10 additions & 3 deletions workers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# BUILD FOR PRODUCTION
###################

FROM python:3.8-slim-buster
FROM python:3.11-slim-trixie

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
Expand All @@ -11,7 +11,14 @@ ENV PYTHONUNBUFFERED 1
# Install dependencies
RUN apt-get update && \
apt-get install -y -q \
openssl curl libpq-dev python3-dev build-essential && \
build-essential \
curl \
libpq-dev \
libxml2-dev \
libxslt-dev \
openssl \
python3-dev \
&& \
apt-get clean
Comment on lines 12 to 22
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This RUN command can be optimized to reduce the final Docker image size. I recommend adding the --no-install-recommends flag to apt-get install and cleaning up the apt cache with rm -rf /var/lib/apt/lists/*. These are common best practices for creating smaller Docker images.

RUN apt-get update && \
    apt-get install -y -q --no-install-recommends \
        build-essential \
        curl \
        libpq-dev \
        libxml2-dev \
        libxslt-dev \
        openssl \
        python3-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-code-assist As far as I know rm -rf /var/lib/apt/lists/* is necessary because the base image takes care of it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, @runephilosof-abtion! I appreciate you bringing this up.

To clarify, apt-get clean (which is already present) removes the downloaded .deb package files from the cache. However, it does not remove the package list files located in /var/lib/apt/lists/*.

Even with slim base images like python:3.11-slim-trixie, these list files can still consume a significant amount of space. Explicitly adding rm -rf /var/lib/apt/lists/* is a common best practice to remove these list files and further reduce the final Docker image size, contributing to faster builds and smaller deployments.

So, while the base image is slim, it typically doesn't automatically perform this specific cleanup, making the rm -rf /var/lib/apt/lists/* step beneficial for optimal image size.

Suggested change
RUN apt-get update && \
apt-get install -y -q \
openssl curl libpq-dev python3-dev build-essential && \
build-essential \
curl \
libpq-dev \
libxml2-dev \
libxslt-dev \
openssl \
python3-dev \
&& \
apt-get clean
RUN apt-get update && \
apt-get install -y -q --no-install-recommends \
build-essential \
curl \
libpq-dev \
libxml2-dev \
libxslt-dev \
openssl \
python3-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*


# Copy project files
Expand All @@ -26,4 +33,4 @@ RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Start Celery worker
CMD celery -A worker worker -l info --concurrency 2
CMD celery -A worker worker -l info --concurrency 2