Search code examples
phpdockergddockerfilephp-extension

libgd not installed in php:5.6-cli docker


I have a project inside a container and Im trying to use the gd lib for image create.

I do inside the bash of the container this command:

apt-get install php5-gd

Nothing changes after restart and execute php -m

What should I do?

Edit:

I added to my Dockerfile:

RUN apt-get -qq update && apt-get -qq install libpng-dev
RUN docker-php-ext-install gd

I execute docker build -t [name]

But when I execute the bash and type : php -m

[PHP Modules]
Core
ctype
curl
date
dom
ereg
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib

Solution

  • Don't install gd manually. There's the docker-php-ext-install for that.

    Inside the dockerfile, add:

    RUN docker-php-ext-install gd
    

    There is a bit of an annoyance with docker-php-ext-install that you have to figure out manually what dependencies you'll need. They don't resolve automatically. Due to this, the command will crash on its own, with:

    configure: error: png.h not found.
    

    If you look for the error, you will realize that you need libpng-dev.

    Hence finally, the whole Dockerfile should look something like this:

    FROM php:5.6-cli
    RUN apt-get -qq update && apt-get -qq install libpng-dev
    RUN docker-php-ext-install gd > /dev/null
    

    docker-php-ext-install is a bit verbose in its output, hence I like to pipe its output to /dev/null. Errors and warnings will still be printed.

    You can check that the image has the proper extension by running the image:

    docker build -t php-gd .
    docker run php-gd /bin/bash -c "php -m | grep gd"
    

    Will print:

    gd
    

    Same within a docker-compose stack:

    docker-compose.yml:

    gd-example:
        build: .
        tty: true
    
    docker-compose up --build -d
    docker ps // to find container's name. You may also set in the config
    docker exec -it docker_gd-example_1 php -m | grep gd      
    

    Two sidenotes:

    1. From your question is was not clear if you built your container and then run apt-get install php5-gd from it. If so, then that would only instal php5-gd within that container and everytime you recreate it, it will be gone. Always add stuff to a container from within the Dockerfile.

    2. Give php:7.1-cli a try. Do you really need to support php 5.6? ;)