diff --git a/api/Dockerfile b/api/Dockerfile
index 1f4ccd8c8ea03b2b28bf6ededb3e4c41994a297e..27e380f435defb64ee329f0b97c2ae68750b17f9 100755
--- a/api/Dockerfile
+++ b/api/Dockerfile
@@ -1,18 +1,36 @@
 # Can be "development" or "production"
 ARG ENV_TYPE
 ARG GIT_COMMIT_SHA
-FROM registry.git.fsmpi.rwth-aachen.de/videoag/backend/${ENV_TYPE}_common_py:${GIT_COMMIT_SHA}
+FROM registry.git.fsmpi.rwth-aachen.de/videoag/backend/${ENV_TYPE}_common_py:${GIT_COMMIT_SHA} AS base
 
 # READ THE NOTE on caching in the README before changing this/for more info!
 ARG PIP_CACHE_DIR=
+ARG APT_CACHE_DIR=
+RUN rm -f /etc/apt/apt.conf.d/docker-clean
+RUN if ! [ -z "$APT_CACHE_DIR" ]; then echo "Dir::Cache::Archives '$APT_CACHE_DIR';" > /etc/apt/apt.conf.d/ci_caching; fi
 
 
 ENV VIDEOAG_API_GIT_COMMIT_HASH $GIT_COMMIT_SHA
 
-COPY extra_requirements.txt ./
+
+# Install and build requirements in different image too reduce final image size
+FROM base AS builder
+
 # READ THE NOTE on caching in the README before changing this/for more info!
+COPY extra_requirements.txt ./
 RUN --mount=type=cache,target=/root/.cache/pip \
-    pip --python .venv/bin/python install -r extra_requirements.txt
+    --mount=type=cache,target=/var/cache/apt,sharing=locked \
+    --mount=type=cache,target=/var/lib/apt,sharing=locked \
+    if ! pip --python .venv/bin/python install -r extra_requirements.txt; \
+    then echo "Pip failed (packages weren't cached) but you can ignore the error above. We will install the build dependencies and try again" \
+         # Packages needed to build lxml with pip
+         && apt-get update && apt-get --no-install-recommends install -y gcc python3-dev libxml2-dev libxslt-dev \
+         && pip --python .venv/bin/python install -r extra_requirements.txt; \
+    fi
+
+FROM base AS final
+
+COPY --from=builder /code/.venv /code/.venv
 
 COPY docker_start.sh ./
 COPY .pylintrc ./
diff --git a/common_py/Dockerfile b/common_py/Dockerfile
index 5b7c3d25d0de0b5a57da9a960c184fb5e8e340b8..27a6c8bfcf0efd77513796ab4fd9bb6aaa1a30ec 100755
--- a/common_py/Dockerfile
+++ b/common_py/Dockerfile
@@ -12,19 +12,21 @@ WORKDIR /code
 RUN mkdir -p /code
 WORKDIR /code
 
+
 # Install and build requirements in different image too reduce final image size
 FROM base AS builder
 
-# Packages needed to build psycopg with pip
 # READ THE NOTE on caching in the README before changing this/for more info!
-RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
-    --mount=type=cache,target=/var/lib/apt,sharing=locked \
-    apt-get update && apt-get --no-install-recommends install -y libpq-dev gcc python3-dev
-
 COPY requirements.txt /code
-RUN python -m venv --without-pip .venv/
 RUN --mount=type=cache,target=/root/.cache/pip \
-    pip --python /code/.venv/bin/python install -r requirements.txt
+    --mount=type=cache,target=/var/cache/apt,sharing=locked \
+    --mount=type=cache,target=/var/lib/apt,sharing=locked \
+    if ! pip --python .venv/bin/python install -r requirements.txt; \
+    then echo "Pip failed (packages weren't cached) but you can ignore the error above. We will install the build dependencies and try again" \
+         # Packages needed to build psycopg with pip
+         && apt-get update && apt-get --no-install-recommends install -y gcc python3-dev libpq-dev \
+         && pip --python .venv/bin/python install -r requirements.txt; \
+    fi
 
 FROM base AS final