Have you ever profiled how long it takes for your PHP or PHP-FPM docker image to build? Probably, you have noticed that the composer install step accounts for the bulk of the image build time.
In this post, I will reveal one of the effective ways that helped us reduce the docker build time by a factor of 5. We decreased it from 5 minutes to 1.
he problem
Brayan was discussing with us that the build for our product management microservice was way too slow. He was trying to speed it up and asked us if we could give any pointers.
We discussed options of caching the vendor folder. It would have been easier if we were using a SAAS CI/CD system like Travis or CircleCI because they provide a setting to cache commonly used vendor code using a few lines of code. But because we use Bamboo to do all our builds and run tests, having a cache folder for the vendor that can be copied on each build didn’t seem like an easy solution.
With caching out of the picture, we decided to try the hirak/prestissimo package. Brayan got started implementing it right away.
The steps
After our discussion, Brayan added the hirak/prestissimo composer plugin to our base PHP-FPM image.
The changes are done in the base image so that this plugin’s goodness will be propagated to all projects using that base image.
Then he pushed the new docker image to re-run his builds. To our pleasant surprise, the results were amazing.
The results
So now the builds were rerun with the prestissimo plugin installed. The plugin page mentions the benchmark as follows:
288s -> 26s
Another post mentions:
In my case the composer update used to take around 10 minutes to finish, now after utilising this package, it turned into 1 min and less, which is blazingly faster for me.
It was not 10 times faster for us but it surely was 5 times faster for us. The build time for the PHP-FPM docker image came down from 5 minutes to a mere 1 minute.
Before
After
As you can see the same image that was taking 5 minutes to build before the plugin now takes just 1 minute, so how did that happen?
Conclusion
Prestissimo composer plugin downloads the packages in parallel and this is how it makes composer install and update faster. It does it with a prefetch all dependencies strategy.
RUN composer global require hirak/prestissimo
It will reduce the composer install/update time possibly significantly.
By optimising the performance of our builds, we can save time, effort and money too!