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:

Show Plain Text
  1. 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:

Show Plain Text
  1. docker network create --driver bridge sqlserver-net

We’ll also need to make a container for SQLServer and create our database:

Show Plain Text
  1. # Start sqlserver
  2. docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=p@ssw0rd" \
  3.    -p 1433:1433 --name sqlserver -h sqlserver \
  4.    --network sqlserver-net \
  5.    -d mcr.microsoft.com/mssql/server:2022-latest
  6.  
  7. # Get a sqlcmd prompt
  8. docker exec -it sqlserver /opt/mssql-tools18/bin/sqlcmd -No -S localhost -U sa -P "p@ssw0rd"
  9.  
  10. # Create our database
  11. CREATE DATABASE app_test;
  12. GO

Creating our PHP container

We’ll also need a docker image that contains both PHP and the PDO extensions:

Show Plain Text
  1. FROM ubuntu:22.04
  2.  
  3. ARG DEBIAN_FRONTEND=noninteractive;
  4.  
  5. # UPDATE PACKAGES
  6. RUN apt-get update;
  7.  
  8. # INSTALL SYSTEM UTILITIES
  9. RUN apt-get install -y \
  10.     apt-utils \
  11.     curl \
  12.     git \
  13.     apt-transport-https \
  14.     software-properties-common \
  15.     g++ \
  16.     build-essential \
  17.     dialog;
  18.  
  19. # INSTALL locales
  20. RUN apt-get install -qy language-pack-en-base \
  21.     && locale-gen en_US.UTF-8;
  22. ENV LANGUAGE en_US.UTF-8;
  23. ENV LANG en_US.UTF-8;
  24. ENV LC_ALL en_US.UTF-8;
  25. ENV LC_CTYPE=en_US.UTF-8
  26. ENV LC_ALL=en_US.UTF-8
  27.  
  28. # INSTALL PHP & LIBRARIES
  29. RUN add-apt-repository -y ppa:ondrej/php;
  30. RUN apt-get update;
  31. RUN apt-get --no-install-recommends --no-install-suggests --yes --quiet install \
  32.     php-pear \
  33.     php8.2 \
  34.     php8.2-common \
  35.     php8.2-mbstring \
  36.     php8.2-dev \
  37.     php8.2-xml \
  38.     php8.2-cli \
  39.     php8.2-mbstring \
  40.     php8.2-curl \
  41.     php8.2-xml \
  42.     php8.2-zip \
  43.     php8.2-odbc \
  44.     php8.2-intl;
  45.  
  46. # INSTALL ODBC DRIVER & TOOLS
  47. RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -;
  48. RUN curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list > /etc/apt/sources.list.d/mssql-release.list;
  49. RUN apt-get update
  50. RUN ACCEPT_EULA=Y apt-get install -y \
  51.     mssql-tools18 \
  52.     unixodbc \
  53.     unixodbc-dev;
  54. RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile;
  55. RUN echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc;
  56. RUN exec bash;
  57.  
  58. # INSTALL & LOAD SQLSRV DRIVER & PDO
  59. RUN pecl install sqlsrv;
  60. RUN echo ";priority=20\nextension=sqlsrv.so" > /etc/php/8.2/cli/conf.d/20-sqlsrv.ini;
  61.  
  62. RUN pecl install pdo_sqlsrv;
  63. RUN echo ";priority=30\nextension=pdo_sqlsrv.so" > /etc/php/8.2/cli/conf.d/30-pdo_sqlsrv.ini;
  64.  
  65. WORKDIR /src
  66.  
  67. CMD ["/bash"]

We can build an image for this dockerfile with:

Show Plain Text
  1. docker build -t php-sqlsrv -f sqlserver.Dockerfile .

and run it with:

Show Plain Text
  1. 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.

Comments

There are no comments, be the first!

Have your say: