Docker php—install php extensions ssh2 - php

I am using the official php docker image as base for my application container, so the Dockerfile starts like so:
FROM php:5.6-fpm-jessie
Later in the file I would like to have something like that:
RUN apt-get update \
&& apt-get install -y libssh2-1-dev libssh2-1 \
&& docker-php-ext-install ssh2
But that tells me:
/usr/src/php/ext/ssh2 does not exist
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?

In case you are using PHP >=8.0 image:
FROM php:8.0.2-fpm
RUN apt-get install -y libssh2-1-dev libssh2-1 \
&& pecl install ssh2-1.3.1 \
&& docker-php-ext-enable ssh2

For Alpine and php > 7
RUN apk add --no-cache libssh2-dev autoconf build-base
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2

Finally, this is successful.
&& apt-get install -y libssh2-1-dev libssh2-1 \
&& pecl install ssh2 \
&& docker-php-ext-enable ssh2

It looks like the pecl option isn't available for anything newer then PHP 6. In order to get it installed, I had to build from source. From php:7.1.27-cli I was able to get it setup with these commands - the php:7.1.27-cli image is based on debian stretch:
# install dependencies
RUN apt-get update && apt-get install -y \
ssh \
libssh2-1 \
libssh2-1-dev \
wget \
libssl-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install ssh2 extension
RUN wget -O libssh2.tar.gz https://www.libssh2.org/download/libssh2-1.8.1.tar.gz \
&& wget -O ssh2.tgz https://pecl.php.net/get/ssh2-1.1.2.tgz \
&& mkdir libssh2 && tar vxzf libssh2.tar.gz -C libssh2 --strip-components 1 \
&& mkdir ssh2 && tar vxzf ssh2.tgz -C ssh2 --strip-components 1 \
&& cd libssh2 && ./configure \
&& make && make install \
&& cd ../ssh2 && phpize && ./configure --with-ssh2 \
&& make && make install \
&& echo "extension=ssh2.so" >> /usr/local/etc/php/conf.d/ssh2.ini \
&& cd ../ && rm -rf libssh2.tar.gz ssh2.tgz ssh2 libssh2

The solution that worked for me on the CLI image of PHP 7.2:
FROM php:7.2-cli
RUN apt-get install -y libssh2-1 libssh2-1-dev
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2
Build the Docker image and confirm the SSH2 extension is installed:
docker build -t example/php-build .
docker run --rm example/php-build php --ri ssh2
ssh2
SSH2 support => enabled
extension version => 1.2
libssh2 version => 1.8.0
banner => SSH-2.0-libssh2_1.8.0

The easiest solution is to use install-php-extensions.
It's a script that can install the required system libraries automatically, on any Docker PHP image.
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions ssh2
install-php-extensions can install a ton of other PHP extensions as well, not only ssh2: more details at https://github.com/mlocati/docker-php-extension-installer

For someone looking for a newer version of this for php 7.4 you'll need the following:
install dependencies
RUN apt-get update && apt-get install -y \
ssh \
libssh2-1 \
libssh2-1-dev \
wget \
libssl-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install ssh2 extension
RUN wget -O libssh2.tar.gz https://www.libssh2.org/download/libssh2-1.9.0.tar.gz \
&& wget -O ssh2.tgz https://pecl.php.net/get/ssh2-1.2.tgz \
&& mkdir libssh2 && tar vxzf libssh2.tar.gz -C libssh2 --strip-components 1 \
&& mkdir ssh2 && tar vxzf ssh2.tgz -C ssh2 --strip-components 1 \
&& cd libssh2 && ./configure \
&& make && make install \
&& cd ../ssh2 && phpize && ./configure --with-ssh2 \
&& make && make install \
&& echo "extension=ssh2.so" >> /usr/local/etc/php/conf.d/ssh2.ini \
&& cd ../ && rm -rf libssh2.tar.gz ssh2.tgz ssh2 libssh2
The links posted here will only work up to php 7.2.

I had some error on an other solutions
In my running container on richarvey/nginx-php-fpm image worked with two command.
apk add --no-cache libssh2-dev autoconf build-base
pecl install https://pecl.php.net/get/ssh2-1.3.tgz
docker-php-ext-enable ssh2

