From cbc879f6180556a5a7f3072f9f35e2390d33d499 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Mon, 4 Mar 2024 22:50:00 +1100
Subject: [PATCH] [Docker] Fix broken image (#6621)

* Install grpcio faster

- Pin version
- Update build packages

* Test dockerfile before push

- SImply test if invoke is present

* Revert commit to docker-compose

* Remove VIRTUAL_ENV from Dockerfile

* Fix pip install

* Run extra checks on docker image

* Ensure mariadb links are installed:

- Closes https://github.com/inventree/InvenTree/issues/6625

* Adjust CI

 build docker image manually

* Should fail

- Test that revert fails CI

* Ok, this should break it

* Fix Dockerfile
---
 .github/workflows/docker.yaml    | 18 ++++++++++--------
 Dockerfile                       | 11 ++++++-----
 docker/install_build_packages.sh |  1 +
 requirements.in                  |  3 ++-
 requirements.txt                 |  2 +-
 5 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml
index ca4593debd..d682d05a35 100644
--- a/.github/workflows/docker.yaml
+++ b/.github/workflows/docker.yaml
@@ -74,6 +74,14 @@ jobs:
           python3 ci/version_check.py
           echo "git_commit_hash=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
           echo "git_commit_date=$(git show -s --format=%ci)" >> $GITHUB_ENV
+      - name: Test Docker Image
+        id: test-docker
+        run: |
+          docker build . --target production --tag inventree-test
+          docker run --rm inventree-test invoke --version
+          docker run --rm inventree-test invoke --list
+          docker run --rm inventree-test gunicorn --version
+          docker run --rm inventree-test pg_dump --version
       - name: Build Docker Image
         # Build the development docker image (using docker-compose.yml)
         run: docker-compose build --no-cache
@@ -137,8 +145,8 @@ jobs:
             inventree/inventree
             ghcr.io/inventree/inventree
 
-      - name: Build and Push
-        id: build-and-push
+      - name: Push Docker Images
+        id: push-docker
         if: github.event_name != 'pull_request'
         uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # pin@v5.0.0
         with:
@@ -152,9 +160,3 @@ jobs:
           build-args: |
             commit_hash=${{ env.git_commit_hash }}
             commit_date=${{ env.git_commit_date }}
-
-      - name: Sign the published image
-        if: ${{ false }} # github.event_name != 'pull_request'
-        env:
-          COSIGN_EXPERIMENTAL: "true"
-        run: cosign sign ${{ steps.meta.outputs.tags }}@${{ steps.build-and-push.outputs.digest }}
diff --git a/Dockerfile b/Dockerfile
index c87a7dea44..cca4ac6a48 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -48,8 +48,6 @@ ENV INVENTREE_BACKGROUND_WORKERS="4"
 ENV INVENTREE_WEB_ADDR=0.0.0.0
 ENV INVENTREE_WEB_PORT=8000
 
-ENV VIRTUAL_ENV=/usr/local
-
 LABEL org.label-schema.schema-version="1.0" \
       org.label-schema.build-date=${DATE} \
       org.label-schema.vendor="inventree" \
@@ -65,8 +63,11 @@ RUN apk add --no-cache \
     libjpeg libwebp zlib \
     # Weasyprint requirements : https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#alpine-3-12
     py3-pip py3-pillow py3-cffi py3-brotli pango poppler-utils openldap \
-    # Core database packages
-    postgresql13-client && \
+    # Postgres client
+    postgresql13-client \
+    # MySQL / MariaDB client
+    mariadb-client mariadb-connector-c \
+    && \
     # fonts
     apk --update --upgrade --no-cache add fontconfig ttf-freefont font-noto terminus-font && fc-cache -f
 
@@ -96,7 +97,7 @@ FROM inventree_base as prebuild
 
 ENV PATH=/root/.local/bin:$PATH
 RUN ./install_build_packages.sh --no-cache --virtual .build-deps && \
-    pip install --user uv --no-cache-dir && pip install -r base_requirements.txt -r requirements.txt --no-cache && \
+    pip install --user -r base_requirements.txt -r requirements.txt --no-cache && \
     apk --purge del .build-deps
 
 # Frontend builder image:
diff --git a/docker/install_build_packages.sh b/docker/install_build_packages.sh
index ca7ea49ddf..358a256e7f 100644
--- a/docker/install_build_packages.sh
+++ b/docker/install_build_packages.sh
@@ -4,6 +4,7 @@
 # Note that for postgreslql, we use the 13 version, which matches the version used in the InvenTree docker image
 
 apk add gcc g++ musl-dev openssl-dev libffi-dev cargo python3-dev openldap-dev \
+    libstdc++ build-base linux-headers py3-grpcio \
     jpeg-dev openjpeg-dev libwebp-dev zlib-dev \
     sqlite sqlite-dev \
     mariadb-connector-c-dev mariadb-client mariadb-dev \
diff --git a/requirements.in b/requirements.in
index 05033758e8..df57ae33c5 100644
--- a/requirements.in
+++ b/requirements.in
@@ -45,7 +45,7 @@ python-barcode[images]                  # Barcode generator
 python-dotenv                           # Environment variable management
 pyyaml>=6.0.1                           # YAML parsing
 qrcode[pil]                             # QR code generator
-rapidfuzz                        # Fuzzy string matching
+rapidfuzz                               # Fuzzy string matching
 regex                                   # Advanced regular expressions
 sentry-sdk                              # Error reporting (optional)
 setuptools                              # Standard dependency
@@ -53,6 +53,7 @@ tablib[xls,xlsx,yaml]                   # Support for XLS and XLSX formats
 weasyprint                              # PDF generation
 
 # OpenTelemetry dependencies
+grpcio==1.54.2                          # Pinned to ensure docker image builds correctly
 opentelemetry-api
 opentelemetry-sdk
 opentelemetry-exporter-otlp
diff --git a/requirements.txt b/requirements.txt
index 5ef46e3ce6..2ed0583e38 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -137,7 +137,7 @@ googleapis-common-protos==1.62.0
     # via
     #   opentelemetry-exporter-otlp-proto-grpc
     #   opentelemetry-exporter-otlp-proto-http
-grpcio==1.60.1
+grpcio==1.54.2
     # via opentelemetry-exporter-otlp-proto-grpc
 gunicorn==21.2.0
 html5lib==1.1