Install php7-gd in alpine - php

This is my Dockerfile
FROM php:7.1-fpm-alpine
RUN docker-php-ext-install mysqli
RUN echo "http://dl-4.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repositories &&
apk update && \
apk upgrade && \
apk add --update \
php7-gd
mysqli is ok, but it does not load GD library.
I also find gd.so in alpine container, please check the image:
Please help

You should not mix Alpine Linux 3.4, Alpine Linux edge and PHP compiled from source.
Solution 1
Use the official latest release of Alpine Linux
FROM alpine:3.5
and add
http://dl-cdn.alpinelinux.org/alpine/3.5/community
then install memcache using pecl (in php7-pear).
Solution 2
Use the docker-php-ext-install script to add gd
FROM php:7.1-fpm-alpine
RUN apk add --no-cache libpng libpng-dev && docker-php-ext-install gd && apk del libpng-dev
For more image support you can also apk add and del:
libjpeg-turbo-dev libwebp-dev zlib-dev libxpm-dev
I left the answer using the official Alpine Linux on top, since we should always try to use official docker repos. But currently the second solution is better.
The second solution was provided by ncopa at the Alpine Linux IRC channel. Thanks.

Related

How to install ZeroMQ for PHP on an Alpine Linux container?

To be able to push notifications via WebSockets from PHP using Ratchet, I need to install ZeroMQ as stated in the documentation. However I didn't find any information about how to do it for Alpine Linux. Most of the time what we can find is with apt-get, for example here. Same about the Docker images (Dockerfile) available on Docker hub.
Since the dependencies and their name seem different, how to do it with Alpine?
For those who face the same situation, I finally found how to do it:
FROM php:7-cli-alpine
RUN apk add autoconf gcc libzmq zeromq-dev zeromq coreutils build-base
RUN pecl install zmq-beta \
&& docker-php-ext-enable zmq
Source: https://smartango.com/2018/10/php-zmq-in-docker-and-checking-whether-the-c-compiler-works-no/
In php:8.0-fpm-alpine, pecl install zmq-beta threw an error and failed to compile, so I used this command:
FROM php:8.0-fpm-alpine
ENV COMPOSER_ALLOW_SUPERUSER 1
RUN set -eux && \
apk add --update-cache --no-cache libzmq zeromq-dev zeromq && \
apk add --update-cache --no-cache --virtual=.build-php-dependencies \
autoconf gcc coreutils build-base git && \
git clone https://github.com/mkoppanen/php-zmq.git && \
cd php-zmq && \
phpize && \
./configure && \
make && \
make install && \
docker-php-ext-enable zmq && \
apk del .build-php-dependencies
Reference:
https://github.com/zeromq/php-zmq/issues/200#issuecomment-610161524

Docker build: Extentions not loading via apk: exif, gd, pdo_mysql, pecl-redis. What can I do to fix my dockerfile? From php:7.4-fpm-alpine

I'm using FROM php:7.4-fpm-alpine as my image and trying to load everything I need with RUN add apk, everything seems to build correctly, but some things are not actually working when I try to use them.
Searching around I seem to have found workarounds for all of them, but now my dockerfile take a long long time to build. I'm just learning Docker and on a super slow internet connection, so this is not ideal.
I'm wondering if I am doing something wrong? Or if I can make my dockerfile more efficient. -- Thanks!
My Dockerfile:
FROM php:7.4-fpm-alpine
RUN apk update \
&& apk add curl php7-curl php7-json php7-tokenizer php7-mbstring php7-exif php7-fileinfo \
php7-bcmath php7-dom php7-session php7-simplexml php7-ctype
#* PDO EXIF The apk is not working!
# RUN apk add php7-exif
RUN docker-php-ext-install exif
#* EXT-GD The apk is not working!
# RUN apk add php7-gd
RUN apk add libpng libpng-dev && docker-php-ext-install gd && apk del libpng-dev
#* PDO MYSQL The apk is not working!
# RUN apk add php7-pdo php7-pdo_mysql
RUN docker-php-ext-install pdo pdo_mysql
#* EXT-REDIS The apk is not working!
# RUN apk php7-pecl-redis
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
#* Get permissions right
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
The official php Docker Image uses a self compiled version of php which is not compatible with the php included in the Alpine package repository. You have two choices to solve this:
Either use docker-php-ext-install and docker-php-ext-enable for everything and no apk add php* or
Use the Alpine base image with the php from the package repository.
A long build time does not necessarily mean your internet connection is to slow. Compiling exif, gd, pdo_mysql and redis takes it time, too. Check your bandwidth and CPU usage while building the image and then you know for sure where your bottle neck is. To speed things up you can efficiently cache layers by first installing dependencies with apk, then composer, pip, etc. and finally adding your code.

