ossentoo
ossentoo

Reputation: 2019

Improve laravel php dockerfile build time and efficiency

I've got a multi-stage docker file that builds a laravel php application with sql server database access via providers.

https://gist.github.com/ossentoo/5b8e490bbd5d9743b0b472fe751e7ae0

The docker build takes about 17 minutes the first time it is run. Unfortunately, it takes around the same amount of time when i run the build most times after that as well. There doesn't appear to be any caching of packages for some unknown reason.

Can anyway see where i can add some quick and simple changes to improve the build time?

It looks like the reason for this taking the time is because of these statements:

thanks

Upvotes: 0

Views: 508

Answers (1)

Aurelijus Rozenas
Aurelijus Rozenas

Reputation: 2247

The docker will build all the steps after the first one that has changed. For example you have a command COPY resources/ /frontend/resources/ which means that all steps after it will be executed every time any file in resources/ changes.
So what you want to do is to have steps that changed rarely in the beginning (e.g., apt installs, php extensions) and steps that change often - in the end.

So first thing for the npm_builder part is to split source code copy and dependencies for npm:

COPY package.json package-lock.json 
RUN npm install

This way npm install will only run if your npm dependencies have changed.
Next COPY source code and do RUN npm run production. This will only run if the source code have changed.
Does that make sense?

You would need to do the same for composer.

A note on composer update - I am not sure what are you trying to achieve building this image, but you should only do update explicitly when updating packages. Normally you would want only to do composer install so that packages would be installed in same versions as defined in lock file. Sorry if you understand this and this is your intention.

As for apt install part - if runs on every build I think the problem is that it is setup as multistage build and second part (php image) relies on first part (npm builder) so every time you change you source code (so in essence one of the first steps) it will bust cache for all next steps they will be executed. Normally if you would have only php image apt install would happen only first time and then they would be cached for the future. Maybe you should explore other ways to build it as separate images.

Upvotes: 2

Related Questions