PHP bcmath extension doesn't get installed in Dockerfile - php

I have run into a strange problem where bcmath PHP extension doesn't get installed in Dockerfile but later if I go into my container then I can install it manually. But the problem is if I restart the container all changes are lost.
My Dockerfile
FROM php:7.2-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install bcmath
EXPOSE 9000
CMD ["php-fpm"]
By the output of php -m I see that bcmath extension did not get installed.
But I can install it manually like this
$ docker-compose exec php bash
$ docker-php-ext-install bcmath
$ kill -s USR2 1
Now bcmath extension is installed and works. But when I restart the container it's gone again. Why didn't it install it from Dockerfile I don't understand... What am I doing wrong?

I finally found the problem so I'll share with others. I removed all containers and all images and re-built everything and now everything installed correctly. So the solution was to rebuild the PHP image.

Related

GD library for Docker image with php:7.1-fpm

I have a Docker container
FROM php:7.1-fpm
RUN apt-get update \
&& apt-get install -y \
&& docker-php-ext-install mysqli pdo_mysql
WORKDIR /var/www
CMD ["php-fpm"]
COPY nginx/www /var/www
COPY php/php /usr/local/etc/php
I tried to add some various code for installing GD library but all they didn't work.
I tried to add this
# GD LIB
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) iconv mcrypt zip pdo gd bcmath
I want to add GD library for PHP. Help me, please.
FROM php:7.1-fpm
RUN apt-get update \
&& apt-get install -y \
&& docker-php-ext-install mysqli pdo_mysql
WORKDIR /var/www
RUN apt-get update && apt-get install -y libpng-dev
RUN apt-get install -y \
libwebp-dev \
libjpeg62-turbo-dev \
libpng-dev libxpm-dev \
libfreetype6-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
CMD ["php-fpm"]
Try this code this working after build image run container and check container logs. show below result.
[15-May-2019 11:46:34] NOTICE: fpm is running, pid 1
[15-May-2019 11:46:34] NOTICE: ready to handle connections
Also, add nginx installation code.

Class ZipArchive not found using Docker [duplicate]

This question already has answers here:
Fatal error: Class 'ZipArchive' not found in
(25 answers)
Closed 3 months ago.
I have the following error on my docker machine/instance:
Class 'ZipArchive' not found
Here is my Dockerfile:
FROM php:7.2-alpine
RUN docker-php-ext-install sockets pdo_mysql
RUN docker-php-ext-install -j$(nproc) \
zip
ADD consumer /opt/craft/app/
ADD app.tar.gz /opt/craft/app
CMD /opt/craft/app/consumer
When I sh in to the container via docker-run, I can do php -m:
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sockets
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
zip is not there. Is it supposed to be there? I have also tried doing:
/opt/craft/app # apk add zip
OK: 17 MiB in 29 packages
/opt/craft/app # php -m
But zip still isn't available and I still get the same output error ziparchive not found.
I'm quite new to docker and installing php modules by myself.
How do I get ZipArchive class installed? (Ideally through dockerfile).
Actually when I faced Laravel excel problem on php-fpm 7.4 on docker, I find your question. And this answer
RUN apk update \
&& apk upgrade \
&& apk add zlib-dev \
&& docker-php-ext-configure zip --with-zlib-dir=/usr \
&& docker-php-ext-install zip
don't working forme
I think this Dockerfile works fine for you
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
RUN apt-get update && \
apt-get install -y \
libzip-dev \
&& docker-php-ext-install zip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
# Set working directory
WORKDIR /var/www
USER $user
try replace
RUN docker-php-ext-install -j$(nproc) \
zip
with
RUN apk update \
&& apk upgrade \
&& apk add zlib-dev \
&& docker-php-ext-configure zip --with-zlib-dir=/usr \
&& docker-php-ext-install zip
in Dockerfile
I tried it and it worked.

Troubles with Docker + PHP7 + GD resulting in "Call to undefined function imagecreatefromjpeg()"

