diff --git a/utilities/automake.mk b/utilities/automake.mk
index de4f6efb5..03e9096fa 100644
--- a/utilities/automake.mk
+++ b/utilities/automake.mk
@@ -42,6 +42,7 @@ EXTRA_DIST += \
     utilities/containers/Makefile \
     utilities/containers/openbfdd.patch \
     utilities/containers/py-requirements.txt \
+    utilities/containers/prepare.sh \
     utilities/containers/fedora/Dockerfile \
     utilities/containers/ubuntu/Dockerfile \
     utilities/docker/Makefile \
diff --git a/utilities/containers/fedora/Dockerfile b/utilities/containers/fedora/Dockerfile
index 019e9f138..9e17a6b20 100755
--- a/utilities/containers/fedora/Dockerfile
+++ b/utilities/containers/fedora/Dockerfile
@@ -45,41 +45,14 @@ RUN dnf -y update \
     && \
     dnf clean all
 
-# Compile sparse from source
-WORKDIR /workspace/sparse
-
-RUN git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git \
-    /workspace/sparse \
-    && \
-    make -j4 PREFIX=/usr HAVE_LLVM= HAVE_SQLITE= install
-
-# Compile OpenBFDD from source
-WORKDIR /workspace/OpenBFDD
+WORKDIR /workspace
 
 COPY $CONTAINERS_PATH/openbfdd.patch /tmp/openbfdd.patch
 
-RUN git clone https://github.com/dyninc/OpenBFDD.git \
-    /workspace/OpenBFDD \
-    && \
-    git apply /tmp/openbfdd.patch \
-    && \
-    ./autogen.sh \
-    && \
-    ./configure --enable-silent-rules \
-    && \
-    make \
-    && \
-    make install
-
-WORKDIR /workspace
-
 COPY $CONTAINERS_PATH/py-requirements.txt /tmp/py-requirements.txt
 
-# Update and install pip dependencies
-RUN python3 -m pip install --upgrade pip \
-    && \
-    python3 -m pip install wheel \
-    && \
-    python3 -m pip install -r /tmp/py-requirements.txt
+COPY $CONTAINERS_PATH/prepare.sh /tmp/prepare.sh
+
+RUN /tmp/prepare.sh
 
 CMD ["/usr/sbin/init"]
diff --git a/utilities/containers/prepare.sh b/utilities/containers/prepare.sh
new file mode 100755
index 000000000..3aac94590
--- /dev/null
+++ b/utilities/containers/prepare.sh
@@ -0,0 +1,36 @@
+#!/bin/bash -xe
+
+function compile_sparse()
+{
+    git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git \
+        /workspace/sparse
+
+    pushd sparse
+    make -j4 PREFIX=/usr HAVE_LLVM= HAVE_SQLITE= install
+    popd
+}
+
+function compile_openbfdd()
+{
+    git clone https://github.com/dyninc/OpenBFDD.git \
+        /workspace/OpenBFDD
+
+    pushd OpenBFDD
+    git apply /tmp/openbfdd.patch
+    ./autogen.sh
+    ./configure --enable-silent-rules
+    make
+    make install
+    popd
+}
+
+function install_python_dep()
+{
+    python3 -m pip install --upgrade pip
+    python3 -m pip install wheel
+    python3 -m pip install -r /tmp/py-requirements.txt
+}
+
+compile_sparse
+compile_openbfdd
+install_python_dep
diff --git a/utilities/containers/ubuntu/Dockerfile b/utilities/containers/ubuntu/Dockerfile
index ce7ce16c6..82fa0d715 100755
--- a/utilities/containers/ubuntu/Dockerfile
+++ b/utilities/containers/ubuntu/Dockerfile
@@ -45,48 +45,21 @@ RUN apt update -y \
     && \
     apt clean
 
-# Compile sparse from source
-WORKDIR /workspace/sparse
-
-RUN git clone git://git.kernel.org/pub/scm/devel/sparse/sparse.git \
-    /workspace/sparse \
-    && \
-    make -j4 PREFIX=/usr HAVE_LLVM= HAVE_SQLITE= install
-
-# Compile OpenBFDD from source
-WORKDIR /workspace/OpenBFDD
+WORKDIR /workspace
 
 COPY $CONTAINERS_PATH/openbfdd.patch /tmp/openbfdd.patch
 
-RUN git clone https://github.com/dyninc/OpenBFDD.git \
-    /workspace/OpenBFDD \
-    && \
-    git apply /tmp/openbfdd.patch \
-    && \
-    ./autogen.sh \
-    && \
-    ./configure --enable-silent-rules \
-    && \
-    make \
-    && \
-    make install
-
-WORKDIR /workspace
-
 COPY $CONTAINERS_PATH/py-requirements.txt /tmp/py-requirements.txt
 
+COPY $CONTAINERS_PATH/prepare.sh /tmp/prepare.sh
+
 # Ubuntu 24.04 marks the Python installation as externally managed, allow pip
 # to install the packages despite that.
 ENV PIP_BREAK_SYSTEM_PACKAGES 1
 
-# Update and install pip dependencies
-RUN python3 -m pip install --upgrade pip \
-    && \
-    python3 -m pip install wheel \
-    && \
-    python3 -m pip install -r /tmp/py-requirements.txt
-
 # The Python Babel fails to detect timezone when it is set to UTC only.
 ENV TZ Etc/UTC
 
+RUN /tmp/prepare.sh
+
 CMD ["/sbin/init"]
