Speedy Composer installs in Docker builds

Chris Harrison
Feb 3, 2018 · 2 min read

I’m going to share a few optimisations you can make to speed up composer installs in Docker builds.

  1. Prestissimo
  2. Turn off Xdebug
  3. Composer install before codebase

1. Prestissimo

Prestissimo is a spiffing plugin for Composer which parallelises all the downloads. You just have to install it to see sometimes large speed increases — no configuration is needed. Installing it locally is simple as:

To leverage it in a Docker build, install Composer in your Dockerfile with the following:

2. Turn off Xdebug

Composer runs much quicker with Xdebug disabled. Many Linux distributions have a CLI command called phpdismod. You can use it to disable Xdebug in your Dockerfile.

If you’re installing PHP yourself as part of your image, you could also just not install Xdebug at all. It’s probably best to keep it out of production images anyway.

3. Composer install before codebase

Docker caches ‘layers’ within your images. When a layer changes, it invalidates the cache for all layers after it. This means a well optimised Dockerfile has things that don’t change much in base layers and stuff that changes frequently in later layers.

If you make the assumption that you change your codebase more often than your Composer dependencies — then your Dockerfile should run composer install before copying across your codebase. This will mean that your composer install layer will be cached even if your codebase changes. The layer will only be invalidated when you actually change your dependencies.

The rm -rf /root/.composer line is to remove Composer’s cache. This reduces the size of the resulting image. The location of the .composer directory may differ on the various Linux distributions.

Important: If you’ve already run Composer in your build context, your local vendor directory would overwrite the one in your build. The most convenient solution for this is to add /vendor/ to your .dockerignore file.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store