Php: Installing php5-gd with apt-get inside php:5.6-apache container

Created on 5 Apr 2016  路  5Comments  路  Source: docker-library/php

I need to install the php extension php5-gd.

I tried to do it by using this Dockerfile:

FROM php:5.6-apache
MAINTAINER "Phuc Dat <[email protected]>"
COPY ./templates/apache2files/piwik /var/www/html/
COPY ./templates/php.ini /usr/local/etc/php/
RUN chmod 777 -R /var/www/html/
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install php5-gd

Giving me the following error:

error: /usr/src/php/ext/php5-gd does not exist

usage: /usr/local/bin/docker-php-ext-install [-jN] ext-name [ext-name ...]
   ie: /usr/local/bin/docker-php-ext-install gd mysqli
       /usr/local/bin/docker-php-ext-install pdo pdo_mysql
       /usr/local/bin/docker-php-ext-install -j5 gd mbstring mysqli pdo pdo_mysql shmop

if custom ./configure arguments are necessary, see docker-php-ext-configure

Possible values for ext-name:
bcmath bz2 calendar ctype curl dba dom enchant exif fileinfo filter ftp gd gettext gmp hash iconv imap interbase intl json ldap mbstring mcrypt mssql mysql mysqli oc                                       i8 odbc opcache pcntl pdo pdo_dblib pdo_firebird pdo_mysql pdo_oci pdo_odbc pdo_pgsql pdo_sqlite pgsql phar posix pspell readline recode reflection session shmop sim                                       plexml snmp soap sockets spl standard sybase_ct sysvmsg sysvsem sysvshm tidy tokenizer wddx xml xmlreader xmlrpc xmlwriter xsl zip

Then I changed the Dockerfile to this, which installed the extension:

FROM php:5.6-apache
MAINTAINER "Phuc Dat <[email protected]>"
COPY ./templates/apache2files/piwik /var/www/html/
COPY ./templates/php.ini /usr/local/etc/php/
RUN chmod 777 -R /var/www/html/
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install mbstring
RUN apt-get update && \
        apt-get install -y php5-gd && \
        rm -r /var/lib/apt/lists/*

I can double check that the library is installed using:

[root@localhost ~]# docker exec -it mypiwikcontainer bash
root@4ddd1ce105a6:/var/www/html# dpkg -l php5-gd
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                          Version                     Architecture                Description
+++-=============================================-===========================-===========================-===============================================================================================
ii  php5-gd                                       5.6.19+dfsg-0+deb8u1        amd64                       GD module for php5

But I can see that the php5-gd is not loaded into the apache2 instance in my container:

[root@localhost ~]# docker exec -it mypiwikcontainer bash
root@4ddd1ce105a6:/var/www/html# php -m
[PHP Modules]
Core
ctype
curl
date
dom
ereg
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

To actually load it into the apache2 instance in the container, I tried this:

[root@localhost ~]# docker exec -it piwikcontainer bash
root@4ddd1ce105a6:/var/www/html# apachectl restart

and also this:

[root@localhost ~]# docker exec -it piwikcontainer bash
root@4ddd1ce105a6:/var/www/html# /etc/init.d/apache2 restart

Both approaches didn't work.

This all leads to the question:

How can I get php5-gd extension working inside the php:5.6-apache container?

Most helpful comment

Ok i have changed my Dockerfile to this and now the images are being shown:

FROM php:5.6-apache
        MAINTAINER "Phuc Dat <[email protected]>"
        COPY ./templates/apache2files/piwik /var/www/html/
        COPY ./templates/php.ini /usr/local/etc/php/
        RUN apt-get update && \
            apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \
            docker-php-ext-install mysqli && \
            docker-php-ext-install mbstring && \
            docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/  &&  \
            docker-php-ext-install gd
        RUN chmod 777 -R /var/www

All 5 comments

Ok i have changed my Dockerfile to this and now the images are being shown:

FROM php:5.6-apache
        MAINTAINER "Phuc Dat <[email protected]>"
        COPY ./templates/apache2files/piwik /var/www/html/
        COPY ./templates/php.ini /usr/local/etc/php/
        RUN apt-get update && \
            apt-get install -y libfreetype6-dev libjpeg62-turbo-dev && \
            docker-php-ext-install mysqli && \
            docker-php-ext-install mbstring && \
            docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/  &&  \
            docker-php-ext-install gd
        RUN chmod 777 -R /var/www

this worked for me! thanks for the help @keltik85

Fatal error: Call to undefined function imagecreatefromjpeg()

I think that should've actually been --with-jpeg-dir=/usr in the example above (instead of --with-jpeg-dir=/usr/include/).

See the wordpress official image Dockerfile for an example: https://github.com/docker-library/wordpress/blob/6a085d90853b8baffadbd3f0a41d6814a2513c11/php7.2/apache/Dockerfile#L14

@tianon: That dir is good, more likely libgd-dev missing in the apt-get install line. Example:

~
apt-get install -qq -y libgd-dev libfreetype6-dev libjpeg62-turbo-dev libpng12-dev
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
docker-php-ext-install gd
~

works for me, also taken from the docker image docs itself (or inspired by it).

Was this page helpful?
0 / 5 - 0 ratings