Skip to content

Instantly share code, notes, and snippets.

@xenogew
Created October 30, 2018 06:32
Example of PHP 7.2.x Docker image install with MS SQL Server extensions
FROM php:7.2.11-fpm
WORKDIR /application
ENV ACCEPT_EULA=Y
# 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 apt-utils libxml2-dev gnupg apt-transport-https \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install git
RUN apt-get update \
&& apt-get -y install git \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install MS ODBC Driver for SQL Server
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \
&& apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev \
&& pecl install sqlsrv \
&& pecl install pdo_sqlsrv \
&& echo "extension=pdo_sqlsrv.so" >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini \
&& echo "extension=sqlsrv.so" >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-sqlsrv.ini \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
# Install required extensions
RUN docker-php-ext-install intl mysqli pdo pdo_mysql
@GibsonGM12
Copy link

Excelent! Thank you for your dockerfile!!

@thefx
Copy link

thefx commented Mar 2, 2020

It works, Thank you!

@sathishd-healthicity
Copy link

Thank you.

@dzona
Copy link

dzona commented Nov 27, 2020

here is my configuration for PHP 7.3 and Ubuntu 18.04

# Microsoft ODBC 17
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get -y install msodbcsql17
RUN apt-get -y install unixodbc-dev

# 7.3
RUN apt-get -y install php-pear php7.3-dev
RUN update-alternatives --set php /usr/bin/php7.3
RUN update-alternatives --set phar /usr/bin/phar7.3
RUN update-alternatives --set phar.phar /usr/bin/phar.phar7.3
RUN update-alternatives --set phpize /usr/bin/phpize7.3
RUN update-alternatives --set php-config /usr/bin/php-config7.3

RUN pecl uninstall -r sqlsrv
RUN pecl uninstall -r pdo_sqlsrv
RUN pecl -d php_suffix=7.3 install sqlsrv
RUN pecl -d php_suffix=7.3 install pdo_sqlsrv
RUN printf "; priority=30\nextension=sqlsrv.so\n" > /etc/php/7.3/mods-available/sqlsrv.ini
RUN printf "; priority=40\nextension=pdo_sqlsrv.so\n" > /etc/php/7.3/mods-available/pdo_sqlsrv.ini
RUN phpenmod -v 7.3 sqlsrv pdo_sqlsrv

@LunkRat
Copy link

LunkRat commented Mar 3, 2021

Thank you. Helped me out.

@utkircoder
Copy link

thank you

@brycekirk
Copy link

brycekirk commented Jan 21, 2022

Fails with this error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.

Edit: Fixed by adding the following after ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update -y && apt-get install -y \
    libicu-dev \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl

@brycekirk
Copy link

After updating WORKDIR and building the image, I run my container with docker run -v /Users/Me/Sites/AppName/src/php:/AppName/src/php -p 8080:8080 whitecap php -S 0.0.0.0:8080. With that, I can visit my php files in my browser, but I get an error: Uncaught Error: Call to undefined function sqlsrv_connect()

Checking phpinfo() is not showing the sqlsrv extension installed, yet there are no errors on the container build. Not sure what could be going wrong here...

@LunkRat
Copy link

LunkRat commented Jan 22, 2022

@brycekirk Double-check that your Dockerfile version of Debian matches the version in the apt sources.list entry.

For example if your image is based on Debian Buster (10) then you need to have 10 in the URL from Microsoft:

...
    curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
...

Something to check. I had an experience earlier this year where the apt sources.list file was not matched to the Debian version of my image and it produced the behavior you describe. You can also post your entire Dockerfile here if this still doesn't help you.

@brycekirk
Copy link

@LunkRat Not sure if I understand–I'm basically just using the exact Dockerfile above (with those libicu lines from my earlier post), so 9 should still be the correct version I believe? I'm on an M1 Mac..not sure if that makes a difference.

Here's my Dockerfile:

FROM php:7.2.11-fpm
WORKDIR /Whitecap

ENV ACCEPT_EULA=Y

# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive

# Install libicu and intl
RUN apt-get update -y && apt-get install -y \
    libicu-dev \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl

# Install selected extensions and other stuff
RUN apt-get update \
    && apt-get -y --no-install-recommends install apt-utils libxml2-dev gnupg apt-transport-https \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

# Install git
RUN apt-get update \
    && apt-get -y install git \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

# Install MS ODBC Driver for SQL Server
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update \
    && apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev \
    && pecl install sqlsrv \
    && pecl install pdo_sqlsrv \
    && echo "extension=pdo_sqlsrv.so" >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini \
    && echo "extension=sqlsrv.so" >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-sqlsrv.ini \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

# Install required extensions
RUN docker-php-ext-install intl mysqli pdo pdo_mysql

@Joshaem
Copy link

Joshaem commented Feb 2, 2022

I followed the instructions but doesn't work. I'm also on my M1 Mac

@kingfisher11
Copy link

can this be use for PHP 7.4?

@fabiante
Copy link

I am using this snippet on top of php:7.4.22-apache. A couple of months ago it worked fine, now I get this error when running apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev

The following packages have unmet dependencies:
 libodbc1 : PreDepends: multiarch-support but it is not installable
 odbcinst1debian2 : PreDepends: multiarch-support but it is not installable
E: Unable to correct problems, you have held broken packages.

@DmitrySidorenkoShim
Copy link

I am using this snippet on top of php:7.4.22-apache. A couple of months ago it worked fine, now I get this error when running apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev

The following packages have unmet dependencies:
 libodbc1 : PreDepends: multiarch-support but it is not installable
 odbcinst1debian2 : PreDepends: multiarch-support but it is not installable
E: Unable to correct problems, you have held broken packages.

the same issue
did you manage to solve this?

@szykulat
Copy link

I am using this snippet on top of php:7.4.22-apache. A couple of months ago it worked fine, now I get this error when running apt-get -y --no-install-recommends install msodbcsql17 unixodbc-dev

The following packages have unmet dependencies:
 libodbc1 : PreDepends: multiarch-support but it is not installable
 odbcinst1debian2 : PreDepends: multiarch-support but it is not installable
E: Unable to correct problems, you have held broken packages.

the same issue did you manage to solve this?

Not sure if this will help but snippet is quite old maybe check if debian version of this images is the same version as used in url "https://packages.microsoft.com/config/debian/9/prod.list". Use "cat /etc/os-version" which should give you the answer, I just build image by changin 9 to 11 but my was based on php:8.1.5.

@zocker-160
Copy link

thank you so much, you saved my ass, chef’s kiss!

@webchi
Copy link

webchi commented Apr 17, 2025

Upgrade to actual versions

FROM php:8.4.6-fpm

ENV ACCEPT_EULA=Y

RUN apt-get update && apt-get install -y gnupg libpq-dev libzip-dev

# sql server drivers https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor -o /usr/share/keyrings/microsoft-prod.gpg && \
    curl https://packages.microsoft.com/config/debian/12/prod.list > /etc/apt/sources.list.d/mssql-release.list && \
    apt-get update && apt-get install -y msodbcsql18 unixodbc-dev && \
    pecl install sqlsrv-5.12.0 && \
    pecl install pdo_sqlsrv-5.12.0 && \
    docker-php-ext-enable sqlsrv pdo_sqlsrv && \
    docker-php-ext-install pdo_mysql pdo_pgsql zip

COPY --from=composer /usr/bin/composer /usr/bin/composer

USER www-data

COPY --chown=www-data:www-data . .

RUN composer install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment