Adam
Adam

Reputation: 29119

How to handle missing dependencies with composer & docker?

I have an application that I want to install for local development on my laptop with Docker. The application requires libraries from composer.

I would like to avoid installing PHP on my laptop and all necessary extensions just to be able to run composer.

I also don't want to run composer during the build inside of my application container, because I need the vendor folder on my local computer using mount binding.

It looked like the perfect solution to me to install composer though a docker container as explained here:

docker run --rm --interactive --tty \
  --volume $PWD:/app \
  composer install 

However, when doing so, how do you resolve any PHP dependency conflicts?

For example

docker run --rm --interactive --tty \
       --volume $PWD:/app \
       composer require phpoffice/phpspreadsheet

will fail with

Using version ^1.14 for phpoffice/phpspreadsheet
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpoffice/phpspreadsheet 1.14.1 requires ext-gd * -> the requested PHP extension gd is missing from your system.
    - phpoffice/phpspreadsheet 1.14.0 requires ext-gd * -> the requested PHP extension gd is missing from your system.
    - Installation request for phpoffice/phpspreadsheet ^1.14 -> satisfiable by phpoffice/phpspreadsheet[1.14.0, 1.14.1].

How can I solve this kind of problems?

Upvotes: 0

Views: 2397

Answers (1)

yivi
yivi

Reputation: 47648

When the environment where you are running install, update or require commands is different from the environment where you are going to execute the code, and if you are absolutely certain that these dependencies are going to be met when the code is actually run, you do not need to check them during installation.

Just use --ignore-platform-reqs (docs).

What I usually put in my Dockerfiles when creating images for this kind of project is something like this, the the very minimum:

composer install --ignore-platform-reqs --prefer-dist

The whole thing goes like this if the artefact is being prepared for production:

composer install --ignore-platform-reqs --prefer-dist --no-scripts \
 --no-progress --no-suggest --no-interaction --no-dev --no-autoloader

A couple additional steps to dump-autoload and execute post-install scripts are going to be needed in this case.

You do not elaborate how are you planning on running the result of this installation afterwards, so I'm not sure that part will be relevant for you.

Note: this is not particularly "docker" dependant. This strategy would apply any time you are creating an installation on a different machine than were you plan on running the installation.

Upvotes: 2

Related Questions