108

I am a complete Docker novice but am having to maintain an existing system. The Dockerfile I am using is as below:

FROM php:5.6-apache

RUN docker-php-ext-install mysql mysqli

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

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

When I run 'docker build [sitename]' everything seems ok until I get the error:

configure: error: png.h not found.
The command '/bin/sh -c docker-php-ext-install gd' returned a non-zero code: 1

What is the cause of this error?

evilscary
  • 2,097
  • 5
  • 23
  • 33

5 Answers5

213

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

starball
  • 20,030
  • 7
  • 43
  • 238
lmtx
  • 5,018
  • 3
  • 20
  • 29
  • That seemed to build but while doing so I got a few lines in red appear; are these fails? – evilscary Sep 23 '16 at 10:40
  • Please paste those lines. I have done that build but did not get any warnings/errors. – lmtx Sep 23 '16 at 10:43
  • Also when I check 'docker images' it says there was a build run at the time I ran it, but the Repository column has 'None' against it. – evilscary Sep 23 '16 at 10:44
  • Please check the edited answer - formatting in comments does not work :/ – lmtx Sep 23 '16 at 10:46
  • I do get the list as you show it, but all the top lines are . When I go to the front end site the docker installation is linked to it says the modules are still missing – evilscary Sep 23 '16 at 11:18
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124037/discussion-between-lmtx-and-evilscary). – lmtx Sep 23 '16 at 12:21
  • Using this method will install incomplete gd, Some functions don't work! – Midhun Dec 12 '19 at 10:57
  • Why libpng-dev and not libpng? Isnt that not secure? – Darius.V Sep 10 '21 at 13:33
  • E: Package 'libpng-dev' has no installation candidate – Eugene Jul 21 '23 at 17:15
31

It is not the case of the OP, but I found that for those that are using php:7.4-fpm-alpine the syntax is a bit different

FROM php:7.4-fpm-alpine

# ... Other instructions ...

# Setup GD extension
RUN apk add --no-cache \
      freetype \
      libjpeg-turbo \
      libpng \
      freetype-dev \
      libjpeg-turbo-dev \
      libpng-dev \
    && docker-php-ext-configure gd \
      --with-freetype=/usr/include/ \
      # --with-png=/usr/include/ \ # No longer necessary as of 7.4; https://github.com/docker-library/php/pull/910#issuecomment-559383597
      --with-jpeg=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-enable gd \
    && apk del --no-cache \
      freetype-dev \
      libjpeg-turbo-dev \
      libpng-dev \
    && rm -rf /tmp/*

# ... Other instructions ...
phaberest
  • 3,140
  • 3
  • 32
  • 40
29

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
DiogoSantana
  • 2,404
  • 2
  • 19
  • 24
suba
  • 1,320
  • 15
  • 22
  • 1
    Options for docker-php-ext-configure have changed. now it's docker-php-ext-configure gd --with-freetype --with-jpeg. See https://github.com/docker-library/php/issues/912 – Brady Emerson Mar 25 '21 at 05:57
  • @BradyEmerson is right. You need to replace the options for `docker-php-ext-configure` as mentioned in the above comment and then works as expected. – Chris Athanasiadis Jun 07 '21 at 08:16
  • Old RUN docker-php-ext-configure gd --with-png-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-freetype-dir=/usr/include/ New RUN docker-php-ext-configure gd --with-png=/usr/include/ --with-jpeg=/usr/include/ --with-freetype=/usr/include Source - https://github.com/docker-library/php/issues/931#issuecomment-568658449 – Aransiola Oluwaseun Sep 20 '21 at 22:46
  • Just saved me from scraping the project :p No answers found anywhere – Khan Sharukh Nov 29 '21 at 09:28
  • @AransiolaOluwaseun I got unrecognized options for --with-png. Can you confirm? – DiogoSantana Dec 01 '21 at 19:39
  • What is your php version? – suba Dec 01 '21 at 19:44
  • This worked for me on my Macbook M1 Chip Docker containerr using the php image arm64v8/php:7.4-fpm. Thanks for your help. – sydadder Feb 15 '22 at 11:42
  • From PHP 7.4.0+ the image option are rename without dir https://www.php.net/manual/fr/image.installation.php – WolfCode Apr 13 '22 at 15:52
23

This Dockerfile worked with Php7 https://hub.docker.com/r/giapnh/php7-gd

FROM php:7-fpm

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN apt-get update -y && apt-get install -y libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \
    libfreetype6-dev
RUN apt-get update && \
    apt-get install -y \
        zlib1g-dev 

RUN docker-php-ext-install mbstring

RUN apt-get install -y libzip-dev
RUN docker-php-ext-install zip

RUN docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir \
    --with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \
    --enable-gd-native-ttf

RUN docker-php-ext-install gd

CMD ["php-fpm"]

EXPOSE 9000
giapnh
  • 2,950
  • 24
  • 20
  • 24
    Can you explain exactly what you needed to change to make it work? Just dumping that code here does not help anybody to learn from your answer – Nico Haase May 20 '19 at 15:51
  • 1
    Not dumping the code but the following fixed the error: function 'imagecreatefromjpeg' not found. ```RUN apt-get install -y libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev RUN docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir \ --with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \ --enable-gd-native-ttf RUN docker-php-ext-install gd``` – Asrar Feb 24 '21 at 03:51
10

Unfortunately, some php extensions have dependencies to other programs. There is a project called docker-php-extension-installer that you can use to install PHP extensions. It will make sure that the required dependencies are present as well.

Since I need that external script in multiple containers, I've put it in a shared script that I then include in the required Dockerfile.

Script (at .shared/scripts/install_php_extensions.sh)

#!/bin/sh

# add wget
apt-get update -yqq && apt-get -f install -yyq wget

# download helper script
wget -q -O /usr/local/bin/install-php-extensions https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions \
    || (echo "Failed while downloading php extension installer!"; exit 1)

# install all required extensions
chmod uga+x /usr/local/bin/install-php-extensions && sync && install-php-extensions \
    gd \
;

Dockerfile

# get the scripts from the build context and make sure they are executable
COPY .shared/scripts/ /tmp/scripts/
RUN chmod +x -R /tmp/scripts/

# install extensions
RUN /tmp/scripts/install_php_extensions.sh

Caution: Make sure to use the correct build context in this case.

Hirnhamster
  • 7,101
  • 8
  • 43
  • 73
  • I wish I used the repo earlier. I had to search my browser history to upvote your answer. Thanks. And also, using this project keeps the image lighter as it removes the unnecessary dependencies after the installation is done. – ssi-anik Jan 07 '22 at 09:49