Installing the SQL Server PHP extension in Docker on Mac
I’m working on a project that uses MS SQL Server as its database. Recently, I noticed that the SQL Server Docker container now works with Apple Silicon Macs, so looked into setting up a PHP-FPM container with the sqlsrv extension installed.
I’m noting the relevant parts of my Dockerfile for when I need them again, so this is entirely an aide-mémoire!
FROM ubuntu:22.04
# Install PHP as per https://akrabat.com/7126
## Register the Microsoft package repository
RUN curl https://packages.microsoft.com/keys/microsoft.asc | tee /etc/apt/trusted.gpg.d/microsoft.asc \
&& curl https://packages.microsoft.com/config/debian/11/prod.list | tee /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update
# Install the ODBC libraries and MS command line tools
RUN ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql18 mssql-tools18
# Install sqlsrv and pdo_sqlsrv PHP extensions
RUN pecl install sqlsrv \
&& pecl install pdo_sqlsrv \
&& echo "extension=sqlsrv.so" > /etc/php/8.2/mods-available/sqlsrv.ini \
&& echo "extension=pdo_sqlsrv.so" > /etc/php/8.2/mods-available/pdo_sqlsrv.ini \
&& phpenmod -v 8.2 sqlsrv pdo_sqlsrv \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Other stuff, such as copying over settings files and starting PHP-FPM
It all works remarkably well and makes development that much easier. There are two key MS libraries needed: msodbcsql18 for the PHP extensions and mssql-tools18 installs /opt/mssql-tools18/bin/sqlcmd, the CLI tool for SQL Server.
Configuring SQL Server in Docker Compose
To run SQL Server on an Apple Silicon Mac you need to enable Rosetta in Docker Desktop and then configure a few environment variables. This is how I set the db service in compose.yaml:
db:
platform: linux/amd64
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
ACCEPT_EULA: 'Y'
MSSQL_PID: 'Developer'
MSSQL_SA_PASSWORD: 'Password123!'
MSSQL_TCP_PORT: 1433
ports:
- "1433:1433"
volumes:
- ./opt/mssql:/var/opt/mssql
We set plaform to remove an annoying warning, but as there isn’t an ARM64 container for full SQL Server, the warning doesn’t help.
The environment variables set are:
- ACCEPT_EULA: Must be Y otherwise nothing works :)
- MSSQL_PID: Set to Developer for the developer licence, unless you have a another license.
- MSSQL_SA_PASSWORD: The root password (The sa user in SQL Server’s world)
- MSSQL_TCP_PORT: Not needed as it defaults to 1433, but a useful reminder.
The default MSSQL_DATA_DIR directory on Linux is /var/opt/mssql, so I map that to the opt/mssql directory in my project, which is registered in .gitignore.