Polyscope - The agent-first dev environment for Laravel

Install Microsoft SQL Drivers for PHP 7 in Docker

Published on by

Install Microsoft SQL Drivers for PHP 7 in Docker image

I started a project recently that required that I connect a Microsoft SQL Server database with a Laravel 5.5 application, so I thought I’d document how I install the pdo_sqlsrv module and install the Microsoft drivers for PHP in Docker.

Once you install the driver, you can easily configure Laravel to use a Microsoft SQL Server connection:

DB_CONNECTION=sqlsrv

I use Docker with most of my PHP projects—I am releasing a book on using Docker with PHP next week—so I thought I’d show you the recipe for getting a SQL server connection with the PDO driver.

Here’s the Dockerfile needed to install the perquisites and pdo_sqlsrv PECL module:

FROM php:7.1-apache
 
ENV ACCEPT_EULA=Y
 
# Microsoft SQL Server Prerequisites
RUN apt-get update \
&& 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 install -y --no-install-recommends \
locales \
apt-transport-https \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen \
&& apt-get update \
&& apt-get -y --no-install-recommends install \
unixodbc-dev \
msodbcsql17
 
RUN docker-php-ext-install mbstring pdo pdo_mysql \
&& pecl install sqlsrv pdo_sqlsrv xdebug \
&& docker-php-ext-enable sqlsrv pdo_sqlsrv xdebug
 
COPY index.php /var/www/html/

I was having difficulties installing the necessary packages with Debian Stretch and PHP 7.2, so I am using the official PHP 7.1 Docker image as the base image that I extend from at the time of writing.

I am demonstrating the apache version so it’s easy to serve up an index.php file to verify the installation. In most of my projects I am using Caddy or Nginx as the web server, but Apache is an excellent choice too!

The first RUN instruction installs the packages necessary for installing the msodbcsql17 and unixodbc-dev packages successfully:

# Microsoft SQL Server Prerequisites
RUN apt-get update \
&& 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 install -y --no-install-recommends \
locales \
apt-transport-https \
&& echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
&& locale-gen \
&& apt-get update \
&& apt-get -y --no-install-recommends install \
unixodbc-dev \
msodbcsql17

These packages are needed to install the PECL module for SQL Server’s PDO driver. You can find installation instructions for various platforms in the Microsoft/msphpsql GitHub project readme.

It’s also important to note that we set the ACCEPT_EULA environment variable to Y which is needed when running apt-get install msodbcsql. It signifies that you’ve accepted the End-user license agreement when installing the package.

The next RUN instruction installs PHP extensions using the official PHP image’s docker-php-ext-install script. In this case, I am installing a Laravel prerequisite mbstring along with pdo, and pdo_mysql:

RUN apt-get update \
&& apt-get -y --no-install-recommends install \
libxml2-dev \
&& docker-php-ext-install mbstring pdo pdo_mysql soap \
&& pecl install sqlsrv pdo_sqlsrv xdebug \
&& docker-php-ext-enable sqlsrv pdo_sqlsrv xdebug

Finally, the most important part of this RUN instruction, we are using pecl to install the sqlsrv and pdo_sqlsrv modules, and then using docker-php-ext-enable to enable them. I am also installing Xdebug because I typically install this in most images for development.

Building and Running The Container

The final instruction in the Dockerfile is the COPY instruction that is copying a simple index.php file that we will create now from the command line:

$ echo "<?php phpinfo();" > index.php

When we build the image, the Dockerfile will copy the index.php file in the location that Apache will look for it, and we can verify that the SQL Server drivers were installed and working with PHP.

You can build the image with the docker build command to try it out real quick:

$ docker build --pull -t sqlserver-demo .

The image will take a few minutes depending on your connection speed. Once the build finishes, you can run it from the command line:

$ docker run --rm -p 8080:80 sqlserver-demo

The docker run command runs the container, and with the help of the -p 8080:80 flag, we have mapped 8080 to the container which is listening on port 80. The --rm flag will remove the container once it exits.

After the container is running you should see some Apache logs in the console, and you can visit http://localhost:8080 in the browser to verify that the SQL Server module is enabled:

You should now be able to connect to a SQL Server database in your PHP 7.1 application! I’ve kept the example simple to just focus on the steps necessary to install the SQL Server PDO drivers, but I hope you can see how Docker can help ease the hassle of connecting to a Microsoft SQL database from your PHP applications.


Want to learn more about using Docker with PHP? I wrote a book, Docker for PHP Developers that is now available for sale. You can pick from one of two packages:

The links included are affiliate links which means if you decide to buy Laravel News gets a little kickback to help run this site.


Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review

The latest

View all →
Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel image

Larapanda: A Type-Safe Lightpanda Browser SDK for Laravel

Read article
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

Read article
DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell image

DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell

Read article
Model-Based Scheduling for Laravel with Cadence image

Model-Based Scheduling for Laravel with Cadence

Read article
Laravel's AI SDK adds sub-agents image

Laravel's AI SDK adds sub-agents

Read article
Laravel Introduces First-Party Passkey Authentication Support image

Laravel Introduces First-Party Passkey Authentication Support

Read article