0
0

More than 1 year has passed since last update.

Docker環境でPHPでGDライブラリを有効にする方法

Last updated at Posted at 2022-08-08

ハマったこと

php:8.0-apache-bullseye環境で開発中、GDに依存しているライブラリをcomposer requireした際、Dockerの立ち上げ時に以下のエラーが出るようになった

#0 0.140 + composer update
#0 0.216 Loading composer repositories with package information
#0 1.609 Info from https://repo.packagist.org: #StandWithUkraine
#0 6.428 Updating dependencies
#0 7.318 Your requirements could not be resolved to an installable set of packages.
#0 7.318 
#0 7.318   Problem 1
#0 7.318     - maatwebsite/excel[3.1.28, ..., 3.1.30] require phpoffice/phpspreadsheet 1.16.* -> satisfiable by phpoffice/phpspreadsheet[1.16.0].
#0 7.318     - maatwebsite/excel[3.1.31, ..., 3.1.x-dev] require phpoffice/phpspreadsheet ^1.18 -> satisfiable by phpoffice/phpspreadsheet[1.18.0, ..., 1.24.1].
#0 7.318     - maatwebsite/excel 3.1.27 requires phpoffice/phpspreadsheet ^1.16 -> satisfiable by phpoffice/phpspreadsheet[1.16.0, ..., 1.24.1].
#0 7.318     - maatwebsite/excel 3.1.26 requires phpoffice/phpspreadsheet ^1.15 -> satisfiable by phpoffice/phpspreadsheet[1.15.0, ..., 1.24.1].
#0 7.318     - maatwebsite/excel[3.1.0, ..., 3.1.25] require php ^7.0 -> your php version (8.0.22) does not satisfy that requirement.
#0 7.318     - phpoffice/phpspreadsheet[1.15.0, ..., 1.24.1] require ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.
#0 7.318     - Root composer.json requires maatwebsite/excel ^3.1 -> satisfiable by maatwebsite/excel[3.1.0, ..., 3.1.x-dev].
#0 7.318 
#0 7.318 To enable extensions, verify that they are enabled in your .ini files:
#0 7.318     - /usr/local/etc/php/php.ini
#0 7.318     - /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini
#0 7.318     - /usr/local/etc/php/conf.d/docker-php-ext-intl.ini
#0 7.318     - /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini
#0 7.318     - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
#0 7.318     - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
#0 7.318 You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
#0 7.318 
#0 7.318 Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Docker環境にGDが入っていなかったのが原因。
DockerfileにGDをインストールするための記載を行う

before

FROM php:8.0-apache-bullseye
SHELL ["/bin/bash", "-oeux", "pipefail", "-c"]

# timezone
ENV TZ=Asia/Tokyo

# language
ENV LANG=ja_JP.UTF-8 \
  LANGUAGE=ja_JP:ja \
  LC_ALL=ja_JP.UTF-8

RUN apt-get update && \
  apt-get install -y locales && \
  apt-get clean && \
  locale-gen ja_JP.UTF-8 && \
  localedef -f UTF-8 -i ja_JP ja_JP.UTF-8

# composer
ENV COMPOSER_ALLOW_SUPERUSER=1 \
  COMPOSER_HOME=/composer

COPY --from=composer:2.1 /usr/bin/composer /usr/bin/composer

# package
RUN apt-get update && \
  apt-get -y install git libicu-dev libonig-dev libzip-dev unzip nodejs npm vim

after

FROM php:8.0-apache-bullseye
SHELL ["/bin/bash", "-oeux", "pipefail", "-c"]

# timezone
ENV TZ=Asia/Tokyo

# language
ENV LANG=ja_JP.UTF-8 \
  LANGUAGE=ja_JP:ja \
  LC_ALL=ja_JP.UTF-8

RUN apt-get update && \
  apt-get install -y locales && \
  apt-get clean && \
  locale-gen ja_JP.UTF-8 && \
  localedef -f UTF-8 -i ja_JP ja_JP.UTF-8

# composer
ENV COMPOSER_ALLOW_SUPERUSER=1 \
  COMPOSER_HOME=/composer

COPY --from=composer:2.1 /usr/bin/composer /usr/bin/composer

# package 3行目以降はPHPのGD拡張を利用する時の書き方なので、GD依存ライブラリを使わない場合は不要
RUN apt-get update && \
  apt-get -y install git libicu-dev libonig-dev libzip-dev unzip nodejs npm vim unzip libfreetype6-dev libjpeg62-turbo-dev libpng-dev libjpeg-dev \
  && docker-php-ext-install \
                pdo_mysql \
                bcmath \
                gd \
  && docker-php-ext-configure gd \
                --with-freetype=/usr/include/ \
                --with-jpeg=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

これで動くようになった

蛇足

docker-php-ext-configureって何?

docker-php-ext-configureとdocker-php-ext-installは、コンテナイメージに事前に圧縮されて入っているエクステンションを操作できます。

ということらしい

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0