Note: this question is not strictly PHP-oriented, since both Docker and Composer can be easily used outside of the PHP ecosystem (although such cases would be rather rare IMO).
In one of the projects I work for, the production environment is set up in a way that uses an image with Composer only (i.e. https://docs.docker.com/samples/library/composer/) to get all the dependencies, and later copies them to actual PHP/FPM container (a so-called multistage build). I couldn't figure any real sense in doing it vs simply apk add
ing composer to the main container, running composer install
on the main container, and then removing Composer from it during the build (note that this affects neither the image size nor layer count; the Composer binary could've been either added to the single RUN
layer of all the other apps of the PHP/FPM source image or just apk add
ed later during the build, and all the immediate files are removed eventually).
My current thoughts on separating Composer are:
PROS:
- if Composer breaks something, it doesn't propagate out of the container
CONS:
- the build process is slower due to the need to handle an additional image and container
- the build process gets more complicated due to the need to handle additional container and file copying
- the Composer is not run on the actual development machine, so all
ext-
checks ain't exectued and have to be forcibly disabled while installing dependencies, at the same time preventing you from verifying the completeness of the needed extensions - it makes little logical sense by itself - Dockers are intended to separate services to make it easier to handle them; Composer is IMVHO not a service, but a part of the build pipeline itself.
Am I missing something?
BTW, this question is not about scenarios like https://medium.com/@othillo/adding-composer-to-php-docker-images-using-multi-stage-builds-2a10967ae6c1 (i.e. just adding the Composer binary from an image) or https://laravel-news.com/multi-stage-docker-builds-for-laravel (where multistage build could be arguably useful to handle Webpack, Node, JS, CSS etc.); some people that actually propose the solution with separate Composer image (https://chrisguitarguy.com/2017/12/16/multi-stage-docker-php/) provide no real rationale on the possible benefit vs the alternative solution, i.e. removing the Composer after installing dependencies.