Install / Configure SQL Server PDO driver for PHP docker image - php

I have a simple docker file, as follows:
FROM php:7.2-apache
COPY src/ /var/www/html/
Normally to install drivers for Mongo or MySQL connectivity I would do so by adding something like the below to the dockerfile:
docker-php-ext-install mongo
On this occasion I want to connect my php application to a SQL Server database, and I understand the best way to do this for php 7.x is by using the PDO driver, however I am unfamiliar with how to do configure this in the dockerfile.
I've tried doing a pecl install, like adding:
RUN pecl install sqlsrv pdo_sqlsrv
However this fails with a combination of errors that do not seem to point me in the right direction.
I'm just looking for a simple way to get this done in a dockerfile or by using docker run.
For added info, here's the error I'm getting:
/tmp/pear/temp/sqlsrv/shared/xplat.h:30:17: fatal error: sql.h: No such file or directory
#include <sql.h>
^
compilation terminated.
Makefile:194: recipe for target 'conn.lo' failed
make: *** [conn.lo] Error 1
ERROR: `make' failed
The command '/bin/sh -c pecl install sqlsrv pdo_sqlsrv && docker-php-ext-enable pdo_sqlsrv' returned a non-zero code: 1
Thanks all

I have created a docker file for this exact purpose:
FROM php:7.3-apache
ENV ACCEPT_EULA=Y
RUN apt-get update && apt-get install -y gnupg2
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN docker-php-ext-enable sqlsrv pdo_sqlsrv
COPY . /var/www/html/
Enjoy!

Did you get an answer to this? I got it working with the following steps. (The unixodbc-dev package should get you past the pecl install.)
in the Docker file:
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
And then you have to add some changes to php.ini to enable sqlserver.
get a local copy of php.ini and add these lines:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
Then copy your local php.ini into the docker image (my file is in a local "config" folder).
in the Docker file:
COPY config/php.ini /usr/local/etc/php/

My docker config:
###############
# MSSQL support
###############
RUN apt-get update \
&& apt-get install -y gpg unixodbc unixodbc-dev \
&& docker-php-ext-install pdo pdo_mysql \
&& pecl install sqlsrv pdo_sqlsrv
# ------------ Install MS SQL client deps ------------ #
# adding custom MS repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
# install SQL Server drivers and tools
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
RUN /bin/bash -c "source ~/.bashrc"
# Debian 9 msodbcsql : https://packages.microsoft.com/debian/9/prod/pool/main/m/msodbcsql17/
RUN wget https://packages.microsoft.com/debian/9/prod/pool/main/m/msodbcsql17/msodbcsql17_17.4.2.1-1_amd64.deb
RUN ACCEPT_EULA=Y dpkg -i msodbcsql17_17.4.2.1-1_amd64.deb
RUN apt-get -y install locales
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen
RUN echo "extension=sqlsrv.so" >> /usr/local/etc/php/conf.d/docker-php-ext-sqlsrv.ini
RUN echo "extension=pdo_sqlsrv.so" >> /usr/local/etc/php/conf.d/docker-php-ext-pdo-sqlsrv.ini
# -------------- END MSSQL -------------------------------- #

FROM php:7.4.0-apache
ENV ACCEPT_EULA=Y
RUN apt-get update && apt-get install -y gnupg2
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-relea`enter code here`se.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable sqlsrv pdo_sqlsrv pdo pdo_mysql
COPY index.php /var/www/html/

Related

Is Ubuntu 21.04 compatible with PHP sqlsrv driver?

I have a web system developed with PHP + MS Sql Server and I do maintenance on Windows environment. But I prefer working in Linux environment, Ubuntu to be more accurate.
But I cannot find a way to install this database driver. I googled but I cannot find any solution. Some tutorials shows how to install in 20.04, 18.04 versions, but even following the steps to the letter, I could no install.
Some tutorials that did not worked.
How to Install the PHP SQLSRV Extension
Linux and macOS Installation Tutorial for the Microsoft Drivers for PHP for SQL Server
Any suggestions?
It seems, at least today, that msodbcsql17 mssql-tools are not available in the Ubuntu 21.04 repos...
$ curl -s https://packages.microsoft.com/config/ubuntu/21.04/prod.list
deb [arch=amd64,armhf,arm64] https://packages.microsoft.com/ubuntu/21.04/prod hirsute main
$ curl -s https://packages.microsoft.com/ubuntu/21.04/prod/dists/hirsute/Contents-amd64.gz | gzip -d | grep -E "msodbcsql|mssql"
# No output.
As a result you'll get errors when you try to install them via apt-get:
$ curl -s https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ sudo bash -c "curl -s https://packages.microsoft.com/config/ubuntu/21.04/prod.list > /etc/apt/sources.list.d/mssql-release.list"
$ sudo apt-get update --yes
# ...
$ sudo ACCEPT_EULA=Y apt-get --verbose-versions --yes install msodbcsql17 mssql-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package msodbcsql17
E: Unable to locate package mssql-tools
Update
I have written a bash script to automatically install the appropriate drivers for Ubuntu or Debian: https://github.com/sfinktah/bash/blob/master/add_sqlsrv_repo_combined.sh
I couldn't test support for other Linux vendors, but the capability is there if someone wants to send a PR.
Original Post
21.04 drivers are now available.
Follow the instructions on
https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 and https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15#ubuntu17
Or as above:
sudo su
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version
# Automatic version selection (does not work for 21.10, but 20.10 or 21.04 are fine)
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -r -s)/prod.list > /etc/apt/sources.list.d/mssql-release.list
# Manual specification: Ubuntu 21.04
curl https://packages.microsoft.com/config/ubuntu/21.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install -y mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install -y unixodbc-dev

I need a docker image that can get connected to an external MSSQL datatbase with php

I need a docker image that can get connected to an external MSSQL datatbase when executing a PHP script. I have created one, but I could only get connected to the DB through the tsql CLI. I think it uses Freetds. But when I tried to use a php script I had an error saying that the php could not find the PDO library... Can somebody help me find out what I have missed ?
Here is the image I used:
FROM php:7.4-cli
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y \
unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
ADD freetds.conf /etc/freetds/freetds.conf
ADD locales.conf /etc/freetds/locales.conf
CMD ["/bin/bash"]
and the PHP script look like this:
<?php
try {
$conn = new PDO("dblib:host={$sql_host};dbname={$sql_dbnm}", "$sql_user", "$sql_pswd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (Exception $e) {
var_dump($e);
die(print_r($e->getMessage()));
}
$tsql = "SELECT field FROM table";
$getResults = $conn->prepare($tsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row){
echo "{$row['field']}\n";
}
Thanks
I have found a way.
Here is the content of my Dockerfile:
FROM php:7.3
# Update packages
RUN apt-get update
# Install PHP and composer dependencies
RUN apt-get install gnupg -qq git wget curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev
# Clear out the local repository of retrieved package files
RUN apt-get clean
# Install needed extensions
# Here you can install any other extension that you need during the test and deployment process
RUN docker-php-ext-install pdo_mysql zip
# adding custom MS repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
# install SQL Server drivers
RUN apt-get update && ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17
RUN wget http://pecl.php.net/get/sqlsrv-5.9.0.tgz
RUN pear install sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable sqlsrv
RUN wget http://pecl.php.net/get/pdo_sqlsrv-5.9.0.tgz
RUN pear install pdo_sqlsrv-5.9.0.tgz
RUN docker-php-ext-enable pdo_sqlsrv
RUN echo "extension= pdo_sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
RUN echo "extension= sqlsrv.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
A much easier way is to use install-php-extensions: it will let you install a ton of PHP extensions with just 1 line:
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions pdo_sqlsrv sqlsrv
More details on https://github.com/mlocati/docker-php-extension-installer

Docker fails to enable php extensions

I am trying to install some php extensions all day but an error I do not understand occurs. Basically if I remove the install docker-php-ext-enable line everything seems to work normally (except my app that depends on them).
Dockerfile
FROM php:8.0-apache
RUN apt-get update && \
apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
RUN a2enmod rewrite
RUN docker-php-ext-enable bcmath gd mysqli soap zip xml ctype curl fileinfo json
COPY idoit-1.16.2.zip /temp/idoit-1.16.2.zip
RUN unzip /temp/idoit-1.16.2.zip -d /var/www
RUN rm /temp/idoit-1.16.2.zip
COPY apache-vhost.conf /etc/apache2/sites-available/000-default.conf
# RUN chown -R www-data:www-data /var/www
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
Here is what I get when i run sudo docker build -t test .
Sending build context to Docker daemon 153.9MB
Step 1/23 : FROM php:8.0-apache
---> 1157b188bd87
Step 2/23 : RUN apt-get update && apt-get install -y unzip libcurl4-openssl-dev zlib1g-dev libpng-dev libxml2-dev libzip-dev
---> Using cache
---> aacd74715e3f
Step 3/23 : RUN a2enmod rewrite
---> Using cache
---> 1ab1cd3d1228
Step 4/23 : RUN docker-php-ext-enable fileinfo
---> Running in 7d8a84075a50
error: 'fileinfo' does not exist
usage: /usr/local/bin/docker-php-ext-enable [options] module-name [module-name ...]
ie: /usr/local/bin/docker-php-ext-enable gd mysqli
/usr/local/bin/docker-php-ext-enable pdo pdo_mysql
/usr/local/bin/docker-php-ext-enable --ini-name 0-apc.ini apcu apc
Possible values for module-name:
opcache.so sodium.so
Some of the above modules are already compiled into PHP; please check
the output of "php -i" to see which modules are already loaded.
Any ideas?

Docker / Symfony / MongoDB - Cannot install mongodb with PHP 5.6 FPM

I'm not able to install mongodb extension using php:5.6-fpm image.
What's wrong with my Dockerfile configuration ?
FROM php:5.6-fpm
RUN apt-get update \
&& mkdir -p /usr/share/man/man1 \
&& mkdir -p /usr/share/man/man7 \
&& apt-get install -y --no-install-recommends vim curl debconf subversion git apt-transport-https apt-utils \
build-essential locales acl mailutils wget zip unzip htop vim \
gnupg gnupg1 gnupg2 \
libmemcached-dev zlib1g-dev \
libcurl4-openssl-dev pkg-config libssl-dev libicu-dev g++
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql zip intl
RUN pecl install mongodb
RUN docker-php-ext-enable mongodb
COPY php.ini /usr/local/etc/php/conf.d/php.ini
...
...
I have the following error when executing docker-compose build
Step 5/18 : RUN pecl install mongodb
---> Running in 5cd13b1b969c
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
pecl/mongodb requires PHP (version >= 7.0.0, version <= 7.99.99), installed version is 5.6.40
No valid packages found
install failed
Solution:
RUN pecl install mongodb-1.7.4

PHP 7, Imagemagick and CircleCI

Does anybody know how to install php 7 with ImageMagick on CircleCI?
Everything except ImageMagick works. Here is the error message I get.
Intervention\Image\Exception\NotSupportedException:
ImageMagick module not available with this PHP installation.
Here is my circle.yml
machine:
pre:
- sudo apt-get update; USE_PRECOMPILE=true sudo -E circleci-install php 7.0.4
php:
version: 7.0.4
timezone: America/Los_Angeles
services:
- mysql
environment:
APP_ENV: testing
APP_KEY: randomrandomrandomrandomrandomra
dependencies:
pre:
- sudo aptitude -y install imagemagick
- sudo apt-add-repository -y ppa:ondrej/php
- sudo apt-get -y update
- sudo apt-get -y install php-imagick
override:
- composer install --prefer-dist --no-interaction
post:
- mv .env.circleci .env
test:
override:
- vendor/bin/phpunit
To get imagick working with PHP 7 you need to use the following in your circle.yml:
machine:
php:
version: 7.1.3
dependencies:
pre:
- printf "\n" | pecl install -f imagick
- echo "extension = imagick.so" >> /opt/circleci/php/$(phpenv global)/etc/php.ini
Hope this helps!
I had a similar problem and solved it using suggestions from these two posts...
From this post from the circle-ci discussion forum, you'll need to install with PECL and then enable with docker-php-ext-enable
RUN apt-get update && apt-get install -y \
libmagickwand-dev --no-install-recommends \
&& pecl install imagick \
&& docker-php-ext-enable imagick
if it fails to activate the extension you may add this after/instead of docker-php-ext-enable imagick
echo "extension=imagick.so" | sudo tee -a /usr/local/etc/php/conf.d/ext-imagick.ini
For that, you can refer to this github comment

Categories