Installing GD in Docker
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
configure: error: png.h not found. The command '/bin/sh -c docker-php-ext-install gd' returned a non-zero code: 1
5 Answers 5
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:
Removing intermediate container f03522715567 Successfully built 9d69212196a2
Let me know if you get any errors.
You should see something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE sitename latest 9d69212196a2 19 minutes ago 414 MB b6c69576a359 25 minutes ago 412.3 MB
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)
docker run —name sitename_test -p 80:80 sitename:1.0
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
Unable to enable GD with JPEG support in PHP8 container running in Docker
Tried many ways, but still unable to get GD enabled with JPEG support in PHP8 container running in Docker. Here’s the fragment of my Docker file:
FROM php:8.0.10-apache RUN apt-get -y update && apt-get -y install \ apt-utils \ vim \ rsync \ curl \ openssl \ openssh-server \ mariadb-client \ git \ zlib1g-dev \ libicu-dev \ libfreetype6-dev \ libjpeg62-turbo-dev \ libzip-dev \ libpng-dev \ g++ \ zip \ unzip \ gnupg \ gnupg2 \ unixodbc-dev RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg RUN docker-php-ext-install gd
As you see the JPEG Support is missing. What am I missing here? Thanks!
Unfortunately no. I’m aware though that the way to enable GD with jpeg support has changed as of PHP7.3
2 Answers 2
I’m using Nginx (not Apache like you) but ran into a similar issue with GD. I got GD running with jpg, png and webp by using the following lines:
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
Looking at your RUN apt-get statement, I noticed that you are using another libjpeg library: libjpeg62-turbo-dev \ . Could you check if that is the correct version? And/or try to change the name to libjpeg-turbo-dev \ (without the 62 in the name).
Fatal error: Call to undefined function: imagejpeg() on Heroku PHP. Fix?
I had GD lib installed and got the same error. The problem was in compiling without jpeg support. Here what you should do to make things work.
1. Install libjpeg
apt-get install libjpeg62-turbo-dev
2. Configure PHP with jpeg support
./configure \ --with-gd --with-jpeg-dir=/usr/lib64 # other options
3. Build PHP
make clean make make install
@DeesOomens step 2 and 3 should be executed in the directory with PHP source code. You can download it from GitHub github.com/php/php-src/releases
For building a docker image (based off php:7.2-apache-stretch), I had to use —with-jpeg-dir=/usr/include. The specific commands for the official php image for docker are a bit different (docker-php-ext-configure, docker-php-ext-enable, etc), but it’s easy to map the steps above into those.
First stop the local server XAMPP or WAMP
- Goto xampp installed folder and then go to php folder and find php.ini file
- and see if your GD library is commented out. If so, remove the comment.
- ;extension=gd (this one is commented one)
- extension=gd (Remove ; to uncomment)
Example path: C:\xampp\php\php.in
- Go to the wamp installed folder, then to the bin folder, and then go to the php folder.
- And then goto php7.0.10, php5.6.25, or something else php version folder and find php.ini file.
- ;extension=gd (this one is commented one)
- extension=gd (Remove ; to uncomment)
Example path: C:\wamp64\bin\php\php7.0.10
If you can’t find the extension=gd, add it yourself
Find «extension=» in the php.ini file and place it after any extension.
Then restart your local server, and everything should be fine.