I'm having troubles when trying to create an image using imagecreatefromjpeg using this Dockerfile to generate the container:
FROM php:7.1-apache
RUN apt-get update && \
apt-get install -y -qq git \
libjpeg62-turbo-dev \
apt-transport-https \
libfreetype6-dev \
libmcrypt-dev \
libpng12-dev \
libssl-dev \
zip unzip \
nodejs \
npm \
wget \
vim
RUN pecl install redis && docker-php-ext-enable redis
RUN docker-php-ext-install -j$(nproc) iconv mcrypt zip pdo pdo_mysql gd bcmath
COPY ./containers/yii.conf /etc/apache2/sites-available/000-default.conf
RUN for mod in rewrite headers; do a2enmod $mod; done && service apache2 restart
WORKDIR /var/www/html/
GD was correctly installed (libjpeg too - both appearing in php -i and phpinfo()) but imagecreatefromjpeg does not works and I don't know why.
I also ran apt install libjpeg-dev libpng-dev libfreetype6-dev trying to ~force~ reinstallation (or reconfiguration) but seems doesn't succeded (yes, I also restart the container).
root#e8db647c96c4:/var/www/html# php -i | grep -i GD
/usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
gd.jpeg_ignore_warning => 1 => 1
root#e8db647c96c4:/var/www/html#
root#e8db647c96c4:/var/www/html# docker-php-ext-enable gd
warning: gd (gd.so) is already loaded!
root#e8db647c96c4:/var/www/html#
I've tried apt install libgd2-xpm-dev* and apparently it doesn't solves the problem.
Solved
I was missing to put
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
into my Dockerfile.
Full revised Dockerfile:
FROM php:7.1-apache
RUN apt-get update && \
apt-get install -y -qq git \
libjpeg62-turbo-dev \
apt-transport-https \
libfreetype6-dev \
libmcrypt-dev \
libpng12-dev \
libssl-dev \
zip unzip \
nodejs \
npm \
wget \
vim
RUN pecl install redis && docker-php-ext-enable redis
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) iconv mcrypt zip pdo pdo_mysql gd bcmath
COPY ./containers/yii.conf /etc/apache2/sites-available/000-default.conf
RUN for mod in rewrite headers; do a2enmod $mod; done && service apache2 restart
WORKDIR /var/www/html/
PHP 7.4 (Alpine)
If anybody is struggling to enable JPEG support in GD with PHP 7.4, here's what I had to do in order to be able to use imagecreatefromjpeg() function.
My example is based on Alpine 3.10, if you're using other distribution adjust it to your needs.
First install dependencies, in my case beside JPEG I need support for PNG files.
apk add jpeg-dev libpng-dev
After that we can run docker-php-ext-configure command to configure our gd with JPEG support. Notice that flag --with-jpeg-dir was changed to --with-jpeg and we don't need to provide flag to enable PNG. More you can read in PHP 7.4 Changelog in GD section.
docker-php-ext-configure gd --with-jpeg
Directly after that let's run docker-php-ext-install to install GD itself.
docker-php-ext-install -j$(nproc) gd
FULL EXAMPLE
FROM php:7.4-fpm-alpine3.10
RUN apk add jpeg-dev libpng-dev \
&& docker-php-ext-configure gd --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd
For PHP 5.6
FROM php:5.6-apache
RUN apt-get update && apt-get install -y \
libfreetype6-dev libjpeg62-turbo-dev \
libgd-dev libpng12-dev
RUN docker-php-ext-configure gd \
--with-freetype-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd
If still not working, can re-install the container.
docker rm <container id>
docker-compose build --pull
docker-compose up
Updated Version PHP 7.4 + Apache:
Dockerfile
FROM php:7.4-apache
RUN apt-get update -y && apt-get install -y sendmail libpng-dev libfreetype6-dev libjpeg62-turbo-dev libgd-dev libpng-dev
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-configure gd \
--with-freetype=/usr/include/ \
--with-jpeg=/usr/include/
RUN docker-php-ext-install gd
...
Add these Commands
docker-php-ext-configure gd --with-freetype --with-jpeg
docker-php-ext-install -j$(nproc) gd
Working full Dockerfile:
FROM php:7.4-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /src/
# Set working directory
WORKDIR /src
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg-dev \
libwebp-dev \
libxpm-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql exif pcntl
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer install --prefer-source --no-interaction
COPY . /src
RUN chmod 777 -R /src/storage
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

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.

Setting up docker on Mac OS - php-intl cannot be installed

I have the following problem:
I want set up a development environment on my Mac using docker and boot2docker.
For that I created an image using the below Dockerfile.
The Problem is, that my php-intl-Extension cannot be installed.. I tried various method. For example to enable it in php.ini via:
extension=php_intl.so
None of the methods will work. Here is the content of my Dockerfile:
FROM php:5.5-apache
# use own php.ini file
COPY config/php.ini /usr/local/etc/php/
# get some php-extensions
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libicu52 \
libicu-dev \
zlib1g-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
RUN apt-get update && apt-get install -y libmemcached-dev \
&& pecl install memcached \
&& docker-php-ext-enable memcached
# Install intl (not working yet):
RUN pecl install intl
# use own src file
COPY src/ /var/www/html/
Can anyone help? That would be so great, because this problem takes a lot of time by now. Thanks in advance!!!
You don't need to use PECL, why don't you just use:
docker-php-ext-install intl
Generally you also need to apt-get install libicu-dev for dependencies, but you already have that.

Categories