Faster Docker builds with composer install ⚡

When working with PHP based projects and Docker, it is a good practice to include all the third party dependencies coming from composer install.

This really helps to reduce the time needed to spin off a new container. However, when not done right, it …


This content originally appeared on DEV Community and was authored by Iacovos Constantinou

When working with PHP based projects and Docker, it is a good practice to include all the third party dependencies coming from composer install.

This really helps to reduce the time needed to spin off a new container. However, when not done right, it can significantly increase the build time.

Throughout this post we will see how we can avoid this and optimize composer install for docker builds.

Consider the following simple example, where we copy our source code before executing composer install

WORKDIR /my-cool-app
COPY . ./
RUN composer install --no-dev --no-interaction --optimize-autoloader

While this works, there is a major flaw; we don't leverage Docker cache. Every time there is a change in our source code, the docker cache for composer install will be invalidated.

In real life, this will happen with almost every build, causing composer install to be executed even when there are no changes in our dependencies.

Not efficient, that's for sure. But how can we avoid this Consider the following example:

WORKDIR /my-cool-app
COPY composer.json ./
COPY composer.lock ./
RUN composer install --no-dev --no-interaction --no-autoloader --no-scripts
COPY . ./
RUN composer dump-autoload --optimize

Here, we are copying onlycomposer.json and composer.lock (instead of copying the entire source) right before doing composer install. This is enough to take advantage of docker cache and composer install will be executed only when composer.json or composer.lock have indeed change!

One downside is that we have to skip autoload generation and script execution since the source code is not available yet. Easy fix though. This can be done as the last step and it shouldn't affect the performance anyway.

As part of this post we saw how we can re-structure our Dockerfile for PHP projects and take better advantage of Docker cache. I hope that's helpful for your project!

Make sure to follow me on dev.to, Medium or Twitter to read more about PHP, Docker and other dev topics.

Photo by CHUTTERSNAP on Unsplash


This content originally appeared on DEV Community and was authored by Iacovos Constantinou


Print Share Comment Cite Upload Translate Updates
APA

Iacovos Constantinou | Sciencx (2021-10-28T08:17:11+00:00) Faster Docker builds with composer install ⚡. Retrieved from https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/

MLA
" » Faster Docker builds with composer install ⚡." Iacovos Constantinou | Sciencx - Thursday October 28, 2021, https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/
HARVARD
Iacovos Constantinou | Sciencx Thursday October 28, 2021 » Faster Docker builds with composer install ⚡., viewed ,<https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/>
VANCOUVER
Iacovos Constantinou | Sciencx - » Faster Docker builds with composer install ⚡. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/
CHICAGO
" » Faster Docker builds with composer install ⚡." Iacovos Constantinou | Sciencx - Accessed . https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/
IEEE
" » Faster Docker builds with composer install ⚡." Iacovos Constantinou | Sciencx [Online]. Available: https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/. [Accessed: ]
rf:citation
» Faster Docker builds with composer install ⚡ | Iacovos Constantinou | Sciencx | https://www.scien.cx/2021/10/28/faster-docker-builds-with-composer-install-%e2%9a%a1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.