From 72bd781104d3b89164b4c83e6429d5ff790ef3b9 Mon Sep 17 00:00:00 2001 From: Pratik Raj Date: Tue, 11 Oct 2022 20:29:07 +0530 Subject: [PATCH] feat : use `--no-cache-dir` flag to `pip` in dockerfiles to save space using the "--no-cache-dir" flag in pip install, make sure downloaded packages by pip don't cache on the system. This is a best practice that makes sure to fetch from a repo instead of using a local cached one. Further, in the case of Docker Containers, by restricting caching, we can reduce image size. In terms of stats, it depends upon the number of python packages multiplied by their respective size. e.g for heavy packages with a lot of dependencies it reduces a lot by don't cache pip packages. Further, more detailed information can be found at https://medium.com/sciforce/strategies-of-docker-images-optimization-2ca9cc5719b6 Signed-off-by: Pratik Raj --- Dockerfile | 8 +++----- documentation/changelog.rst | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index c9dcc36f7..a53e0fe80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,16 +5,14 @@ ENV LC_ALL C.UTF-8 ENV LANG C.UTF-8 # pre-requisites -RUN apt-get update && apt-get install -y --upgrade python3 python3-pip git curl gunicorn coinor-cbc +RUN apt-get update && apt-get install --no-install-recommends -y --upgrade python3 python3-pip git curl gunicorn coinor-cbc && apt-get clean WORKDIR /app # requirements - doing this earlier, so we don't install them each time. Use --no-cache to refresh them. COPY requirements /app/requirements # py dev tooling -RUN python3 -m pip install --upgrade pip && python3 --version -RUN pip3 install --upgrade setuptools -RUN pip3 install -r requirements/app.txt -r requirements/dev.txt -r requirements/test.txt +RUN python3 -m pip install --no-cache-dir --upgrade pip && python3 --version && pip3 install --no-cache-dir --upgrade setuptools && pip3 install --no-cache-dir -r requirements/app.txt -r requirements/dev.txt -r requirements/test.txt # Copy code and meta/config data COPY setup.* .flaskenv wsgi.py /app/ @@ -22,7 +20,7 @@ COPY flexmeasures/ /app/flexmeasures RUN find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf COPY .git/ /app/.git -RUN pip3 install . +RUN pip3 install --no-cache-dir . EXPOSE 5000 diff --git a/documentation/changelog.rst b/documentation/changelog.rst index 9248c2e26..757f80e04 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -18,6 +18,7 @@ Bugfixes Infrastructure / Support ---------------------- +* Reduce size of Docker image (from 2GB to 1.4GB) [see `PR #512 `_] * Remove bokeh dependency and obsolete UI views [see `PR #476 `_]