Skip to content
Advertisement

From which Dockerfile should composer install be called from

I need to call composer install and unsure from which Dockerfile to call it from – Dockerfile-apache or Dockerfile-php-fpm?

Should I install composer in Dockerfile-apache (and PHP CLI?) and run it from there?

Running composer install from Dockerfile-php-fpm gives me this: Composer could not find a composer.json file in /var/www/html

Docker-php-fpm

JavaScript

Docker-apache:

JavaScript

Edit #1

docker-compose.yml

JavaScript

Edit #2

This docker-compose file enables the communication via a common volume. I just need to compile, build and copy the files in Apache at this point.

JavaScript

Advertisement

Answer

I would go with neither of the above; instead, run composer install locally and copy the resulting vendor directory as part of your application.

Fetching dependencies is fundamentally part of building an application, not part of running it, so Composer shouldn’t even be installed on a production host or container. If you were writing a C application which needed to be compiled with gcc, you would run that as an earlier step, and then copy the binary into the container; composer install can be treated the same way.

So for instance, you might have a build script (to run manually, or in a CI server like Jenkins, Github Actions, Azure DevOps, etc) that went through the following steps:

  1. Clone the repo from a git repository
  2. Check out the latest tag
  3. Run composer install
  4. Run a script to minify the client-side JS
  5. Run docker-composer, copying the source code, minified JS, and vendor directory

The software inside the Docker container therefore only needs to be able to run the application, not build it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement