Install PHP 7 extensions in docker container - php

I've created a docker container using https://hub.docker.com/_/php/.
I need to connect from this container to another container running postgres (https://hub.docker.com/_/postgres/). However, I can't seem to get the PHP 7 PDO drivers for postgres to install properly.
Here's what my Dockerfile looks like:
FROM php:7.0-cli
COPY ./app /app
WORKDIR /app
RUN apt-get update && apt-get install -y \
postgresql-client \
&& docker-php-ext-install -j$(nproc) pgsql
CMD [ "php", "./do_stuff.php" ]
in place of pgsql I've tried pdo-pgsql, php7.0-pgsql, and tried adding a line to add the ppa:ondrej/php repo to pull from with no luck.
Most of the packages fail to install (even pdo-pgsql which was suggested by docker after failing to find php7.0-pgsql), but the odd one that does install still throws an error that the drivers aren't found when trying to create a PDO connection.
Is there a right way to get the PDO drivers for postgres installed in a docker container?

Ensure the extension is symlinked in /etc/php/7/cli/conf.d or /etc/php/7/fpm/conf.d - folders depend on your distribution.
You can find out if the module is loaded by running
php -m
php7-fpm -m
if the module is missing, check /etc/php/7/modules if the .ini file is present there and symlink it, then the module should get loaded.

Related

How to install extensions using docker-php-extension-installer

I'm new to docker.
I would like to use docker-php-extension-installer for installing PHP extensions to my container, because according to php-docker hub docs 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.
The problem is when I try to install gd or pdo_mysql or something else, extensions are installed but not shown in the modules list when I run php -m command in my bash.
Here is my DockerFile
FROM php:7.3-fpm
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN apt-get update && apt-get install -y unzip \
&& apt-get install -y git nano \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# use https://github.com/mlocati/docker-php-extension-installer to simply install php extensions with dependencies
ADD https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions /usr/local/bin/
RUN chmod uga+x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions gd pdo_mysql
Maybe I have to enable these extensions somehow, but how can I do it from my Dockerfile? There is nothing said about this neither in docker-php-extension-installer page docs, no in official php docker page.
If anybody knows, please help. Thanks.
The above-mentioned Dockerfile is used as part of docker-compose. When I created a docker-compose.yml file, I made a mistake. To change default php configuration, I mounted host folded, where I keep my custom php.ini, and mapped it to conf.d folder in php container. Like this:
# PHP Service
php:
build: ./.docker/php
volumes:
- ./.docker/php/conf.d:/usr/local/etc/php/conf.d:ro
depends_on:
- mysql
But the conf.d folder keeps the configuration of all installed extensions, for example:
docker-php-ext-gd.ini docker-php-ext-pdo_mysql.ini docker-php-ext-sodium.ini docker-php-ext-xdebug.ini
So the contents of my host folder replaced the original conf.d contents and as a result - extensions didn't work.

Docker & Laravel : configure: error: Package requirements (oniguruma) were not met

Can anyone help me with this problem.
When i try to create a docker image from a dockerfile for laravel application i get this error:
checking for oniguruma... no
configure: error: Package requirements (oniguruma) were not met:
No package 'oniguruma' 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 ONIG_CFLAGS
and ONIG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
The command '/bin/sh -c docker-php-ext-install pdo mbstring' returned a non-zero code: 1
Here is my Dockerfile:
FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring
WORKDIR /app
COPY app /app # this copies all the app files to a folder called `app`
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
and the docker command to build the Dockerfile
sudo docker build -t test .
What #kalatabe said is correct. But in case you absolutely wanted to make sure mbstring gets installed, you can also add libonig-dev to your apt-get install
Just remove mbstring from the docker-php-ext-install instruction.
The error is caused by a dependency problem - the mbstring extension requires the oniguruma library to make multibyte regular expression functions work. From the installation guide:
Oniguruma is necessary for the regular expression functions with multibyte character support. Oniguruma is bundled with mbstring. As of PHP 5.4.0, if Oniguruma is already installed on the system, --with-onig[=DIR] can be specified to use the installed library.
However, in the image that you're using, the extension is already installed and configured, so you don't need to do anything else:
$> docker run --rm -it php:7 php -r "var_dump(mb_ereg_match('^99.*', '123456'));"
bool(false)
$> docker run --rm -it php:7 php -r "var_dump(mb_ereg_match('^12.*', '123456'));"
bool(true)
If your are using a distro with yum package manager. Execute this command:
sudo yum install -y oniguruma-devel
for phpbrew user
You can solve this error by removing the mbstring variant from the installation command using -mbstring parameter.
Example:
phpbrew install php-7.4 +default -mbstring
For ALPINE-based images e.g. FROM php:8.0.24-fpm-alpine3.16
you have to add the package oniguruma-dev like:
FROM php:8.0.24-fpm-alpine3.16
...
RUN apk update && apk add oniguruma-dev
...
RUN docker-php-ext-install -j$(nproc) mbstring
...
As #katalabe & #kgx already mentioned:
mbstring requires any kind of oniguruma installed.
For Debian-based images please see #kgx answer above: https://stackoverflow.com/a/59373581/6852290

Install mongodb driver in docker with dockerfile

I have a mongodb docker container. I need another docker container which will have php and apache installed. I want to run a php script from this container and send some data to the mongodb container to save the data in mongodb database. So i need to install mongodb driver in the php-apache container.
To do that, i have created following dockerfile:
FROM php:7.3-apache
COPY src/ /var/www/html
RUN apt-get update
RUN apt-get install openssl libssl-dev libcurl4-openssl-dev
RUN pecl install mongodb
RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN echo "extension=mongodb.so" > /usr/local/etc/php/php.ini
EXPOSE 80
It first builds php-apache docker image. Then it should install mongodb driver.
But when i run the following command
docker build -t my-mongo .
At one point it shows following message and stops the execution:
Need to get 2213 kB of archives.
After this operation, 9593 kB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get install openssl libssl-dev libcurl4-openssl-dev' returned a non-zero code: 1
What went wrong here? Is there anything wrong in the dockerfile ?
What went wrong here? Is there anything wrong in the dockerfile ?
There are at least three things wrong with your Dockerfile IMO.
The first one is not in direct relation to your problem, but you are creating way too many layers (one for each RUN command) for something as simple as adding a driver to your image. You should put all this in a single layer (i.e. a single RUN command) and cleanup after yourself at the end to keep the layer footprint small.
Now the core of your real problem. As you can see in your output, apt-get is launched in interactive mode and asks for a confirmation. The docker build process can't handle that and therefore aborts the command causing the build to fail. To overcome this, apt-get has a -y option to answer 'yes' to all prompts by default.
The last one is in the line where you add the mongo driver to php.ini: you are redirecting echo output to your file with a single gt; sign (>), hence you are replacing the full content of the file you just copied. You must use a double gt; sign (>>) for content to be appended.
The following Dockerfile should fix the problems above (tested without the copy of sources + cp of your own php.ini file since I don't have them)
FROM php:7.3-apache
COPY src/ /var/www/html
RUN apt-get update \
&& apt-get install -y --no-install-recommends openssl libssl-dev libcurl4-openssl-dev \
&& pecl install mongodb \
&& cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
&& echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
EXPOSE 80
Some explanation:
The && notation allows to run all commands one after the other in a single docker RUN command resulting in a single intermediate container, thus a single layer.
-y --no-install-recommends options to apt-get ask apt to not go interactive (answer yes everywhere) and to install only needed packages, not the recommended ones.
The two last instruction apt-get cleann && rm -rf /var/lib/apt/lists/* remove all caches made by running apt so that the layer stays as small as possible. (see apt-get chapter in docker best practice)
put this at the beginning of your Docker file, it is important to have it before installing all other extensions, otherwise it fails.
FROM php:7.3-apache
RUN apt-get update -y && apt-get upgrade \
&& pecl install mongodb && docker-php-ext-enable mongodb
That's all what I needed to get mongodb running FROM php:7.3-cli-buster Probably it will work for other versions - fpm, apache etc as well.

Docker PHP/MySQL/Apache container

Working with a Docker file which includes PHP, Apache, and MySQL. I was able to get the page to pull up in localhost. However, I am unable to get the MySQL running.
# Use an official PHP Apache runtime as a parent image
FROM php:7.0-apache
# Set the working directory to /var/www/html/
WORKDIR /var/www/html/
# Install mysqli extensions
RUN docker-php-ext-install mysqli && \
apt-get update && \
apt-get install -y zlib1g-dev && \
apt-get install -y libxml2-dev && \
docker-php-ext-install zip && \
docker-php-ext-install xml
# Make port 80 available to the world outside this container
EXPOSE 80
Based on the above, when I attempt to run the following command:
docker run --name some-mysql -e MYSQL_abcd_123456=my-secret-pw -d mysql:tag
I receive the following error in the terminal:
Unable to find image 'mysql:tag' locally
docker: Error response from daemon: manifest for mysql:tag not found.
What am I missing?
docker run command requires the image name parameter with optional version of the image (recommended).
Use:
docker run --name some-mysql -e MYSQL_abcd_123456=my-secret-pw -d mysql:latest
to pull the latest mysql image or choose the exact version listed by supported tags
for example:
5.7.25
or
8.0.15
In majority cases you should not use the tag with the version latest (or skip the version) because that is ambiguous and can give you different versions of the image on two different machines even they used for the build the same Dockerfile FROM statement or you used the same docker run (docker pull) command. Read more
I would recommend always stick to the explicit version number if possible, for example:
docker run --name some-mysql -e MYSQL_abcd_123456=my-secret-pw -d mysql:8.0.15

Installing Postgres driver in php7-fpm Docker container

I've installed PHP:latest Docker container using the docker-compose command. It installed php-7.1.6-fpm in my Docker. When I tried to install php7-pgsql extension it failed to find that package, instead found pdo and pdo_pgsql packages. That will not satisfy my need. When I search for the available packages in the installed PHP container, I could not find any related pgsql packages for php7, instead, I saw php5-pgsql package, that will not work with php7-fpm.
Finally, I installed php-5.6-fpm container after removing the old one targeting to use php5-pgsql package. But now I disappointed again that I could not find php5-pgsql package in the newly installed container.
I know I'll be missing some important points. Whether Alpine Linux does not have php-pgsql extension. What are the possible ways to include this extension in my PHP container. I've also included Nginx and Postgres in my docker-compose.yml
I've only 3-day theory knowledge in Docker and first-day practical experience.
Thanks for reading.
I ran into the same issue when I was settings up a new project to use pgsql.
I am using php7, so you should be able to use it as well. On your Dockerfile ensure you're covering the following steps.
Ensure you have the dependencies installed:
RUN apt-get update && apt-get install -y libpq-dev
Configure the extension:
RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
Install the extension:
RUN docker-php-ext-install pdo pdo_pgsql
This is an old question i know, but i ran into this today and believe the above solution has changed with php:7.4-fpm-alpine
So where it used to be:
RUN apt-get update && apt-get install -y libpq-dev
Should now be:
RUN apt-get update && apt-get install -y postgresql-dev
Hopefully this helps anyone that runs into the same issue.

Categories