Working solution with PHP 7.4:
FROM php:7.4-fpm
RUN apt-get install -y libssh2-1 libssh2-1-dev
RUN pecl install ssh2-1.2 && docker-php-ext-enable ssh2

Related

Can't install pdo_mysql with docker-php-ext-install

I need help, I can’t install pdo_mysql, ampq or opcache with docker-php-ext-install. I prepared a docker project on a local PC (Mac OSX). On the local PC after build docker image is the final PHP configuration right. When I deploy the project config to the remote Debian server after building it, I have a completely different PHP version (phpinfo). Missing extensions opcache, pdo_mysql, ampq.
I tried to configure it differently and deleted the docker cache on the server, but it didn’t help.
docker system prune -a
My PHP dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/symfony
RUN apt-get update \
&& docker-php-ext-install mysqli pdo pdo_mysql \
&& docker-php-ext-enable pdo_mysql
RUN apt update && apt-get udpate \
&& apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip libxslt1-dev\
&& docker-php-ext-install intl \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip \
&& docker-php-ext-install xsl
RUN apt-get update \
&& docker-php-ext-install opcache
RUN apt-get update \
&& apt-get install -y \
librabbitmq-dev \
libssh-dev \
&& pecl install amqp \
&& docker-php-ext-enable amqp
RUN apt-get update && \
apt-get install -y libxml2-dev \
&& docker-php-ext-install soap
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
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Node.js
RUN curl -sL https://deb.nodesource.com/setup_14.x -o node_setup.sh && \
bash node_setup.sh && \
apt-get install -y nodejs && \
npm install npm -g
# Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && \
apt-get install -y yarn
RUN apt-get update && apt-get install -y cron
ADD ./crontab /etc/cron.d/crontab
#
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/crontab
RUN crontab /etc/cron.d/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
RUN apt-get update && apt-get install -y supervisor
ADD ./supervisord.conf /etc/supervisor/
# Copy docker entrypoint file
COPY ./entrypoint.sh /entrypoint.sh
# Docker init
RUN ["chmod", "+x", "/entrypoint.sh"]
Not sure if it's a solution to your issue, but let's see.
From https://hub.docker.com/_/php:
If you are having difficulty figuring out which Debian or Alpine packages need to be installed before docker-php-ext-install, then have a look at the install-php-extensions project. This script builds upon the docker-php-ext-* scripts and simplifies the installation of PHP extensions by automatically adding and removing Debian (apt) and Alpine (apk) packages. For example, to install the GD extension you simply have to run install-php-extensions gd. This tool is contributed by community members and is not included in the images, please refer to their Git repository for installation, usage, and issues.
I had similar issues in the past and resolved it by using install-php-extensions. See this comment.

Install php imap on docker container through dockerfile

