Berisko
Berisko

Reputation: 643

Installing GD extension in Docker

I'm new to Docker and I'm trying to install PHP GD extension.

This is my current Dockerfile:

FROM php:7.4-fpm-alpine

RUN docker-php-ext-install mysqli pdo pdo_mysql bcmath gd

When running the docker via docker-compose build && docker-compose up -d I'm getting a lots of errors and in the end I get this message:

configure: error: Package requirements (zlib) were not met:

Package 'zlib', required by 'virtual:world', not found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ZLIB_CFLAGS
and ZLIB_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
ERROR: Service 'php' failed to build: The command '/bin/sh -c docker-php-ext-install mysqli pdo pdo_mysql bcmath gd' returned a non-zero code: 1

Without the "gd" at the end the Docker container runs normally.

What could be the problem and how could I fix it?

Upvotes: 28

Views: 41287

Answers (4)

pe7er
pe7er

Reputation: 444

In my docker using PHP8.0 I got GD working for jpg,png and webp by adding the following lines to my php.dockerfile:

FROM php:8.0-fpm-alpine

# Install dependencies for GD and install GD with support for jpeg, png webp and freetype
# Info about installing GD in PHP https://www.php.net/manual/en/image.installation.php
RUN apk add --no-cache \
        libjpeg-turbo-dev \
        libpng-dev \
        libwebp-dev \
        freetype-dev

# As of PHP 7.4 we don't need to add --with-png
RUN docker-php-ext-configure gd --with-jpeg --with-webp --with-freetype
RUN docker-php-ext-install gd

Upvotes: 19

Dmitry Leiko
Dmitry Leiko

Reputation: 4412

You can try to add these settings in Dockerfile:

FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd

Official documentation. Hope it's help you.

Upvotes: 45

FDG
FDG

Reputation: 810

One more way to approach the issue is to use install-php-extensions in Dockerfile:

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions gd xdebug

Works in Alpine and PHP8. Docs here

Upvotes: 4

Berisko
Berisko

Reputation: 643

If someone has problems installing php-gd extension in Docker, look up to @Dmitry comment or Documentation and search for "PHP Core Extensions".

You can see full code on my Github, if you want to see how am I running my NGINX and PHP with php-gd extension.

Upvotes: 0

Related Questions