"phpspreadsheet" 根 composer.json 需要 PHP 扩展名 ext-gd * 但它丢失了

"phpspreadsheet" Root composer.json requires PHP extension ext-gd * but it is missing

我正在 运行创建 Laravel 应用程序并添加了 Laravel Excel,这需要 phpoffice/phpspreadsheet。 由于两个依赖项都添加到我的 composer.json...,

"maatwebsite/excel": "^3.1",
"phpoffice/phpspreadsheet": "^1.18",

我运行进入以下构建问题:

 > [vendor 3/4] RUN composer install     --no-scripts:
#14 0.883 Installing dependencies from lock file (including require-dev)
#14 0.902 Verifying lock file contents can be installed on current platform.
#14 0.934 Your lock file does not contain a compatible set of packages. Please run composer update.
#14 0.934
#14 0.934   Problem 1
#14 0.934     - Root composer.json requires PHP extension ext-gd * but it is missing from your system. Install or enable PHP's gd extension.
#14 0.934   Problem 2
#14 0.934     - phpoffice/phpspreadsheet is locked to version 1.18.0 and an update of this package was not requested.
#14 0.934     - phpoffice/phpspreadsheet 1.18.0 requires ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.
#14 0.934   Problem 3
#14 0.934     - phpoffice/phpspreadsheet 1.18.0 requires ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.
#14 0.934     - maatwebsite/excel 3.1.33 requires phpoffice/phpspreadsheet ^1.18 -> satisfiable by phpoffice/phpspreadsheet[1.18.0].
#14 0.934     - maatwebsite/excel is locked to version 3.1.33 and an update of this package was not requested.
#14 0.934
#14 0.934 To enable extensions, verify that they are enabled in your .ini files:
#14 0.934     - /usr/local/etc/php/php-cli.ini
#14 0.934     - /usr/local/etc/php/conf.d/docker-php-ext-bz2.ini
#14 0.934     - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
#14 0.934     - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
#14 0.934 You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
------
executor failed running [/bin/sh -c composer install     --no-scripts]: exit code: 2

我的 Dockerfile:

# PHP Dependencies
FROM composer:2.1.6 as vendor
COPY . /app
RUN composer install \
    --no-scripts
RUN composer dump-autoload

# Frontend
FROM node:16.7 as frontend
COPY . /app
WORKDIR /app
RUN npm install && npm run dev && rm -rf node_modules
RUN ls -R -lsah /app

# Application
FROM php:7.4.21-apache
# Install system dependencies
RUN apt-get update && apt-get install  \
    git \
    curl \
    libpng-dev \
    libxml2-dev \
    zip \
    unzip -y
RUN docker-php-ext-install pdo_mysql gd
RUN a2enmod rewrite
COPY --chown=www-data:www-data . /var/www/html
COPY --chown=www-data:www-data --from=vendor /app/vendor/ /var/www/html/vendor/
COPY --chown=www-data:www-data --from=frontend /app/public/js/ /var/www/html/public/js/
COPY --chown=www-data:www-data --from=frontend /app/public/css/ /var/www/html/public/css/
COPY --chown=www-data:www-data --from=frontend /app/public/mix-manifest.json /var/www/html/public/mix-manifest.json
RUN php artisan optimize:clear

我不确定我该从哪里继续。当我在没有 COPY --chown=www-data:www-data --from=vendor 的情况下构建容器时,我可以获得一些 php 信息:

已安装 ext-gd:

root@303d9b2b0694:/var/www/html# php -ini | grep gd
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
gd

这看起来也不错,不是吗?

root@303d9b2b0694:/var/www/html# php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File:         (none)
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed:      /usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
/usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini,
/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini

我需要作曲家:

    "require": {
        "php": "^7.4|^8.0",
        "ext-gd": "*",
        "ext-json": "*",
        "doctrine/dbal": "^3.1.1",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "guzzlehttp/guzzle": "^7.3.0",
        "jenssegers/agent": "^2.6",
        "laravel/framework": "^8.12",
        "laravel/jetstream": "^2.3.11",
        "laravel/sanctum": "^v2.11.2",
        "laravel/tinker": "^v2.6.1",
        "league/oauth2-client": "^2.6",
        "livewire/livewire": "^v2.5.1",
        "maatwebsite/excel": "^3.1",
        "microsoft/microsoft-graph": "^1.34.0",
        "motze92/office365-mail": "^2.0.7",
        "phpoffice/phpspreadsheet": "^1.18",
        "predis/predis": "^1.1"
    },

最后但同样重要的是,模块:

root@303d9b2b0694:/var/www/html# php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

已编辑

关于 PHP extension ext-gd 我的 php.info 快捷方式:

gd

GD Support => enabled
GD Version => bundled (2.1.0 compatible)
GIF Read Support => enabled
GIF Create Support => enabled
PNG Support => enabled
libPNG Version => 1.6.36
WBMP Support => enabled
XBM Support => enabled
BMP Support => enabled
TGA Read Support => enabled

已编辑 2

这个接缝处在FROM composer:2.1.6 as vendor阶段的原因。我认为错误来自 FROM php:7.4.21-apache 阶段,这让我花了很多时间来研究模块和库。

我现在将在 composer 阶段安装 GD,看看是否能解决问题

非常感谢任何提示或建议! 非常感谢!

我终于解决了这个问题。原因很简单。在第一阶段,作曲家正在从未安装 PHP extension ext-gd 的锁定文件安装依赖项。

所以我将扩展添加到 FROM composer:2.1.6 as vendor 阶段:

# PHP Dependencies
FROM composer:2.1.6 as vendor
RUN apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev && \
  docker-php-ext-configure gd \
    --with-freetype \
    --with-jpeg \
  NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) && \
  docker-php-ext-install -j$(nproc) gd && \
  apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev

COPY . /app
RUN composer install \
    --no-scripts
RUN composer dump-autoload

希望这对社区中的其他人也有帮助。