【问题标题】:Composer post-install scripts not executedComposer 安装后脚本未执行
【发布时间】:2022-01-21 17:47:53
【问题描述】:

我正在尝试构建一个包含 PHP 应用程序的 docker 映像。

此应用程序通过 composer.json 安装一些依赖项,并且在 composer 安装后,需要完成一些自定义(例如,必须将一些文件从供应商文件夹复制到其他位置等等)。

所以我将这些步骤写成 bash 命令并放在 composer.json post-install-cmd 部分。

这是我的composer.json(我省略了细节,但结构是一样的):

{
  "name": "MyApp",
  "description": "My Service",
  "type": "project",
  "repositories": {
    "a-fancy-library-i-depend-on": {
      "type": "package",
      "package": {
        "name": "a-fancy-library-i-depend-on",
        "version": "1.0",
        "source": {
          "url": "https://github.com/a-fancy-library-i-depend-on",
          "type": "git",
          "reference": "master"
        }
      }
    },
  },
  "require": {
    "symfony/filesystem": "5.2.6",
    "symfony/yaml": "^4.2"
  },
  "require-dev": {
    "a-fancy-library-i-depend-on": "*"
  },
  "scripts": {
    "post-install-cmd": [
      "rm -rf ./src/setup",
      "cp -r vendor/a-fancy-library-i-depend-on/setup/ ./src/setup",
      "mkdir vendor/a-fancy-library-i-depend-on/log"
    ]
  },
  "autoload": {
    "classmap": [
      "src/"
    ]
  },
  "config": {
    "allow-plugins": {
      "a-fancy-library-i-depend-on/composer-module-installer": true
    }
  }
}

在我的 Dockerfile 中,我包含了一个运行 composer install 的步骤:

FROM php:7.4.20-fpm

RUN apt-get update && apt-get install -y git zlib1g-dev libzip-dev libpng-dev libicu-dev g++ zip unzip

RUN docker-php-ext-install zip gd intl

RUN docker-php-ext-configure intl

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

ADD code /code

WORKDIR /code

RUN composer install

问题是“安装后”脚本没有执行。

镜像构建成功,但安装文件夹为空。

如果我从此图像运行容器并在其上输入 bash

 docker container run -ti my-image:local bash

然后我尝试运行

# composer install

瞧!脚本被执行。

这是构建映像命令的 composer install 阶段输出:

