Skip to content

Instantly share code, notes, and snippets.

@PaulKish
Created September 22, 2023 09:18
Show Gist options
  • Save PaulKish/fc22538194e2aa0ca5b9a8384998810d to your computer and use it in GitHub Desktop.
Save PaulKish/fc22538194e2aa0ca5b9a8384998810d to your computer and use it in GitHub Desktop.
Docker File for PHP8.2 with an SQL Server Connection
# Use an official PHP runtime as a parent image
FROM serversideup/php:8.2-cli
# Set the working directory to /app
WORKDIR /app
# Install system dependencies and lsb-release
RUN apt-get update && \
apt-get install -y lsb-release unixodbc-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Add the Microsoft SQL Server repository and key
RUN curl https://packages.microsoft.com/keys/microsoft.asc | tee /etc/apt/trusted.gpg.d/microsoft.asc && \
curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | tee /etc/apt/sources.list.d/mssql-release.list
# Install Microsoft SQL Server driver and dependencies
RUN apt-get update && \
ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools18 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install PHP PEAR (which includes pecl) and PHP development tools
RUN apt-get update && \
apt-get install -y php-pear php-dev autoconf && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install SQLSRV and PDO SQL Server extensions
RUN pecl install sqlsrv pdo_sqlsrv
# Set the PATH environment variable to include mssql-tools
ENV PATH="$PATH:/opt/mssql-tools18/bin"
# Create sqlsrv and pdo_sqlsrv configuration files
RUN printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.2/cli/conf.d/sqlsrv.ini && \
printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.2/cli/conf.d/pdo_sqlsrv.ini
# Enable the extensions
RUN phpenmod -v 8.2 sqlsrv pdo_sqlsrv
# Copy composer.lock and composer.json and install dependencies
COPY composer.lock composer.json /app/
RUN composer install --no-scripts --no-dev --no-autoloader
# Copy the rest of the application code
COPY . /app
# Generate optimized autoload files and cache
RUN composer dump-autoload --optimize
# Define the entry point for the CLI application
ENTRYPOINT ["php", "artisan"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment