Compile PHP OCI8 extension under Alpinelinux - php

I'm trying to compile PHP OCI8 extension and run it under Alpinelinux. But it seems extension has some dependencies on symbols defined in glibc but missing in musl libc.
Is there any existent compiled PHP OCI8 extension I can use? Or is there any workaround to make oci8 extension and oracle instant client work with alpinelinux.
Thanks in advance!

My solution for Alpine turned out to be simple: use Instant Client v11 (11.2.0.4) instead of v12 (12.2.0.1):
ENV LD_LIBRARY_PATH /usr/local/instantclient
ENV ORACLE_HOME /usr/local/instantclient
# Install Oracle Client and build OCI8 (Oracle Command Interface 8 - PHP extension)
RUN apk add php7-pear php7-dev gcc musl-dev libnsl libaio &&\
## Download and unarchive Instant Client v11
curl -o /tmp/basic.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-basic-linux.x64-11.2.0.4.0.zip && \
curl -o /tmp/sdk.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-sdk-linux.x64-11.2.0.4.0.zip && \
curl -o /tmp/sqlplus.zip https://raw.githubusercontent.com/bumpx/oracle-instantclient/master/instantclient-sqlplus-linux.x64-11.2.0.4.0.zip && \
unzip -d /usr/local/ /tmp/basic.zip && \
unzip -d /usr/local/ /tmp/sdk.zip && \
unzip -d /usr/local/ /tmp/sqlplus.zip && \
## Links are required for older SDKs
ln -s /usr/local/instantclient_11_2 ${ORACLE_HOME} && \
ln -s ${ORACLE_HOME}/libclntsh.so.* ${ORACLE_HOME}/libclntsh.so && \
ln -s ${ORACLE_HOME}/libocci.so.* ${ORACLE_HOME}/libocci.so && \
ln -s ${ORACLE_HOME}/lib* /usr/lib && \
ln -s ${ORACLE_HOME}/sqlplus /usr/bin/sqlplus &&\
ln -s /usr/lib/libnsl.so.2.0.0 /usr/lib/libnsl.so.1 &&\
## Build OCI8 with PECL
echo "instantclient,${ORACLE_HOME}" | pecl install oci8 &&\
echo 'extension=oci8.so' > /etc/php7/conf.d/30-oci8.ini &&\
# Clean up
apk del php7-pear php7-dev gcc musl-dev &&\
rm -rf /tmp/*.zip /var/cache/apk/* /tmp/pear/

Try these commands.
It works under Docker on Alpine.
But, you have to install the GLIB Alpine compatibility layer: https://github.com/sgerrand/alpine-pkg-glibc
# Install OCI8
COPY ./oracle-sdk /tmp/oracle-sdk
RUN apk add --no-cache libaio-dev && \
unzip /tmp/oracle-sdk/instantclient-basic-linux.x64-12.2.0.1.0.zip -d /usr/local/ && \
unzip /tmp/oracle-sdk/instantclient-sdk-linux.x64-12.2.0.1.0.zip -d /usr/local/ && \
unzip /tmp/oracle-sdk/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip -d /usr/local/ && \
ln -s /usr/local/instantclient_12_2 /usr/local/instantclient && \
ln -s /usr/local/instantclient/libclntsh.so.* /usr/local/instantclient/libclntsh.so && \
ln -s /usr/local/libclntshcore.so.* /usr/local/instantclient/libclntshcore.so && \
ln -s /usr/local/instantclient/libocci.so.* /usr/local/instantclient/libocci.so && \
ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus && \
docker-php-ext-configure oci8 --with-oci8=instantclient,/usr/local/instantclient && \
docker-php-ext-install oci8 && \
rm -rf /tmp/oracle-sdk

People who've tried Alpine haven't found a way. The problem is not specific to PHP, so check other languages that also use Oracle Instant Client and have tried Alpine. For example, see the Alpine issue https://github.com/sgerrand/alpine-pkg-glibc/issues/31

FROM php:alpine3.13
RUN apk add --no-cache composer libaio libc6-compat \
&& wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-basiclite-linux.x64-21.1.0.0.0.zip -O- | busybox unzip -q - \
&& wget https://download.oracle.com/otn_software/linux/instantclient/211000/instantclient-sdk-linux.x64-21.1.0.0.0.zip -O- | busybox unzip -q - \
&& cp /instantclient_21_1/libclntsh.so.21.1 /instantclient_21_1/libclntsh.so && cp /lib64/ld-linux-x86-64.so.2 /instantclient_21_1/ && cp /lib/libc.so.6 /usr/lib/libresolv.so.2 \
&& docker-php-ext-configure oci8 --with-oci8=instantclient,/instantclient_21_1 && docker-php-ext-install oci8

Related

oci8-2.2.0 does not connect to the database but doesn't return an error either

I'm trying to install oci8-2.2.0 on an ubuntu 18 docker with php 7.2
I'm using the following commands (php-pear and libc6 installed before those):
RUN apt-get update && \
apt-get install -y unzip libaio1 libsnl-dev && \
cd /tmp && \
wget https://download.oracle.com/otn_software/linux/instantclient/214000/instantclient-sdk-linux.x64-21.4.0.0.0dbru.zip && \
wget https://download.oracle.com/otn_software/linux/instantclient/214000/instantclient-sqlplus-linux.x64-21.4.0.0.0dbru.zip && \
wget https://download.oracle.com/otn_software/linux/instantclient/214000/instantclient-basic-linux.x64-21.4.0.0.0dbru.zip && \
mkdir -p /opt/oracle && \
cp instantclient-* /opt/oracle/ && \
cd /opt/oracle/ && \
unzip instantclient-basic-linux.x64-21.4.0.0.0dbru.zip && \
unzip instantclient-sdk-linux.x64-21.4.0.0.0dbru.zip && \
unzip instantclient-sqlplus-linux.x64-21.4.0.0.0dbru.zip && \
rm -rf /var/lib/apt/lists/* instantclient-basic-linux.x64-21.4.0.0.0dbru.zip instantclient-sdk-linux.x64-21.4.0.0.0dbru.zip instantclient-sqlplus-linux.x64-21.4.0.0.0dbru.zip && \
ln -s /opt/oracle/instantclient_21_4/libclntsh.so.21.1 /usr/lib/libclntsh.so && \
ln -s /lib64/ld-linux-x86-64.so.2 /usr/lib/ld-linux-x86-64.so.2 && \
ln -s /opt/oracle/instantclient_21_4/libnnz21.so /usr/lib/libnnz21.so && \
ln -s /opt/oracle/instantclient_21_4/libclntshcore.so /usr/lib/libclntshcore.so.21.1 && \
echo /opt/oracle/instantclient_21_4 > /etc/ld.so.conf.d/oracle-instantclient.conf
RUN echo "instantclient,/opt/oracle/instantclient_21_4" | pecl install oci8-2.2.0 && \
echo "extension=oci8.so" >> /etc/php/7.2/cli/php.ini && \
echo "extension=oci8.so" >> /etc/php/7.2/apache2/php.ini
PHP Seems to recognize the module and load it, but when I call #oci_new_connect I don't get a connection, but I don't get an error either.
Using sqlplus from the same server connects just fine, so its not that. Another installation with the adminer 4.8.1 docker (alpine & php 7.4) works fine.
I'm completely stumped. Does anyone have an idea where to look next?
Found the issue, needed to add
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/oracle/instantclient_21_4

executor failed running /bin/sh -c docker-php-ext-install

I am trying upgrade this docker file from 7.3 to 7.4 but getting executor failed error.
Detailed error:
executor failed running [/bin/sh -c docker-php-ext-install bcmath
ctype dom gd hash iconv intl mbstring
mysqli opcache pdo_mysql simplexml sockets soap
sodium xsl zip ;]: exit code: 2
FROM php:7.4-fpm as base
ENV COMPOSER_HOME=/tmp/composer
ENV APCU_VERSION=5.1.18
RUN apt-get update && apt-get install -y --no-install-recommends gnupg \
netcat \
sudo \
libicu-dev \
libfreetype6-dev \
libjpeg-dev \
libpng-dev \
libsodium-dev \
libxml2-dev \
libxslt-dev \
libzip-dev \
rsync \
unzip \
git \
openssh-client \
;
RUN pecl install apcu-${APCU_VERSION}
RUN docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install \
bcmath \
ctype \
dom \
gd \
hash \
iconv \
intl \
mbstring \
mysqli \
opcache \
pdo_mysql \
simplexml \
sockets \
soap \
sodium \
xsl \
zip \
;
RUN docker-php-ext-enable apcu
RUN echo "memory_limit=1G" >> /usr/local/etc/php/conf.d/zz-memory-limit-php.ini
RUN echo "apc.enable=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "apc.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-apcu.ini
RUN echo "opcache.memory_consumption=512MB" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.max_accelerated_files=60000" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.consistency_checks=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.validate_timestamps=0" >> /usr/local/etc/php/conf.d/zz-opcache.conf
RUN echo "opcache.enable_cli=1" >> /usr/local/etc/php/conf.d/zz-opcache.conf
FROM base as build
RUN curl https://files.magerun.net/n98-magerun2.phar -o /usr/local/bin/magerun \
&& chmod 755 /usr/local/bin/magerun
RUN mkdir -p -m 0775 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN mkdir -p -m 0775 ~/.ssh && ssh-keyscan github.com >> /var/www/.ssh
# USER www-data
WORKDIR /var/www/html
ARG COMPOSER_AUTH
COPY --from=composer:1 /usr/bin/composer /usr/bin/composer
RUN composer global require hirak/prestissimo
COPY auth.json auth.json
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN --mount=type=ssh,id=id_rsa php -d memory_limit=2G $(which composer) install --no-progress --no-dev
COPY app/etc/config.php app/etc/config.php
COPY bin bin
FROM build as app
ENV MAGE_MODE=production
RUN php -d memory_limit=2G bin/magento setup:di:compile
RUN composer dump-autoload --optimize --apcu
RUN php -d memory_limit=2G bin/magento setup:static-content:deploy -f
RUN rm -rf /var/www/html/var/cache
RUN rm -rf /var/www/html/var/page_cache
RUN rm -rf /var/www/html/var/session
COPY --chown=www-data app/etc/env.docker.php app/etc/env.php
i had a similar issue today.
Seems that in php7.4 iconv his a native library.
So you should try to remove it from your Dockerfile and see if it works.
Even if it's an 6 month old issue, i hope it could help someone.
Chears.

Docker php—install php extensions ssh2

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

Docker with wkhtmltopdf is not work correctly

I am trying to use wkhtmltopdf in a container with Docker and Symfony.
The installation is done well, but the result is very different from the reality, so I think some libraries must be missing in the system, but I don't know which one.
I've been checking other questions here and in google, but they don't help.
FROM phpdockerio/php71-fpm:latest
# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive
# Install selected extensions and other stuff
RUN apt-get update \
&& apt-get -y --no-install-recommends install cron \
php7.1-mysql \
php-redis \
php7.1-bcmath \
php7.1-gd \
php-imagick \
php7.1-intl \
cron \
python \
xz-utils \
libxrender1 \
libfontconfig1 \
zlib1g \
fontconfig \
libfreetype6 \
libx11-6 \
libxext6 \
wkhtmltopdf
RUN curl --insecure https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer
RUN cd /tmp \
&& curl "https://downloads.wkhtmltopdf.org/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz" -L -o "wkhtmltopdf.tar.xz" \
&& tar Jxvf wkhtmltopdf.tar.xz \
&& rm wkhtmltopdf.tar.xz \
&& mv wkhtmltox/include/* /usr/local/include/ \
&& mv wkhtmltox/lib/* /usr/local/lib/ \
&& mv wkhtmltox/bin/* /usr/local/bin/ \
&& mv wkhtmltox/share/* /usr/local/share/
RUN apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
CMD php-fpm -F
This is the result in the container(docker)
And this is the result on my machine (without docker), as it should be.
Any idea what might be happening?

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