Search code examples
phplaraveldockeroci8laravel-sail

Laravel Sail - How to get oci8 extension working for non-root (sail) user?


Is there someone knowledgeable about docker around?

I'm basically trying to install the oci8 extension inside a docker image (based on ubuntu) and failing at the last step. I've managed to compile the extension but for some reason it's only available to the root user.

oci8 extension not working for sail user

oci8 extension working for root user

My original steps were

...
RUN pecl channel-update pecl.php.net
RUN echo 'instantclient,/usr/local/instantclient_21_11' | pecl install oci8-2.2.0
RUN echo "extension=oci8.so" >> /etc/php/7.4/cli/conf.d/30-oci8.ini
...

I've tried changing the last one to

RUN echo '; priority=30' >> /etc/php/7.4/mods-available/oci8.ini \
    && echo 'extension=oci8.so' >> /etc/php/7.4/mods-available/oci8.ini \
    && ln -s /etc/php/7.4/mods-available/oci8.ini /etc/php/7.4/cli/conf.d/30-oci8.ini

And it still doesn't work. The error I get when I'm NOT a root user is

PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/lib/php/20190902/oci8.so (libnnz21.so: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/oci8.so.so (/usr/lib/php/20190902/oci8.so.so: cannot open shared object file: No such file or directory))

Doing ldd /usr/lib/php/20190902/oci8.so outputs the following

sail@:/var/www/html$ ldd /usr/lib/php/20190902/oci8.so
    linux-vdso.so.1 (0x00007fff43cd0000)
    libclntsh.so.21.1 => /usr/local/instantclient/libclntsh.so.21.1 (0x00007f53320c4000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5331ec4000)
    libnnz21.so => /usr/local/instantclient/libnnz21.so (0x00007f533184c000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5331844000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f53316f4000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f53316cc000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f53316bc000)
    libaio.so.1 => /lib/x86_64-linux-gnu/libaio.so.1 (0x00007f53316b4000)
    libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f5331694000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f53364ec000)
    libclntshcore.so.21.1 => /usr/local/instantclient/libclntshcore.so.21.1 (0x00007f53310e4000)

I thought there was a problem with libnnz21.so since it appears in the error but the file exists and is in the correct directory. libnnz21.so => /usr/local/instantclient/libnnz21.so

ls -lah /usr/local/instantclient/lib*

This is my full dockerfile, edited from the one published with php artisan sail:publish

FROM ubuntu:20.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP
ARG NODE_VERSION=16
ARG POSTGRES_VERSION=13

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils \
    && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /usr/share/keyrings/ppa_ondrej_php.gpg > /dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php7.4-cli php7.4-dev \
       php7.4-pgsql php7.4-sqlite3 php7.4-gd \
       php7.4-curl php7.4-memcached \
       php7.4-imap php7.4-mysql php7.4-mbstring \
       php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap \
       php7.4-intl php7.4-readline php7.4-pcov \
       php7.4-msgpack php7.4-igbinary php7.4-ldap \
       php7.4-redis php7.4-xdebug \
    && php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sLS https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt focal-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client-$POSTGRES_VERSION \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

### Override: PHP oci8 extension
# Install Oracle Client
RUN apt-get update \
    && apt-get install -y libaio1 libaio-dev unzip wget --no-install-recommends \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-basic-linux.x64-21.11.0.0.0dbru.zip \
    && mkdir -p /opt/oracle \
    && cp instantclient-* /opt/oracle/ \
    && cd /opt/oracle/ \
    && unzip instantclient-basic-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && unzip instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && unzip instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && ln -s /usr/local/instantclient_21_11 /usr/local/instantclient \
    && ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus  

# Install php7 oci8 extension
ENV LD_LIBRARY_PATH=/usr/local/instantclient
ENV ORACLE_HOME=/usr/local/instantclient

RUN pecl channel-update pecl.php.net \
    && echo 'instantclient,/usr/local/instantclient_21_11' | pecl install oci8-2.2.0