Step 10/15 : RUN composer install
 ---> Running in af5ffb3ef4e9
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 76 installs, 0 updates, 0 removals
  - Locking gettext/gettext (v4.8.6)
  - Locking gettext/languages (2.9.0)
  - Locking phpfastcache/riak-client (3.4.3)
  - Locking phpmailer/phpmailer (v6.5.3)
  - Locking psr/cache (1.0.1)
  - Locking psr/container (1.1.2)
  - Locking psr/event-dispatcher (1.0.0)
  - Locking psr/log (1.1.4)
  - Locking robrichards/xmlseclibs (3.1.1)
  - Locking simplesamlphp/assert (v0.0.13)
  - Locking simplesamlphp/composer-module-installer (v1.1.8)
  - Locking simplesamlphp/saml2 (v4.4.0)
  - Locking simplesamlphp/simplesamlphp (v1.19.4)
  - Locking simplesamlphp/simplesamlphp-module-adfs (v0.9.9)
  - Locking simplesamlphp/simplesamlphp-module-authcrypt (v0.9.4)
  - Locking simplesamlphp/simplesamlphp-module-authfacebook (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-authorize (v0.9.4)
  - Locking simplesamlphp/simplesamlphp-module-authtwitter (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-authwindowslive (v0.9.1)
  - Locking simplesamlphp/simplesamlphp-module-authx509 (v0.9.9)
  - Locking simplesamlphp/simplesamlphp-module-authyubikey (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-cas (v0.9.1)
  - Locking simplesamlphp/simplesamlphp-module-cdc (v0.9.2)
  - Locking simplesamlphp/simplesamlphp-module-consent (v0.9.8)
  - Locking simplesamlphp/simplesamlphp-module-consentadmin (v0.9.2)
  - Locking simplesamlphp/simplesamlphp-module-discopower (v0.10.1)
  - Locking simplesamlphp/simplesamlphp-module-exampleattributeserver (v1.0.0)
  - Locking simplesamlphp/simplesamlphp-module-expirycheck (v0.9.4)
  - Locking simplesamlphp/simplesamlphp-module-ldap (v0.9.17)
  - Locking simplesamlphp/simplesamlphp-module-memcachemonitor (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-memcookie (v1.2.2)
  - Locking simplesamlphp/simplesamlphp-module-metarefresh (v0.9.7)
  - Locking simplesamlphp/simplesamlphp-module-negotiate (v0.9.12)
  - Locking simplesamlphp/simplesamlphp-module-oauth (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-preprodwarning (v0.9.3)
  - Locking simplesamlphp/simplesamlphp-module-radius (v0.9.4)
  - Locking simplesamlphp/simplesamlphp-module-riak (v0.9.1)
  - Locking simplesamlphp/simplesamlphp-module-sanitycheck (v0.9.1)
  - Locking simplesamlphp/simplesamlphp-module-smartattributes (v0.9.2)
  - Locking simplesamlphp/simplesamlphp-module-sqlauth (v0.9.4)
  - Locking simplesamlphp/simplesamlphp-module-statistics (v0.9.6)
  - Locking simplesamlphp/twig-configurable-i18n (v2.3.4)
  - Locking symfony/cache (v5.4.2)
  - Locking symfony/cache-contracts (v2.5.0)
  - Locking symfony/config (v5.4.2)
  - Locking symfony/console (v5.4.2)
  - Locking symfony/dependency-injection (v5.4.2)
  - Locking symfony/deprecation-contracts (v2.5.0)
  - Locking symfony/error-handler (v5.4.2)
  - Locking symfony/event-dispatcher (v5.4.0)
  - Locking symfony/event-dispatcher-contracts (v2.5.0)
  - Locking symfony/filesystem (v5.2.6)
  - Locking symfony/finder (v5.4.2)
  - Locking symfony/framework-bundle (v5.4.2)
  - Locking symfony/http-foundation (v5.4.2)
  - Locking symfony/http-kernel (v5.4.2)
  - Locking symfony/polyfill-ctype (v1.24.0)
  - Locking symfony/polyfill-intl-grapheme (v1.24.0)
  - Locking symfony/polyfill-intl-normalizer (v1.24.0)
  - Locking symfony/polyfill-mbstring (v1.24.0)
  - Locking symfony/polyfill-php72 (v1.24.0)
  - Locking symfony/polyfill-php73 (v1.24.0)
  - Locking symfony/polyfill-php80 (v1.24.0)
  - Locking symfony/polyfill-php81 (v1.24.0)
  - Locking symfony/routing (v5.4.0)
  - Locking symfony/service-contracts (v2.5.0)
  - Locking symfony/string (v5.4.2)
  - Locking symfony/var-dumper (v5.4.2)
  - Locking symfony/var-exporter (v5.4.2)
  - Locking symfony/yaml (v4.4.36)
  - Locking twig/extensions (v1.5.4)
  - Locking twig/twig (v2.14.10)
  - Locking webmozart/assert (1.10.0)
  - Locking whitehat101/apr1-md5 (v1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 76 installs, 0 updates, 0 removals
  - Downloading symfony/polyfill-php72 (v1.24.0)
  - Downloading symfony/polyfill-mbstring (v1.24.0)
  - Downloading symfony/polyfill-ctype (v1.24.0)
  - Downloading twig/twig (v2.14.10)
  - Downloading symfony/yaml (v4.4.36)
  - Downloading symfony/polyfill-php80 (v1.24.0)
  - Downloading symfony/var-exporter (v5.4.2)
  - Downloading symfony/deprecation-contracts (v2.5.0)
  - Downloading symfony/routing (v5.4.0)
  - Downloading symfony/polyfill-php73 (v1.24.0)
  - Downloading symfony/http-foundation (v5.4.2)
  - Downloading psr/event-dispatcher (1.0.0)
  - Downloading symfony/event-dispatcher-contracts (v2.5.0)
  - Downloading symfony/event-dispatcher (v5.4.0)
  - Downloading symfony/var-dumper (v5.4.2)
  - Downloading psr/log (1.1.4)
  - Downloading symfony/error-handler (v5.4.2)
  - Downloading symfony/http-kernel (v5.4.2)
  - Downloading symfony/polyfill-php81 (v1.24.0)
  - Downloading symfony/finder (v5.4.2)
  - Downloading symfony/filesystem (v5.2.6)
  - Downloading psr/container (1.1.2)
  - Downloading symfony/service-contracts (v2.5.0)
  - Downloading symfony/dependency-injection (v5.4.2)
  - Downloading symfony/config (v5.4.2)
  - Downloading psr/cache (1.0.1)
  - Downloading symfony/cache-contracts (v2.5.0)
  - Downloading symfony/cache (v5.4.2)
  - Downloading symfony/framework-bundle (v5.4.2)
  - Downloading symfony/polyfill-intl-normalizer (v1.24.0)
  - Downloading symfony/polyfill-intl-grapheme (v1.24.0)
  - Downloading symfony/string (v5.4.2)
  - Downloading symfony/console (v5.4.2)
  - Downloading twig/extensions (v1.5.4)
  - Downloading simplesamlphp/twig-configurable-i18n (v2.3.4)
  - Downloading simplesamlphp/simplesamlphp (v1.19.4)
  - Downloading simplesamlphp/composer-module-installer (v1.1.8)
  - Downloading gettext/languages (2.9.0)
  - Downloading webmozart/assert (1.10.0)
  - Downloading simplesamlphp/simplesamlphp-module-statistics (v0.9.6)
  - Downloading simplesamlphp/simplesamlphp-module-sqlauth (v0.9.4)
  - Downloading simplesamlphp/simplesamlphp-module-smartattributes (v0.9.2)
  - Downloading simplesamlphp/simplesamlphp-module-sanitycheck (v0.9.1)
  - Downloading phpfastcache/riak-client (3.4.3)
  - Downloading simplesamlphp/simplesamlphp-module-riak (v0.9.1)
  - Downloading simplesamlphp/simplesamlphp-module-radius (v0.9.4)
  - Downloading simplesamlphp/simplesamlphp-module-preprodwarning (v0.9.3)
  - Downloading simplesamlphp/simplesamlphp-module-oauth (v0.9.3)
  - Downloading simplesamlphp/simplesamlphp-module-ldap (v0.9.17)
  - Downloading simplesamlphp/simplesamlphp-module-negotiate (v0.9.12)
  - Downloading simplesamlphp/simplesamlphp-module-metarefresh (v0.9.7)
  - Downloading simplesamlphp/simplesamlphp-module-memcookie (v1.2.2)
  - Downloading simplesamlphp/simplesamlphp-module-memcachemonitor (v0.9.3)
  - Downloading simplesamlphp/simplesamlphp-module-expirycheck (v0.9.4)
  - Downloading simplesamlphp/simplesamlphp-module-exampleattributeserver (v1.0.0)
  - Downloading simplesamlphp/simplesamlphp-module-discopower (v0.10.1)
  - Downloading simplesamlphp/simplesamlphp-module-consent (v0.9.8)
  - Downloading simplesamlphp/simplesamlphp-module-consentadmin (v0.9.2)
  - Downloading simplesamlphp/simplesamlphp-module-cdc (v0.9.2)
  - Downloading simplesamlphp/simplesamlphp-module-cas (v0.9.1)
  - Downloading simplesamlphp/simplesamlphp-module-authyubikey (v0.9.3)
  - Downloading simplesamlphp/simplesamlphp-module-authx509 (v0.9.9)
  - Downloading simplesamlphp/simplesamlphp-module-authwindowslive (v0.9.1)
  - Downloading simplesamlphp/simplesamlphp-module-authtwitter (v0.9.3)
  - Downloading simplesamlphp/simplesamlphp-module-authorize (v0.9.4)
  - Downloading simplesamlphp/simplesamlphp-module-authfacebook (v0.9.3)
  - Downloading whitehat101/apr1-md5 (v1.0.0)
  - Downloading simplesamlphp/simplesamlphp-module-authcrypt (v0.9.4)
  - Downloading simplesamlphp/simplesamlphp-module-adfs (v0.9.9)
  - Downloading robrichards/xmlseclibs (3.1.1)
  - Downloading simplesamlphp/saml2 (v4.4.0)
  - Downloading simplesamlphp/assert (v0.0.13)
  - Downloading phpmailer/phpmailer (v6.5.3)
  - Downloading gettext/gettext (v4.8.6)
  0/74 [>---------------------------]   0%
  3/74 [=>--------------------------]   4%
 12/74 [====>-----------------------]  16%
 20/74 [=======>--------------------]  27%
 27/74 [==========>-----------------]  36%
 33/74 [============>---------------]  44%
 37/74 [==============>-------------]  50%
 39/74 [==============>-------------]  52%
 43/74 [================>-----------]  58%
 44/74 [================>-----------]  59%
 47/74 [=================>----------]  63%
 51/74 [===================>--------]  68%
 54/74 [====================>-------]  72%
 58/74 [=====================>------]  78%
 62/74 [=======================>----]  83%
 63/74 [=======================>----]  85%
 67/74 [=========================>--]  90%
 73/74 [===========================>]  98%
 74/74 [============================] 100%
  - Installing symfony/polyfill-php72 (v1.24.0): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.24.0): Extracting archive
  - Installing symfony/polyfill-ctype (v1.24.0): Extracting archive
  - Installing twig/twig (v2.14.10): Extracting archive
  - Installing symfony/yaml (v4.4.36): Extracting archive
  - Installing symfony/polyfill-php80 (v1.24.0): Extracting archive
  - Installing symfony/var-exporter (v5.4.2): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
  - Installing symfony/routing (v5.4.0): Extracting archive
  - Installing symfony/polyfill-php73 (v1.24.0): Extracting archive
  - Installing symfony/http-foundation (v5.4.2): Extracting archive
  - Installing psr/event-dispatcher (1.0.0): Extracting archive
  - Installing symfony/event-dispatcher-contracts (v2.5.0): Extracting archive
  - Installing symfony/event-dispatcher (v5.4.0): Extracting archive
  - Installing symfony/var-dumper (v5.4.2): Extracting archive
  - Installing psr/log (1.1.4): Extracting archive
  - Installing symfony/error-handler (v5.4.2): Extracting archive
  - Installing symfony/http-kernel (v5.4.2): Extracting archive
  - Installing symfony/polyfill-php81 (v1.24.0): Extracting archive
  - Installing symfony/finder (v5.4.2): Extracting archive
  - Installing symfony/filesystem (v5.2.6): Extracting archive
  - Installing psr/container (1.1.2): Extracting archive
  - Installing symfony/service-contracts (v2.5.0): Extracting archive
  - Installing symfony/dependency-injection (v5.4.2): Extracting archive
  - Installing symfony/config (v5.4.2): Extracting archive
  - Installing psr/cache (1.0.1): Extracting archive
  - Installing symfony/cache-contracts (v2.5.0): Extracting archive
  - Installing symfony/cache (v5.4.2): Extracting archive
  - Installing symfony/framework-bundle (v5.4.2): Extracting archive
  - Installing symfony/polyfill-intl-normalizer (v1.24.0): Extracting archive
  - Installing symfony/polyfill-intl-grapheme (v1.24.0): Extracting archive
  - Installing symfony/string (v5.4.2): Extracting archive
  - Installing symfony/console (v5.4.2): Extracting archive
  - Installing twig/extensions (v1.5.4): Extracting archive
  - Installing simplesamlphp/twig-configurable-i18n (v2.3.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp (v1.19.4): Extracting archive
  0/36 [>---------------------------]   0%
 10/36 [=======>--------------------]  27%
 20/36 [===============>------------]  55%
 30/36 [=======================>----]  83%
 35/36 [===========================>]  97%
 36/36 [============================] 100%
  - Installing simplesamlphp/composer-module-installer (v1.1.8): Extracting archive
  - Installing gettext/languages (2.9.0): Extracting archive
  - Installing webmozart/assert (1.10.0): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-statistics (v0.9.6): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-sqlauth (v0.9.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-smartattributes (v0.9.2): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-sanitycheck (v0.9.1): Extracting archive
  - Installing phpfastcache/riak-client (3.4.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-riak (v0.9.1): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-radius (v0.9.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-preprodwarning (v0.9.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-oauth (v0.9.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-ldap (v0.9.17): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-negotiate (v0.9.12): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-metarefresh (v0.9.7): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-memcookie (v1.2.2): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-memcachemonitor (v0.9.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-expirycheck (v0.9.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-exampleattributeserver (v1.0.0): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-discopower (v0.10.1): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-consent (v0.9.8): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-consentadmin (v0.9.2): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-cdc (v0.9.2): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-cas (v0.9.1): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authyubikey (v0.9.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authx509 (v0.9.9): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authwindowslive (v0.9.1): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authtwitter (v0.9.3): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authorize (v0.9.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authfacebook (v0.9.3): Extracting archive
  - Installing whitehat101/apr1-md5 (v1.0.0): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-authcrypt (v0.9.4): Extracting archive
  - Installing simplesamlphp/simplesamlphp-module-adfs (v0.9.9): Extracting archive
  - Installing robrichards/xmlseclibs (3.1.1): Extracting archive
  - Installing simplesamlphp/saml2 (v4.4.0): Extracting archive
  - Installing simplesamlphp/assert (v0.0.13): Extracting archive
  - Installing phpmailer/phpmailer (v6.5.3): Extracting archive
  - Installing gettext/gettext (v4.8.6): Extracting archive
  0/37 [>---------------------------]   0%
 10/37 [=======>--------------------]  27%
 20/37 [===============>------------]  54%
 29/37 [=====================>------]  78%
 37/37 [============================] 100%
26 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package simplesamlphp/simplesamlphp-module-authfacebook is abandoned, you should avoid using it. No replacement was suggested.
Package simplesamlphp/simplesamlphp-module-authwindowslive is abandoned, you should avoid using it. No replacement was suggested.
Package simplesamlphp/simplesamlphp-module-oauth is abandoned, you should avoid using it. No replacement was suggested.
Package simplesamlphp/simplesamlphp-module-riak is abandoned, you should avoid using it. No replacement was suggested.
Package twig/extensions is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
32 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Removing intermediate container af5ffb3ef4e9

如何调试这个问题?

【问题讨论】:

  • 我知道脚本没有被执行,因为必须复制的文件不在目标文件夹中。顺便说一句,我发现如果我第二次重复 composer install 脚本会被执行,但在我看来这不是一个非常干净的解决方案......
  • No composer.lock file present - 这不是一个好兆头。你不应该在没有锁定文件的情况下运行composer install
  • 另外,为什么需要在vendor文件夹中手动新建文件夹?你不应该以任何方式修改这些内容,并且有一个日志文件夹有一个巨大的危险信号
  • 我知道这不是一个好习惯,但我正在使用(某种)自定义库,强制这样做(我的意思是在供应商文件夹中写东西)......跨度>

标签: composer-php


【解决方案1】:

请查看Composer scripts 的文档。它解释得很清楚:

post-install-cmd:在安装命令执行后出现锁定文件。

如果您在使用composer install 时锁定文件 存在(如控制台输出所示),则不会触发此事件。

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多