From 02c073d91e7ebd66d992b22ebab3ad827bc99967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Wed, 24 Apr 2024 21:03:56 +0200 Subject: [PATCH] feat: optimize docker build by multistage (#3) --- Dockerfile | 49 ++++++++++++++++++++++++++++++++++++++----------- README.md | 2 +- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 600f408..a39e9cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,10 @@ -FROM python:3.12-slim +# +# Builder image +# +FROM python:3.12-slim as builder -RUN apt-get update -RUN apt-get install -y gcc python3-dev +RUN apt-get update && \ + apt-get install -y --no-install-recommends gcc python3-dev # Directories paths as environment variables ENV APP_HOME=/app @@ -11,19 +14,43 @@ ENV VIRTUAL_ENV=/app/venv WORKDIR $APP_HOME RUN mkdir $VIRTUAL_ENV +# Copy requirements.txt +COPY requirements.txt $APP_HOME + +# Active Python venv +RUN python -m venv $VIRTUAL_ENV +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +# Install requirements.txt +RUN pip install --upgrade pip && \ + pip install --no-cache-dir -r requirements.txt + +# +# Production size image +# +FROM python:3.12-slim as production + +# Directories paths as environment variables +ENV APP_HOME=/app +ENV VIRTUAL_ENV=/app/venv + +# Set PATH to include venv +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +# Working directory +WORKDIR $APP_HOME +RUN mkdir $VIRTUAL_ENV + # Use a non-root user -RUN useradd -m cli -RUN chown cli:cli $VIRTUAL_ENV +RUN useradd -m cli && \ + chown cli:cli $VIRTUAL_ENV USER cli -# Copy requirements.txt -COPY --chown=cli:cli requirements.txt $APP_HOME +# Copy sources from builder image +COPY --from=builder --chown=cli:cli $VIRTUAL_ENV $VIRTUAL_ENV -# Active venv +# Active Python venv RUN python -m venv $VIRTUAL_ENV ENV PATH="$VIRTUAL_ENV/bin:$PATH" -RUN pip install --upgrade pip -RUN pip install --no-cache-dir -r requirements.txt - ENTRYPOINT ["app-store-connect"] diff --git a/README.md b/README.md index d978988..20ec0cc 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ We welcome contributions from the community. Whether it's improving the Dockerfi ## License -This project is released under the MIT License. See the `LICENSE` file in the repository for more details. +This project is released under the MIT License. See the [`LICENSE`](https://github.com/bednar/app-store-connect-cli?tab=MIT-1-ov-file#readme) file in the repository for more details. ## Contact