You should add the libpng-dev package to your Dockerfile:

FROM php:5.6-apache

RUN docker-php-ext-install mysql mysqli

RUN apt-get update -y && apt-get install -y sendmail libpng-dev

RUN apt-get update && \
    apt-get install -y \
        zlib1g-dev 

RUN docker-php-ext-install mbstring

RUN docker-php-ext-install zip

RUN docker-php-ext-install gd

Then go to directory with Dockerfile and run:

docker build -t sitename .

It worked in my case:

Removing intermediate container f03522715567
Successfully built 9d69212196a2

Let me know if you get any errors.

EDIT:

You should see something like this:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sitename            latest              9d69212196a2        19 minutes ago      414 MB
<none>              <none>              b6c69576a359        25 minutes ago      412.3 MB

EDIT2:

Just to double check everything:

Please run the docker build command this way:

docker build -t sitename:1.0 .

(adding :1.0 should not change anything, I added it just to have additional row in docker images output)

Then start the container:

docker run --name sitename_test -p 80:80 sitename:1.0

It should work just fine.

I assumed that apache is using standard port (80) - maybe you need to adjust that. If you have other services/containers listening on port 80 you can make your container listening on other port:

docker run --name sitename_test -p 8080:80 sitename:1.0

That will redirect the traffic from port 8080 to port 80 "inside" the container.

Normally you run container in the background. To do this add the -d option to the docker run command (but for testing purposes you can omit -d to see output in the console).

If you decide to run container in the background you can check logs using docker logs sitename_test. To follow the logs (and see updates in logs) use -f option:

docker logs -f sitename_test

Answer from lmtx on Stack Overflow
Top answer
1 of 8
234

You should add the libpng-dev package to your Dockerfile:

FROM php:5.6-apache

RUN docker-php-ext-install mysql mysqli

RUN apt-get update -y && apt-get install -y sendmail libpng-dev

RUN apt-get update && \
    apt-get install -y \
        zlib1g-dev 

RUN docker-php-ext-install mbstring

RUN docker-php-ext-install zip

RUN docker-php-ext-install gd

Then go to directory with Dockerfile and run:

docker build -t sitename .

It worked in my case:

Removing intermediate container f03522715567
Successfully built 9d69212196a2

Let me know if you get any errors.

EDIT:

You should see something like this:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sitename            latest              9d69212196a2        19 minutes ago      414 MB
<none>              <none>              b6c69576a359        25 minutes ago      412.3 MB

EDIT2:

Just to double check everything:

Please run the docker build command this way:

docker build -t sitename:1.0 .

(adding :1.0 should not change anything, I added it just to have additional row in docker images output)

Then start the container:

docker run --name sitename_test -p 80:80 sitename:1.0

It should work just fine.

I assumed that apache is using standard port (80) - maybe you need to adjust that. If you have other services/containers listening on port 80 you can make your container listening on other port:

docker run --name sitename_test -p 8080:80 sitename:1.0

That will redirect the traffic from port 8080 to port 80 "inside" the container.

Normally you run container in the background. To do this add the -d option to the docker run command (but for testing purposes you can omit -d to see output in the console).

If you decide to run container in the background you can check logs using docker logs sitename_test. To follow the logs (and see updates in logs) use -f option:

docker logs -f sitename_test

2 of 8
34

This answer is too late, but it will help.

RUN apt-get update && \
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ && \
docker-php-ext-install gd
🌐
Docker Community
forums.docker.com › docker hub
Official PHP Image - How to add more extensions? - Docker Hub - Docker Community Forums
December 11, 2021 - Hi, i need to know how exactly i can add (for php8.1.0-fpm) the following (for docker rootless production system): GD, intl, mysqli, pdo_mysql, zip, opcache PS: I dont know if it’s important… i will use nginx, not apa…
Discussions

