You are currently viewing How to install PDO_SQLSRV in docker

How to install PDO_SQLSRV in docker

Introduction:

In modern web development, Docker has become an indispensable tool for creating consistent and portable development environments. When working with PHP applications that require connectivity to Microsoft SQL Server databases, installing the pdo_sqlsrv extension in a Docker environment is essential. In this guide, we’ll walk through the steps to install pdo_sqlsrv in Docker, enabling seamless interaction between PHP and SQL Server databases.

Dockerfile

FROM php:8.2.4
# Install required packages
RUN apt-get update && apt-get install -y \
    unixodbc \
    unixodbc-dev \
    libgss3 \
    odbcinst \
    freetds-dev

# Download and install SQLSRV extension
RUN pecl install sqlsrv pdo_sqlsrv \
    && docker-php-ext-enable sqlsrv pdo_sqlsrv

# Update php.ini to enable SQLSRV extension
RUN echo "extension=sqlsrv.so" >> /usr/local/etc/php/php.ini
RUN echo "extension=pdo_sqlsrv.so" >> /usr/local/etc/php/php.ini

# Set working directory
WORKDIR /var/www/html

# Copy your application code
COPY index.php /var/www/html/

# Expose port
EXPOSE 80

# Command to run the application
CMD ["php", "-S", "0.0.0.0:80", "-t", "/var/www/html/"]

Step 1: Use the Appropriate Base Image

FROM php:8.2.4

This line specifies the base image to use for building the Docker image. Here, we choose the PHP 8.2.4 image as the foundation for our environment.

Step 2: Install Required Packages

RUN apt-get update && \
    apt-get install -y \
    unixodbc \
    unixodbc-dev \
    libgss3 \
    odbcinst \
    freetds-dev

This command updates the package lists and installs necessary packages and dependencies required for SQL Server connectivity.

Step 3: Download and Install SQLSRV Extension

RUN pecl install sqlsrv pdo_sqlsrv \
    && docker-php-ext-enable sqlsrv pdo_sqlsrv

Here, we use PECL to download and install the sqlsrv and pdo_sqlsrv PHP extensions, which provide connectivity to Microsoft SQL Server databases.

Step 4: Enable SQLSRV Extension

RUN echo "extension=sqlsrv.so" >> /usr/local/etc/php/php.ini \
    && echo "extension=pdo_sqlsrv.so" >> /usr/local/etc/php/php.ini

These lines update the php.ini configuration file to enable the newly installed SQLSRV extension.

Step 5: Set Working Directory and Copy Application Code

WORKDIR /var/www/html
COPY . /var/www/html/

These commands set the working directory inside the container and copy the application code (e.g., index.php) into the container.

Step 6: Expose Port and Define Command

EXPOSE 80
CMD ["php", "-S", "0.0.0.0:80", "-t", "/var/www/html/"]

This step exposes port 80 and specifies the command to run the PHP built-in web server, binding it to all network interfaces on port 80.

Upload & run the docker:

  1. docker build -t my-image .
  2. docker run -d -p 8080:80 –name my-php-container my-image

Check After Container Running http://localhost:8080/

Conclusion:
In this tutorial, we’ve demonstrated how to install the pdo_sqlsrv extension in a Docker environment. By following these steps, developers can seamlessly connect PHP applications to Microsoft SQL Server databases within Docker containers, enabling efficient development and deployment workflows. With pdo_sqlsrv installed, Dockerized PHP applications gain enhanced database connectivity and portability, facilitating robust and scalable solutions

Leave a Reply