使用 docker-php-ext-configure gd 出現 error: unrecognized options: –with-freetype-dir, –with-jpeg-dir

最近有一個需求是要使用 Docker 建立 PHP 的容器,而因為要使用到 GD 函式庫,所以在使用上遇到了一些問題在這邊跟大家分享

發生問題的原因

我們可以從兩版 Dockerfile 比較一下需要對我們的 Dockerfile 做什麼樣的修正

舊版的 Dockerfile

FROM php:7.4.1-apache

docker-php-ext-configure gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr

其實這個使用方法在 php:7.3.7-apache 是可以使用的,不過在新版的 PHP 也就是 7.4 以後,已經對 docker-php-ext-configure 的使用方法做了一點修改

新版的 Dockerfile

FROM php:7.4.1-apache

RUN docker-php-ext-configure gd --with-freetype --with-jpeg

緣由說明

我們可以看到在 #910 (comment) 看到,新版的 docker-php-ext-configure 需要改變指令

A number of extensions have been migrated to exclusively use pkg-config for the detection of library dependencies. Generally, this means that instead of using –with-foo-dir=DIR or similar only –with-foo is used. Custom library paths can be specified either by adding additional directories to PKG_CONFIG_PATH or by explicitly specifying compilation options through FOO_CFLAGS and FOO_LIBS.