Docker环境下Composer使用--prefer-source无法克隆Git仓库问题排查
Composer --prefer-source 无法在Docker容器内克隆仓库
在Docker容器中搭建WordPress本地环境时,使用composer install --prefer-source或composer update --prefer-source命令,所有需要克隆的组件均安装失败,进程卡在80%进度。常规composer install/update命令可正常执行。
错误输出
- Syncing johnpbloch/wordpress-core (5.9.3) into cache - Syncing mars/mars-wp-blocks (dev-master fa67b2e) into cache - Syncing mars/wp-artixor (dev-master 79fc740) into cache - Syncing mars/wp-safe-theme (dev-master 4d8b41e) into cache - Upgrading google/apiclient-services (v0.289.0 => v0.290.1): Extracting archive - Upgrading google/apiclient (v2.13.0 => v2.13.1): Extracting archive - Upgrading aws/aws-sdk-php (3.261.7 => 3.261.12): Extracting archive - Installing johnpbloch/wordpress-core (5.9.3): Cloning 455d38c7b7bcdce3e0a1c6108eccca8de1860361 from cache Install of johnpbloch/wordpress-core failed - Installing mars/mars-wp-blocks (dev-master fa67b2e): Cloning fa67b2e0b443103f3a3640e8d7ad4705d36c328d from cache Install of mars/mars-wp-blocks failed - Installing mars/wp-artixor (dev-master 79fc740): Cloning 79fc74046c32c9f21c03b0febd4bb13779d455e7 from cache Install of mars/wp-artixor failed - Upgrading myclabs/php-enum (1.7.7 => 1.8.4): Extracting archive - Installing mars/wp-safe-theme (dev-master 4d8b41e): Cloning 4d8b41e1b51e88e4c5ea253c7a5137c82147d7f9 from cache Install of mars/wp-safe-theme failed - Upgrading wp-cli/entity-command (v2.4.1 => v2.4.2): Extracting archive 4/5 [======================>-----] 80% 10 mins
环境检测结果(composer diagnose)
Checking platform settings: OK Checking git settings: OK git version 2.30.2 Checking http connectivity to packagist: OK Checking https connectivity to packagist: OK Checking github.com oauth access: OK Checking disk free space: OK Checking pubkeys: Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0 87719BA6 8F3BB723 4E5D42D0 84A14642 Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B 0C708369 153E328C AD90147D AFE50952 OK Checking composer version: You are not running the latest stable version, run `composer self-update` to update (2.4.3 => 2.5.4) Composer version: 2.4.3 PHP version: 7.3.33 PHP binary path: /usr/local/bin/php OpenSSL version: OpenSSL 1.1.1k 25 Mar 2021 cURL version: 7.74.0 libz 1.2.11 ssl OpenSSL/1.1.1n zip: extension present, unzip present, 7-Zip not available
已尝试操作:重装组件、重新生成GitHub Token、清除Composer缓存、删除vendor目录重试,问题仍存在。
解决方案
升级Composer到最新稳定版
当前使用的Composer 2.4.3存在--prefer-source相关已知bug,执行升级后重试:composer self-update检查容器内Git配置
- 配置Git用户信息,部分仓库克隆需要身份标识:
git config --global user.name "你的全名" git config --global user.email "你的邮箱地址" - 手动克隆失败的仓库排查根源,例如:
若手动克隆失败,说明问题出在Git网络或权限,而非Composer本身。git clone https://github.com/johnpbloch/wordpress-core.git
- 配置Git用户信息,部分仓库克隆需要身份标识:
重置Composer缓存
清除缓存并删除VCS缓存目录,避免缓存损坏导致克隆失败:composer clear-cache rm -rf ~/.composer/cache/vcs之后执行
composer install --prefer-source --no-cache跳过缓存直接克隆。修正文件系统权限
- 确保当前用户对项目目录有读写权限:
chown -R $(whoami):$(whoami) /你的项目路径 - 避免用root用户执行Composer命令,Git在root环境下可能存在身份验证限制,切换到普通用户操作。
- 确保当前用户对项目目录有读写权限:
验证私有仓库权限
针对mars/*这类私有仓库,确认GitHub Token拥有仓库读取权限,且仓库已添加Token对应用户为协作者。
内容的提问来源于stack exchange,提问作者almerion
相关产品推荐
相关产品推荐

