docker-php-extension-installer icon indicating copy to clipboard operation
docker-php-extension-installer copied to clipboard

[feature req] add option to preserve compiled sources to allow reuse in multi-staged builds using new buildx layer caching

Open quentincaffeino opened this issue 2 years ago • 3 comments

Short story: each time layer above the layer that installs php dependencies change, this layer has to redo all the work. Also in multistaged builds some dependencies might repeat. While some of it is pretty fast (like downloading packages) some of it is not as fast (compilation from source) and can be optimized.

I had a look at the code and it seems like these two lines would need to be wrapped in a condition (not sure about /tmp/pickle) https://github.com/mlocati/docker-php-extension-installer/blob/c516929275abe93ec10b617015696fe35890f9cc/install-php-extensions#L3154-L3155

And maybe to prevent redownloading same packages getPackageSource could be updated too.

quentincaffeino avatar Nov 11 '21 22:11 quentincaffeino

A pull request is welcome

mlocati avatar Nov 11 '21 22:11 mlocati

A pull request is welcome

Will try tomorrow

quentincaffeino avatar Nov 11 '21 22:11 quentincaffeino

Hey @mlocati,

so I'm trying to implement this right now and can't find why it's not working, maybe you would have some idea?

Here is the patch:

diff --git a/install-php-extensions b/install-php-extensions
index 4520d3e..69e3e12 100755
--- a/install-php-extensions
+++ b/install-php-extensions
@@ -3151,8 +3151,13 @@ cleanup() {
                                ;;
                esac
        fi
-       docker-php-source delete
-       rm -rf /tmp/src
+       case "${IPE_KEEP_SOURCE:-}" in
+               1 | y* | Y*) ;;
+               *)
+                       docker-php-source delete
+                       rm -rf /tmp/src
+                       ;;
+       esac
        rm -rf /tmp/pickle
        rm -rf /tmp/pickle.tmp
        rm -rf "$CONFIGURE_FILE"
@@ -3167,7 +3172,13 @@ cleanup() {
                                        rm -rf /var/lib/apt/lists/*
                                        ;;
                        esac
-                       rm -rf /tmp/pear
+
+                       case "${IPE_KEEP_SOURCE:-}" in
+                               1 | y* | Y*) ;;
+                               *)
+                                       rm -rf /tmp/pear
+                                       ;;
+                       esac
                        ;;
        esac
 }

Here is build step I'm using:

RUN IPE_KEEP_SOURCE=1 IPE_KEEP_SYSPKG_CACHE=1 install-php-extensions gd

And for some reason there's nothing in /tmp after this RUN

quentincaffeino avatar Nov 12 '21 19:11 quentincaffeino

Is this also a good place to discuss if this installer should support cross-compiling? I think it could speed up the process when you want to build php app images for multiple platforms (arm64 vs amd64)

Hipska avatar Apr 25 '23 13:04 Hipska

I fully agree with @Hipska , cross compiling would be a killer feature

leolivier avatar Aug 06 '23 18:08 leolivier

@mlocati what do you think? Should I create a new issue for that request?

Hipska avatar Aug 07 '23 07:08 Hipska

I don't think I'll add support neither for cross compiling nor for storing compiled binaries: this script is already rather complex, and supporting those stuff isn't that easy: Why? Because it's not a matter of just storing compiled stuff: we also need to store the apt/apk dependencies, some system libraries are compiled and installed, and some PHP extension needs special handling.

mlocati avatar Aug 07 '23 12:08 mlocati