docker-php-ext-enable gd - cannot create directory
when i tried to install GD extension using docker-php-ext-install gd command the process stopped because docker-php-ext-enable gd couldn't create ini file in the following directory /usr/local/... More on github.com
🌐 github.com
6
September 24, 2016
Issue with installing php extension gd
Since this morning I started getting the following error when trying to enable the PHP GD extension: /usr/local/bin/docker-php-ext-enable: 108: /usr/local/bin/docker-php-ext-enable: cannot create /conf.d/docker-php-ext-gd.ini: Directory nonexistent I’ve not touched the .circleci/config.yml ... More on discuss.circleci.com
🌐 discuss.circleci.com
1
1
November 16, 2018
How to Enable PHP extensions via Dockerfile?
Ensure the extensions are installed and then copy a php.ini into the container with the extensions enabled. More on reddit.com
🌐 r/docker
4
1
October 4, 2021
docker - How to add php gd extension to Dockerfile - Stack Overflow
In my case (image php:7.1-apache) libpng-dev' is mandatory, otherwise: E: Package 'libpng12-dev' has no installation candidate 2022-01-31T23:43:01.607Z+00:00 ... RUN apk update RUN apk add libpng libpng-dev libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev gd && docker-php-ext-install gd More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › sertxudev › install-gd-in-a-php-docker-image-1ah9
Install GD in a PHP Docker image - DEV Community
December 26, 2024 - In order to install the GD extension in a Docker image, you should add the following instruction to you Dockerfile. RUN set -eux; \ docker-php-ext-configure gd --with-freetype --with-jpeg; \ docker-php-ext-install exif gd
🌐
GitHub
gist.github.com › shov › f34541feae29afedd93208df4bf428f3
Docker PHP 7.2 fpm with GD jpg, png suppot · GitHub
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \ && docker-php-ext-install gd
🌐
GitHub
github.com › docker-library › php › issues › 304
docker-php-ext-enable gd - cannot create directory · Issue #304 · docker-library/php
September 24, 2016 - FROM php:5.6.25-apache RUN apt-get update && \ apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \ docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \ docker-php-ext-install gd
Author   itshaadi
🌐
Stuff-things
stuff-things.net › 2021 › 12 › 13 › taking-the-pain-out-of-php-development-with-docker-compose
Taking the Pain Out of PHP Development with Docker Compose
December 14, 2021 - FROM php:7.2-apache - start with the PHP 7.2 image, anything after will be layered on that. RUN apt-get update && apt-get -y install libjpeg-dev libpng-dev zlib1g-dev git zip - install the required dependencies to build the GD extension. RUN docker-php-ext-configure gd [...] - Configure, build, ...
🌐
GitHub
github.com › mlocati › docker-php-extension-installer
GitHub - mlocati/docker-php-extension-installer: Easily install PHP extensions in Docker containers · GitHub
RUN --mount=type=bind,from=mlocati/php-extension-installer:latest,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \ install-php-extensions gd xdebug · Warning: by using this method you may use an outdated version of the Docker image image.
Starred by 4.9K users
Forked by 431 users
Languages   Shell 84.2% | PHP 13.8% | JavaScript 1.6%
Find elsewhere
🌐
Underweb
underweb.dev › blog › making-php-gd-work-in-docker
Making PHP-GD work in Docker | UnderWeb
FROM php:8.1-fpm RUN apt-get update \ && apt-get install -y --no-install-recommends \ libz-dev \ libpq-dev \ libssl-dev \ libzip-dev \ libxml2-dev \ libxml2 \ libgd-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev libjpeg-dev jpegoptim libwebp-dev optipng pngquant gifsicle \ libmcrypt-dev \ libcurl4-openssl-dev \ libonig-dev \ curl \ unzip \ zip \ ca-certificates curl gnupg \ && apt-get clean \ && docker-php-ext-configure gd \ --with-freetype=/usr/include/ \ --with-jpeg=/usr/lib/x86_64-linux-gnu \ --with-webp=/usr/lib/x86_64-linux-gnu \ && docker-php-ext-install \ gd \ exif \ xml \ curl \ opcache \ intl \ pdo_mysql \ pdo \ pcntl \ zip \ intl \ mysqli \ mbstring \ && docker-php-ext-enable gd \ && rm -rf /var/lib/apt/lists/*;
🌐
Docker Hub
hub.docker.com › r › laradock › php-fpm › dockerfile
laradock/php-fpm Dockerfile
# #---------------------------... extention docker-php-ext-install pdo_mysql; \ # Install the PHP pdo_pgsql extention docker-php-ext-install pdo_pgsql; \ # Install the PHP gd library docker-php-ext-configure gd \ --prefix=/usr \ --with-jpeg \ --with-freetype; \ ...
🌐
Reddit
reddit.com › r/docker › how to enable php extensions via dockerfile?
r/docker on Reddit: How to Enable PHP extensions via Dockerfile?
October 4, 2021 -

Hi, I'm new to Docker, so please forgive me if this question is stupid.

I'm trying to build a custom image via a Dockerfile. This is the file:

FROM php:7.4-fpm

# Install Composer
COPY --from=composer /usr/bin/composer /usr/bin/composer

# Install unzip utility and libs needed by zip PHP extension
RUN apt-get update && apt-get install -y \
    zlib1g-dev \
    libzip-dev \
    unzip
RUN docker-php-ext-install zip

This is my docker-compose.yml

version: "3.7"

services:

  web:
    image: nginx
    ports:
      - 80:80

  php:
    build: .

Now, I go to the "php" container and create a terminal. (I guess this is correct?)

Now, I'm trying to install Magento 2 via composer:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2

Unfortunately I'm getting this error message:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires magento/product-community-edition 2.4.3 -> satisfiable by magento/product-community-edition[2.4.3].
    - magento/product-community-edition 2.4.3 requires ext-gd * -> it is missing from your system. Install or enable PHP's gd extension.
  Problem 2
    - magento/magento2-functional-testing-framework[3.0.0, ..., 3.7.0] require ext-intl * -> it is missing from your system. Install or enable PHP's intl extension.
    - Root composer.json requires magento/magento2-functional-testing-framework ^3.0 -> satisfiable by magento/magento2-functional-testing-framework[3.0.0, ..., 3.7.0].

To enable extensions, verify that they are enabled in your .ini files:
    - 
    - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
    - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

If I understand it correctly I need to enable these two extensions? But how would I do this?

Thank you very much in advance.

🌐
Docker Hub
hub.docker.com › _ › php
php - Official Image | Docker Hub
If you are having difficulty figuring ... Debian (apt) and Alpine (apk) packages. For example, to install the GD extension you simply have to run install-php-extensions gd....
🌐
Sertxu
sertxu.dev › blog › install-gd-in-a-php-docker-image
Install GD in a PHP Docker image | sertxu.dev
April 21, 2025 - In order to install the GD extension in a Docker image, you should add the following instruction to you Dockerfile. RUN set -eux; \ docker-php-ext-configure gd --with-freetype --with-jpeg; \ docker-php-ext-install exif gd
🌐
GitHub
github.com › docker-library › php › issues › 913
GD JPEG extension unable to install with PHP 7.4 · Issue #913 · docker-library/php
December 1, 2019 - RUN docker-php-ext-configure gd --with-freetype-dir=/usr/lib --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib && \ docker-php-ext-install gd
Author   zachrattner
🌐
Smith Talks Tech
smithtalkstech.com › home › adding gd support to a docker image
Adding GD support to a Docker image - Smith Talks Tech
March 31, 2022 - RUN NUMPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \ && docker-php-ext-install -j${NUMPROC} gd
🌐
GitHub
github.com › docker-library › php › issues › 815
can't install gd php7.2-fpm · Issue #815 · docker-library/php
April 17, 2019 - FROM php:7.2-fpm RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && docker-php-ext-install -j$(nproc) iconv && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && docker-php-ext-install -j$(nproc) gd
Author   miguelBarreiro85
🌐
Shouts
shouts.dev › snippets › how-to-install-php-gd-in-docker
How to Install PHP GD in Docker - Shouts.dev
November 29, 2022 - After 9 years, Shouts.dev is closing. Thank you to 60,000+ monthly readers.