如何使用 composer & 处理缺失的依赖项码头工人?

2022-01-21 00:00:00 docker php composer-php

我有一个应用程序,我想用 Docker 在我的笔记本电脑上安装它以进行本地开发.该应用程序需要来自 composer 的库.

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

我想避免在我的笔记本电脑上安装 PHP 和所有必要的扩展,只是为了能够运行 composer.

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

我也不想在我的应用程序容器内部构建期间运行 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.

这对我来说似乎是通过 docker 容器安装 composer 的完美解决方案,如 here:

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 

但是,这样做时,您如何解决任何 PHP 依赖冲突?

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

例如

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

将失败

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?

推荐答案

当你运行的环境installupdaterequirecommands 与您将要执行代码的环境不同,如果您绝对确定在实际运行代码时会满足这些依赖关系,则无需在安装过程中检查它们.

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.

只需使用 --ignore-platform-reqs (文档).

在为此类项目创建映像时,我通常会在 Dockerfiles 中放入类似的内容,最低限度:

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.

相关文章