thecodingmachine / docker-images-php

A set of PHP Docker images
MIT License
768 stars 137 forks source link

Install SQL Server (sqlsrv) and PDO_SQLSRV (pdo_sqlsrv) #385

Closed joelataylor closed 2 months ago

joelataylor commented 3 months ago

I did a search for sqlsrv and couldn't find anything specific around installing this extension. Do you have any tips for installing?

ARG PHP_EXTENSIONS="mysqlsrv pdo_sqlsrv"
FROM thecodingmachine/php:8.3-v4-apache

Didn't work, but I didn't really expect it to as it's not listed on the available extensions. Help? Thank you!

doreinhardt commented 3 months ago

@joelataylor I just had to do that and it seems to be quite the hassle.

This is an excerpt from my Dockerfile:

RUN sh -c "curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -" \
    && apt-get update \
    && sh -c "curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list" \
    && apt-get update \
    && ACCEPT_EULA=Y apt-get install -y \
        msodbcsql18 \
        mssql-tools18 \
        unixodbc-dev \
        php-pear \
        php-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN pecl config-set php_ini /etc/php/8.3/fpm/php.ini
RUN pecl install sqlsrv
RUN pecl install pdo_sqlsrv
RUN printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.3/mods-available/sqlsrv.ini
RUN printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.3/mods-available/pdo_sqlsrv.ini
RUN phpenmod -v 8.3 sqlsrv pdo_sqlsrv

First your installing the ODBC drivers from microsoft then your are installing php-pear so you can use pecl. This is done for php-fpm so you might need to adjust it to your needs.

This will probably get the job done but i would rather avoid installing php-pear in the future. So an easier way would be much appreciated.

joelataylor commented 2 months ago

Thank you @mistraloz (sorry, I didn't get an alert on this message). Good to go now. Much appreciated!