From 961daa91f3f3af4edb3b09da334f578a6047468c Mon Sep 17 00:00:00 2001 From: Job Date: Wed, 13 Oct 2021 01:45:48 +0200 Subject: [PATCH 1/3] Improved Docker image size Improved Docker image size by using python's alpine image and installing the required dependencies seperately. This reduces the size of the image from 1.46 GB to 0.982 GB --- Dockerfile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 007e637f..1b485811 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,19 +5,20 @@ COPY ./static/package*.json /code/static/ RUN cd /code/static && npm install # Main image -FROM python:3.7 +FROM python:3.7-alpine -# install poetry, "pip3 install poetry==1.1.5" doesn't work -# poetry will be available at /root/.poetry/bin/poetry -RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - +# install poetry +RUN apk update \ + && apk add --no-cache build-base openssl-dev libffi-dev \ + && pip3 install poetry WORKDIR /code # install dependencies COPY poetry.lock pyproject.toml ./ -RUN /root/.poetry/bin/poetry config virtualenvs.create false \ - && /root/.poetry/bin/poetry install --no-root +RUN poetry config virtualenvs.create false \ + && poetry install --no-root # copy npm packages COPY --from=npm /code /code From be82600fe6d57d83653636a2d0bad443e37b9b25 Mon Sep 17 00:00:00 2001 From: Job Date: Fri, 15 Oct 2021 10:59:33 +0000 Subject: [PATCH 2/3] Fixed docker builds --- Dockerfile | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1b485811..444b6db8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,12 +5,24 @@ COPY ./static/package*.json /code/static/ RUN cd /code/static && npm install # Main image -FROM python:3.7-alpine +FROM python:3.7-slim + +# Keeps Python from generating .pyc files in the container +ENV PYTHONDONTWRITEBYTECODE 1 +# Turns off buffering for easier container logging +ENV PYTHONUNBUFFERED 1 + +# Install and setup poetry +RUN pip install -U pip \ + && apt-get update \ + && apt install -y curl netcat gcc python3-dev \ + && curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - + # Clear apt cache + # && apt-get clean \ + # && rm -rf /var/lib/apt/lists/* + +ENV PATH="${PATH}:/root/.poetry/bin" -# install poetry -RUN apk update \ - && apk add --no-cache build-base openssl-dev libffi-dev \ - && pip3 install poetry WORKDIR /code @@ -18,7 +30,7 @@ WORKDIR /code COPY poetry.lock pyproject.toml ./ RUN poetry config virtualenvs.create false \ - && poetry install --no-root + && poetry install --no-interaction --no-ansi --no-root # copy npm packages COPY --from=npm /code /code From 4e5ca3b30baa67c85843997cf978ade117ebbcdf Mon Sep 17 00:00:00 2001 From: Job Date: Fri, 15 Oct 2021 11:20:07 +0000 Subject: [PATCH 3/3] Added apt folder cleaning --- Dockerfile | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index 444b6db8..e7cfd0b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,25 +12,27 @@ ENV PYTHONDONTWRITEBYTECODE 1 # Turns off buffering for easier container logging ENV PYTHONUNBUFFERED 1 +# Add poetry to PATH +ENV PATH="${PATH}:/root/.poetry/bin" + +WORKDIR /code + +# Copy poetry files +COPY poetry.lock pyproject.toml ./ + # Install and setup poetry RUN pip install -U pip \ && apt-get update \ && apt install -y curl netcat gcc python3-dev \ - && curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - + && curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - \ + # Remove curl and netcat from the image + && apt-get purge -y curl netcat \ + # Run poetry + && poetry config virtualenvs.create false \ + && poetry install --no-interaction --no-ansi --no-root \ # Clear apt cache - # && apt-get clean \ - # && rm -rf /var/lib/apt/lists/* - -ENV PATH="${PATH}:/root/.poetry/bin" - - -WORKDIR /code - -# install dependencies -COPY poetry.lock pyproject.toml ./ - -RUN poetry config virtualenvs.create false \ - && poetry install --no-interaction --no-ansi --no-root + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* # copy npm packages COPY --from=npm /code /code