Unable to install composer packages dependent on GD PHP extension in Docker Container

I an trying to create a dockerized network for my application. With my current docker-compose.yml configuration I am able to get everything up and running and working. The issue I am facing is when I am trying to install composer packages dependant on the GD extension. Can anyone tell me what is wrong with either my Dockerfile for PHP or my docker-compose network setup?
PHP Dockerfile
FROM php:7.3-alpine
# Install php extensions
RUN apk update && apk add --no-cache postgresql-dev && apk add libzip-dev freetype-dev libjpeg-turbo-dev libpng-dev
RUN docker-php-ext-install pdo pdo_pgsql mbstring zip
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd
# Setup working directory
WORKDIR /var/www/html
Docker Compose PHP Network Block
php:
build:
context: .
dockerfile: Dockerfile
container_name: ${PROJECT_NAME}-php
volumes:
- ./src:/var/www/html
ports:
- "9001:9000"
networks:
- testNetwork
Thanks in advance!!
Continued: When I shell into the running docker container, you can see the loaded docker-php-extensions.
/usr/local/etc/php/conf.d # ls
docker-php-ext-gd.ini docker-php-ext-pdo_pgsql.ini docker-php-ext-sodium.ini docker-php-ext-zip.ini
/usr/local/etc/php/conf.d #
Easiest way to do so, is this awesome project on github, easily replace:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions gd
with your Dockerfile's php extension installation section.
Edit:
You can also install composer on any docker container as easy as pasting:
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN apt-get install -y openssl zip unzip git libonig-dev libxml2-dev libpng-dev libjpeg-dev libfreetype6-dev libonig-dev curl mcrypt gnupg build-essential software-properties-common wget vim libwebp-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-webp-dir=/usr/include/ --with-jpeg-dir=/usr/include
RUN docker-php-ext-install mysqli mbstring pdo pdo_mysql tokenizer xml bcmath opcache pcntl intl soap exif gd
Try with above code snippet, for gd there are php-ext-configure install and install some lib like libpng-dev
Hope it will helpful for you

Installing GD extension in Docker

I'm new to Docker and I'm trying to install PHP GD extension.
This is my current Dockerfile:
FROM php:7.4-fpm-alpine
RUN docker-php-ext-install mysqli pdo pdo_mysql bcmath gd
When running the docker via docker-compose build && docker-compose up -d I'm getting a lots of errors and in the end I get this message:
configure: error: Package requirements (zlib) were not met:
Package 'zlib', required by 'virtual:world', not found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.
Alternatively, you may set the environment variables ZLIB_CFLAGS
and ZLIB_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
ERROR: Service 'php' failed to build: The command '/bin/sh -c docker-php-ext-install mysqli pdo pdo_mysql bcmath gd' returned a non-zero code: 1
Without the "gd" at the end the Docker container runs normally.
What could be the problem and how could I fix it?
You can try to add these settings in Dockerfile:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd
Official documentation. Hope it's help you.
In my docker using PHP8.0 I got GD working for jpg,png and webp by adding the following lines to my php.dockerfile:
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
One more way to approach the issue is to use install-php-extensions in Dockerfile:
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions gd xdebug
Works in Alpine and PHP8.
Docs here
If someone has problems installing php-gd extension in Docker, look up to #Dmitry comment or Documentation and search for "PHP Core Extensions".
You can see full code on my Github, if you want to see how am I running my NGINX and PHP with php-gd extension.

Docker php:fpm—install php extensions

I am using the official php:fpm docker image as base for my application container, so the Dockerfile starts like so:
FROM php:fpm
Later in the file I would like to have something like that:
RUN apt-get install -y \
php7.0-gd
But that tells me:
E: Unable to locate package php7.0-gd
E: Couldn't find any package by regex 'php7.0-gd'
»Bashing« into the container using docker exec -it <name> /bin/bash and executing: apt-cache search php | grep gd yields:
php5-gdcm - Grassroots DICOM PHP5 bindings
php5-vtkgdcm - Grassroots DICOM VTK PHP bindings
php5-gd - GD module for php5
So since that is a debian (yessie) based image, only the old php5 packages are available and php7 is installed by some tricky script in the php:fpm dockerfile and it seams that all extensions are compiled within the used php executable.
How can I install more extensions in this scenario?
Quoting https://hub.docker.com/_/php/
How to install more PHP extensions
We provide the helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.
To install PHP with iconv, mcrypt and GD, they provide the following example:
FROM php:7.0-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
Please refer to the Dockerhub page for more details.

Categories