RUN echo '; priority=30' >> /etc/php/7.4/mods-available/oci8.ini \
    && echo 'extension=oci8.so' >> /etc/php/7.4/mods-available/oci8.ini \
    && ln -s /etc/php/7.4/mods-available/oci8.ini /etc/php/7.4/cli/conf.d/30-oci8.ini \
    && php -m

# Clean up
RUN apt-get -y remove wget unzip \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf instantclient-basic-linux.x64-21.11.0.0.0dbru.zip \
    && rm -rf instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip \
    && rm -rf instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip
### End Override

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php7.4

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/7.4/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

EDIT: Here is the full working Dockerfile after the problem was solved. There might still be superfluous things in it but it's working.

FROM ubuntu:20.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP
ARG NODE_VERSION=16
ARG POSTGRES_VERSION=13

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 dnsutils \
    && curl -sS 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c' | gpg --dearmor | tee /usr/share/keyrings/ppa_ondrej_php.gpg > /dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php7.4-cli php7.4-dev \
       php7.4-pgsql php7.4-sqlite3 php7.4-gd \
       php7.4-curl php7.4-memcached \
       php7.4-imap php7.4-mysql php7.4-mbstring \
       php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap \
       php7.4-intl php7.4-readline php7.4-pcov \
       php7.4-msgpack php7.4-igbinary php7.4-ldap \
       php7.4-redis php7.4-xdebug \
    && php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sLS https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \
    && echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt focal-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client-$POSTGRES_VERSION \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

### Override: PHP oci8 extension
# Install Oracle Client
RUN apt-get update \
    && apt-get install -y libaio1 libaio-dev unzip wget --no-install-recommends \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip \
    && wget https://download.oracle.com/otn_software/linux/instantclient/2111000/instantclient-basic-linux.x64-21.11.0.0.0dbru.zip \
    && mkdir -p /opt/oracle \
    && cp instantclient-* /opt/oracle/ \
    && cd /opt/oracle/ \
    && unzip instantclient-basic-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && unzip instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && unzip instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip -d /usr/local/ \
    && ln -s /usr/local/instantclient_21_11 /usr/local/instantclient \
    && ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus \
    && echo /usr/local/instantclient_21_11 > /etc/ld.so.conf.d/oracle-instantclient.conf \
    && ldconfig

# Install php7 oci8 extension
RUN pecl channel-update pecl.php.net \
    && echo 'instantclient,/usr/local/instantclient_21_11' | pecl install oci8-2.2.0

RUN echo '; priority=30' >> /etc/php/7.4/mods-available/oci8.ini \
    && echo 'extension=oci8.so' >> /etc/php/7.4/mods-available/oci8.ini \
    && ln -s /etc/php/7.4/mods-available/oci8.ini /etc/php/7.4/cli/conf.d/30-oci8.ini \
    && php -m

# Clean up
RUN apt-get -y remove wget unzip \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf instantclient-basic-linux.x64-21.11.0.0.0dbru.zip \
    && rm -rf instantclient-sdk-linux.x64-21.11.0.0.0dbru.zip \
    && rm -rf instantclient-sqlplus-linux.x64-21.11.0.0.0dbru.zip
### End Override

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php7.4

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/7.4/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

Solution

  • Follow the Instant Client installation instructions and run something like:

    sudo sh -c "echo /usr/local/instantclient_21_11 > \
          /etc/ld.so.conf.d/oracle-instantclient.conf"
    sudo ldconfig
    

    Also:

    • Setting LD_LIBRARY_PATH is generally problematic with Apache because it needs to be set at the right place: and the shell generally isn't right. Using ldconfig solves this.
    • Never set ORACLE_HOME with Instant Client
    • You don't need SQL*Plus for PHP (but I respect you might have it for some other reason)
    • Some of the info in Docker for Oracle Database Applications in Node.js and Python will apply to PHP.