SQLServer 2022 + PHP with Docker
A few years back I made a post on how I work with PHP + SQLServer. In the intervening years, the code samples in that post have aged like milk and no longer work.
The problem
The reason my previous post no longer works is that newer Linux versions are incompatible with SQLServer 2017.
Running sqlserver
To get a working setup in 2025, we need to use SQLServer 2022. Much like last time, we start off pulling down the image for SQLServer:
- docker pull mcr.microsoft.com/mssql/server:2022-latest
We also need a container for PHP + pdo_sqlsrv
to live in, and create a container network so that our containers can communicate:
- docker network create --driver bridge sqlserver-net
We’ll also need to make a container for SQLServer and create our database:
- # Start sqlserver
- docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=p@ssw0rd" \
- -p 1433:1433 --name sqlserver -h sqlserver \
- --network sqlserver-net \
- -d mcr.microsoft.com/mssql/server:2022-latest
- # Get a sqlcmd prompt
- docker exec -it sqlserver /opt/mssql-tools18/bin/sqlcmd -No -S localhost -U sa -P "p@ssw0rd"
- # Create our database
- CREATE DATABASE app_test;
- GO
Creating our PHP container
We’ll also need a docker image that contains both PHP and the PDO extensions:
- FROM ubuntu:22.04
- ARG DEBIAN_FRONTEND=noninteractive;
- # UPDATE PACKAGES
- RUN apt-get update;
- # INSTALL SYSTEM UTILITIES
- RUN apt-get install -y \
- apt-utils \
- curl \
- git \
- apt-transport-https \
- software-properties-common \
- g++ \
- build-essential \
- dialog;
- # INSTALL locales
- RUN apt-get install -qy language-pack-en-base \
- && locale-gen en_US.UTF-8;
- ENV LANGUAGE en_US.UTF-8;
- ENV LANG en_US.UTF-8;
- ENV LC_ALL en_US.UTF-8;
- ENV LC_CTYPE=en_US.UTF-8
- ENV LC_ALL=en_US.UTF-8
- # INSTALL PHP & LIBRARIES
- RUN add-apt-repository -y ppa:ondrej/php;
- RUN apt-get update;
- RUN apt-get --no-install-recommends --no-install-suggests --yes --quiet install \
- php-pear \
- php8.2 \
- php8.2-common \
- php8.2-mbstring \
- php8.2-dev \
- php8.2-xml \
- php8.2-cli \
- php8.2-mbstring \
- php8.2-curl \
- php8.2-xml \
- php8.2-zip \
- php8.2-odbc \
- php8.2-intl;
- # INSTALL ODBC DRIVER & TOOLS
- RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -;
- RUN curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list;
- RUN apt-get update
- RUN ACCEPT_EULA=Y apt-get install -y \
- mssql-tools18 \
- unixodbc \
- unixodbc-dev;
- RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile;
- RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc;
- RUN exec bash;
- # INSTALL & LOAD SQLSRV DRIVER & PDO
- RUN pecl install sqlsrv;
- RUN echo ";priority=20\nextension=sqlsrv.so" > /etc/php/8.2/cli/conf.d/20-sqlsrv.ini;
- RUN pecl install pdo_sqlsrv;
- RUN echo ";priority=30\nextension=pdo_sqlsrv.so" > /etc/php/8.2/cli/conf.d/30-pdo_sqlsrv.ini;
- WORKDIR /src
- CMD ["/bash"]
We can build an image for this dockerfile with:
- docker build -t php-sqlsrv -f sqlserver.Dockerfile .
and run it with:
- docker run --rm -it --network sqlserver-net -v $(pwd):/src php-sqlsrv bash
You should now have a SQLServer compatible development environment that works on newer linux kernels, and uses newer PHP & SQLServer versions.
There are no comments, be the first!