From 6db02a12c33ce66e08ed09bfca07db98ef91947b Mon Sep 17 00:00:00 2001
From: Artem Baranovskyi <artem.baranovsky1980@gmail.com>
Date: Fri, 10 Jan 2025 21:13:24 +0200
Subject: [PATCH] Image build is optimized.

---
 .gitlab-ci.yml          | 19 +------------------
 flask_ml_api/Dockerfile | 29 ++++-------------------------
 2 files changed, 5 insertions(+), 43 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7728975..3b20031 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -27,23 +27,7 @@ before_script:
   - mkdir -p ~/.docker
   - echo $DOCKER_CONFIG_JSON | base64 -d > ~/.docker/config.json
   - docker system prune -a -f --volumes || true
-  - docker container prune -f || true
-  - docker network prune -f || true
-  - docker image prune -a -f || true
-  - docker builder prune -f || true
-  - apt-get update
-  - apt-get install -y apt-utils
-  - apt-get clean
-  - rm -rf /var/lib/apt/lists/*
-  - rm -rf /tmp/*
-  - journalctl --vacuum-time=1s
-
-after_script:
-  - docker system prune -a -f --volumes || true
-  - docker container prune -f || true
-  - docker network prune -f || true
-  - docker image prune -a -f || true
-  - docker builder prune -f || true
+  - apt-get update && apt-get install -y apt-utils && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/*
 
 build:
   stage: build
@@ -52,7 +36,6 @@ build:
     - name: docker:20.10.16-dind
       command: ["--host=tcp://docker:2375", "--tls=false", "--storage-driver=overlay2", "--mtu=1500", "--data-root=/mnt/volume"]
   script:
-    - docker info
     - docker buildx create --use
     - docker buildx build --cache-from=type=registry,ref=22baar1mst/asyst-nlp-grader --no-cache --progress=plain -t 22baar1mst/asyst-nlp-grader:latest -f flask_ml_api/Dockerfile .
     - docker push 22baar1mst/asyst-nlp-grader:latest
diff --git a/flask_ml_api/Dockerfile b/flask_ml_api/Dockerfile
index a090370..8bfd685 100755
--- a/flask_ml_api/Dockerfile
+++ b/flask_ml_api/Dockerfile
@@ -1,37 +1,17 @@
-=FROM pytorch/pytorch:1.10.0-cuda11.3-cudnn8-runtime as build
-
-RUN apt-get update && apt-get install -y curl
-RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /usr/local/bin/rustup-init.sh
-RUN chmod +x /usr/local/bin/rustup-init.sh
-RUN /usr/local/bin/rustup-init.sh -y --default-toolchain none
-
-ENV PATH="$PATH:/root/.cargo/bin"
-
-RUN /root/.cargo/bin/rustup toolchain install stable --profile minimal --no-self-update
-
-RUN /root/.cargo/bin/rustup --version
-RUN /root/.cargo/bin/cargo --version
+FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime as base
 
 RUN apt-get update && apt-get install -y --no-install-recommends \
-    build-essential python3-venv && \
+    build-essential python3-venv curl && \
     apt-get clean && \
     rm -rf /var/lib/apt/lists/*
 
 RUN python3 -m venv /opt/myenv
 ENV PATH="/opt/myenv/bin:$PATH"
 
-WORKDIR /app
-COPY . /app
-
 RUN pip install --no-cache-dir Flask matplotlib scikit-learn transformers pandas sentence_transformers && \
-    pip install --no-cache-dir --upgrade setuptools wheel && \
-    rm -rf /root/.cache/pip
-
-FROM pytorch/pytorch:1.10.0-cuda11.3-cudnn8-runtime
+    pip install --no-cache-dir --upgrade setuptools wheel
 
-COPY --from=build /opt/myenv /opt/myenv
-
-ENV PATH="/opt/myenv/bin:$PATH"
+FROM base as build
 
 WORKDIR /app
 COPY . /app
@@ -39,5 +19,4 @@ COPY . /app
 RUN chown -R www-data:www-data /app && chmod -R 755 /app
 
 EXPOSE 5000
-
 CMD ["/opt/myenv/bin/python", "/app/api.py"]
\ No newline at end of file
-- 
GitLab