I'm trying to modify my dockerfile so that it will install php-imap for php on our docker container
I have this:
RUN set -ex && \
apt-get update && \
apt-get install -y php-imap && \
docker-php-ext-install php-imap
but I'm getting an error that it can't find php-imap?
What am I doing wrong here
Try:
RUN apt-get install -y libc-client-dev libkrb5-dev && rm -r /var/lib/apt/lists/* && docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap

Installing ldap extension Docker

Hey all I am trying to instal ldap extension on a Docker php:5.6-fm images I need ldap for my project.
I have tired to install the extension through Dockerfile like so:
RUN apt-get install php5-ldap -y
get this error:
The LDAP PHP extension is not enabled.
I have also found some 'suggestion' online like so:
RUN \
apt-get update && \
apt-get install libldap2-dev -y && \
rm -rf /var/lib/apt/lists/* && \
docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \
docker-php-ext-install ldap
get this error:
An exception occurred in driver: could not find driver
am I doing something wrong...? How do I install ldap in a docker image so that I can use it in my project...?
Here is what I did... and it's working. I had other modules configured, but I don't think they interfere with anything.
FROM php:5.6-apache
RUN apt-get update -y \
&& apt-get install -y libcurl4-openssl-dev pkg-config libssl-dev vim
# Configure MongoDB.
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
# Configure MySQL.
RUN docker-php-ext-install mysqli
# Configure LDAP.
RUN apt-get update \
&& apt-get install libldap2-dev -y \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ \
&& docker-php-ext-install ldap
RUN a2enmod rewrite && service apache2 restart
# Python 2.7 and modules
RUN apt install python2.7 python-pip -y \
&& python2.7 -m pip install pymysql pymongo redis
# We must reconfigure here the CMD directive.
CMD apache2-foreground

Docker install PHP-7-fpm with Memcache extention ? fails with Cannot find config.m4

I am trying install Docker with PHP version 7.0 with memcache support, my docker-file is as below, however it fails #STEP 10 with error
Step 10 : RUN /usr/bin/phpize
---> Running in 450678a59cd4
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module
[31mERROR[0m: Service 'php' failed to build: The command '/bin/sh -c /usr/bin/phpize' returned a non-zero code: 1
Docker file is as below
FROM php:7.0-fpm
#FROM php:5.6-fpm
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-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 docker-php-ext-install mysqli mbstring pdo_mysql
# Download and Installing php libraries
RUN apt-get -y install php-pear php5-dev
# Download and Installing git and vim
RUN apt-get -y install git vim gcc
# Download and Installing zip unzip
RUN apt-get -y install zip unzip
# install PHP PEAR extensions
RUN apt-get -y install wget
#RUN wget http://pecl.php.net/get/memcache-3.0.9-dev.tgz && gunzip memcache-3.0.9-dev.tgz && tar -xvf memcache-3.0.9-dev.tar && cd memcache-3.0.9-dev \
# && phpize && ./configure && make && make install
# RUN git clone https://github.com/websupport-sk/pecl-memcache && cd pecl-memcache
RUN apt-get -y install libmemcached-dev libmemcached11
RUN git clone https://github.com/php-memcached-dev/php-memcached && cd php-memcached && git checkout -b php7 origin/php7
RUN /usr/bin/phpize
RUN ./configure && make && make install
RUN apt-get install -y memcached
EXPOSE 9000
COPY ./www.conf /etc/php-fpm.d/www.conf
COPY ./php.ini /etc/php.ini
COPY ./php-fpm.conf /etc/php-fpm.conf
COPY ./40-memcache.ini /etc/php.d/40-memcache.ini
#COPY bootstrap.sh /opt/bootstrap.sh
#RUN chmod +x /opt/bootstrap.sh
#ENTRYPOINT ["/opt/bootstrap.sh"]
Here's how your Dockerfiles should look like:
PHP-FPM Dockerfile:
FROM php:7.0-fpm
# ...
RUN apt-get update && apt-get install -y \
libpq-dev \
libmemcached-dev \
curl
# ...
# Install Memcached for php 7
RUN curl -L -o /tmp/memcached.tar.gz "https://github.com/php-memcached-dev/php-memcached/archive/php7.tar.gz" \
&& mkdir -p /usr/src/php/ext/memcached \
&& tar -C /usr/src/php/ext/memcached -zxvf /tmp/memcached.tar.gz --strip 1 \
&& docker-php-ext-configure memcached \
&& docker-php-ext-install memcached \
&& rm /tmp/memcached.tar.gz
# ...
CMD ["php-fpm"]
EXPOSE 9000
Memcached Dockerfile:
FROM memcached:latest
CMD ["memcached"]
EXPOSE 11211
This is taken from https://github.com/LaraDock/laradock
You need to run that command /usr/bin/phpize in the right folder.
See this Dockerfile as an example
RUN wget https://github.com/phpredis/phpredis/archive/2.2.5.zip; unzip 2.2.5.zip
WORKDIR /tmp/php-redis/phpredis-2.2.5
RUN /usr/bin/phpize; ./configure; make; make install
In your case, you did clone the repo php-memcached and make a cd in it, but that does not change the working directory for the next Dockerfile RUN directive.
Set that working directory before the RUN directive:
WORKDIR /php-memcached
RUN /usr/bin/phpize
This work for me:
FROM php:7.1.1-fpm
RUN apt-get update
RUN docker-php-ext-install mysqli
RUN apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-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 install git -y
RUN git --version
RUN apt-get install -y build-essential libmemcached-dev
RUN git clone https://github.com/php-memcached-dev/php-memcached.git
RUN cd php-memcached \
&& git checkout php7 \
&& phpize \
&& ./configure --disable-memcached-sasl \
&& make \
&& make install

How to install php-redis extension using the official PHP Docker image approach?

I want to build my PHP-FPM image with php-redis extension based on the official PHP Docker image, for example, using this Dockerfile: php:5.6-fpm.
The docs say that I can install extensions this way, installing dependencies for extensions manually:
FROM php:5.6-fpm
# Install modules (iconv, mcrypt and gd extensions)
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd
CMD ["php-fpm"]
Without Docker, I installed it with apt-get install php5-redis. But how can I install it using the approach above?
Redis is not an extension that is included in “php-src”, therefore you cannot use docker-php-ext-install. Use PECL:
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& docker-php-ext-enable redis
On alpine php 7.3.5 we can use:
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
My opinion, the easiest way is:
RUN pecl install redis && docker-php-ext-enable redis
;)
Slightly revised version of starikovs and skyred answers for the current PHP 7 version of the docker image (tested on php:7.0.8-fpm-alpine and php:7.0.8-alpine).
Uses the newly released 3.0 version (June 2016) for PHP 7.
ENV PHPREDIS_VERSION 3.0.0
RUN mkdir -p /usr/src/php/ext/redis \
&& curl -L https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis
I've found two ways to install php-redis extension for official php-fpm Docker image. Here they are:
The first way is to compile redis from sources and install.
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-2.2.7 /usr/src/php/ext/redis \
&& docker-php-ext-install redis
docker-php-ext-install script is included in php-fpm image and can compile extensions and install them.
The second way you can do it is with PECL.
As TimWolla answered, you can do it with PECL, but in my case, PECL isn't installed by default.
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
Based on #starikovs answer. I added a variable for docker style.
# install phpredis extension
ENV PHPREDIS_VERSION 2.2.7
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis
If you want to use redis as session handler;
RUN { \
echo 'session.save_handler = redis'; \
echo 'session.save_path = tcp://redis:6379'; \
} >> /usr/local/etc/php/conf.d/docker-php-ext-redis.ini
If you want to use redis extension with PHP 7 in 2015 (borrowed from skyred's answer);
ENV PHPREDIS_VERSION php7
RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis
This works for alpine images:
RUN set -xe \
&& apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install -o -f redis \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini \
&& rm -rf /usr/share/php \
&& rm -rf /tmp/* \
&& apk del .phpize-deps
Edit: Added missing backslash
Tried few ways. On alpine php 7.3.5 we can use:
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
I'm using combination of PECL and PHP official docker extension script
RUN pecl bundle -d /usr/src/php/ext redis \
&& rm /usr/src/php/ext/redis-*.tgz \
&& docker-php-ext-install redis
For PHP7 you need to wait for official redis pecl release or use git:
RUN apt-get update \
&& apt-get install git -y -q \
&& git clone -b php7 https://github.com/phpredis/phpredis.git /usr/src/php/ext/redis \
&& docker-php-ext-install redis
Slightly revised version of starikovs and skyred answers for current version of the docker image.
Tested on php:5-fpm-alpine
# install phpredis extension
ENV PHPREDIS_VERSION 2.2.8
ADD https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz /tmp/redis.tar.gz
RUN tar xzf /tmp/redis.tar.gz -C /tmp \
&& mkdir -p /usr/src/php/ext \
&& mv /tmp/phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis \
&& rm -rf /usr/src/php/ext/redis
In your Dockerfile you can clone the repo and install it with:
RUN git clone https://github.com/phpredis/phpredis.git /tmp/phpredis \
&& cd /tmp/phpredis \
&& git checkout -b 3.1.2 \ ## or the release you need #
&& phpize \
&& ./configure \
&& make \
&& make install
For image php:7.2-fpm-alpine.
RUN apk add autoconf gcc g++ make && pecl install redis && docker-php-ext-enable redis
You may need to update before
apk --update upgrade

Categories