From 971335bd93911d90e1364518c2af00a12d91c18d Mon Sep 17 00:00:00 2001 From: prv-proton Date: Thu, 12 Dec 2024 11:48:12 -0800 Subject: [PATCH] optimize --- .github/workflows/docker-auto-test.yaml | 9 +++++ backend/Dockerfile | 44 +++++++++++-------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.github/workflows/docker-auto-test.yaml b/.github/workflows/docker-auto-test.yaml index 30c21c1dc..5b11e92af 100644 --- a/.github/workflows/docker-auto-test.yaml +++ b/.github/workflows/docker-auto-test.yaml @@ -51,6 +51,15 @@ jobs: run: | sed -i 's/: true/: "true"/g; s/: false/: "false"/g' docker-compose.yml + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-docker-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-docker-${{ github.ref_name }} + ${{ runner.os }}-docker- + - name: Build and start services run: | docker-compose build diff --git a/backend/Dockerfile b/backend/Dockerfile index 2f863e90b..749325463 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,38 +1,31 @@ # Base stage for common setup FROM python:3.11-slim-bullseye as base -RUN apt-get update && apt-get install -y \ - gcc \ - && rm -rf /var/lib/apt/lists/* - -RUN pip install poetry==1.6.1 - -# Configuring poetry -RUN poetry config virtualenvs.create false - -# Copying requirements of a project -COPY pyproject.toml poetry.lock /app/ +# Install build dependencies and Poetry in one step to minimize layers +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + && pip install --no-cache-dir poetry==1.6.1 \ + && poetry config virtualenvs.create false \ + && apt-get purge -y gcc \ + && rm -rf /var/lib/apt/lists/* + +# Set work directory WORKDIR /app -# Installing requirements -RUN poetry install --only main +# Copy only dependency files first (to leverage caching) +COPY pyproject.toml poetry.lock /app/ -# Removing gcc -RUN apt-get purge -y \ - gcc \ - && rm -rf /var/lib/apt/lists/* +# Install only main dependencies +RUN poetry install --only main --no-root -# Copying the actual application, wait-for-it script, and prestart script +# Copy the rest of the application files COPY . /app/ -# Note: We mount the local directory using docker-compose so ensure these scripts also have execute permissions -# by running the following command on your host machine from the root of this project: -# chmod +x ./backend/wait-for-it.sh ./backend/lcfs/prestart.sh ./backend/lcfs/start.sh ./backend/lcfs/start-reload.sh - -# Make all startup scripts executable +# Make necessary scripts executable RUN chmod +x /app/wait-for-it.sh /app/lcfs/prestart.sh /app/lcfs/start.sh -CMD /bin/bash /app/lcfs/start.sh +# Default startup command +CMD ["/bin/bash", "/app/lcfs/start.sh"] # Production stage FROM base as prod @@ -43,5 +36,6 @@ ENV APP_ENVIRONMENT=prod FROM base as dev # Set the APP_ENVIRONMENT variable to 'development' ENV APP_ENVIRONMENT=dev + # Install additional dependencies for development -RUN poetry install +RUN poetry install --no-root