microsoft / msphpsql

Microsoft Drivers for PHP for SQL Server
MIT License
1.8k stars 374 forks source link

Add this driver to the alpine linux php docker image #300

Open zippy1981 opened 7 years ago

zippy1981 commented 7 years ago

Alpine linux uses a smaller libc than ubuntu and redhat and produces really small docker images. There are official php docker images on docker hub that use this. One installs an extension on them via the command docker-php-ext-install this compiles the extension from source and setups up an INI file for it.

I have not determined if any of the following tasks are already done:

  1. Compile this driver on alpine linux. If this works can someone at MSFT setup travis CI to do this?
  2. Get whoever maintains the alpine linux php docker images on board with this
  3. Create a pull request so docker-php-ext-install will pull the driver.
croblesm commented 4 years ago

In case you guys are still looking for a minimal "mssql-tools" with Alpine. I was able to create one, you can find the Dockerfile here.

Let me know if you have any questions or comments.

Cheers,

neclimdul commented 4 years ago

It was a bit tricky getting the combination of pecl commands and tracking down the odbc drivers(which aren't mentioned in the documentation) but once I did it was seamless. Nice to walk away from a new segfault in FreeTDS instead of tracing it out and just being able to use this. Thank you! Keep up the great work!

yitam commented 4 years ago

Thanks @neclimdul and glad that it works for you!

In case anyone else is looking for the documentation for installing sqlsrv drivers in Alpine Linux, this is the section for Alpine 3.11, which references the ODBC Driver 17

neclimdul commented 4 years ago

@yitam sorry. yeah that was perfect but I found that documentation here. I meant its not documented in the php extension install instructions here: https://docs.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15

neclimdul commented 4 years ago

oh no it is i just hadn't found it... ignore all the noise. Sorry everyone :(

StefanSchoof commented 4 years ago

Is there any work done to get the sql driver direct into official alpine packages, so that

RUN docker-php-ext-install pdo_sqlsrv

is enough?

yitam commented 4 years ago

No, @StefanSchoof , but please check this out: https://github.com/mlocati/docker-php-extension-installer

I wrote a simple shell script install_drivers.sh based on the latest ODBC instructions

curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.6.1.1-1_amd64.apk
curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.6.1.1-1_amd64.apk
printf '\n' | apk add --allow-untrusted msodbcsql17_17.6.1.1-1_amd64.apk
printf '\n' | apk add --allow-untrusted mssql-tools_17.6.1.1-1_amd64.apk
ln -sfnv /opt/mssql-tools/bin/* /usr/bin

Write a simple php script for checking client info:

<?php
$server = <host>;
$uid = <user>;
$pwd = <password>;
$conn = new PDO("sqlsrv:server=$server", $uid, $pwd);

$attr = $conn->getAttribute(constant('PDO::ATTR_CLIENT_VERSION'));
$version = substr($attr['DriverVer'], 0, 2);
print_r($conn->getAttribute(PDO::ATTR_CLIENT_VERSION));
unset($conn);
?>

This is the Dockerfile I've tested:

FROM php:7.4.9-fpm-alpine3.12
ADD install_drivers.sh /tmp
ADD pdo_client_info.php /tmp
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN chmod +x /tmp/install_drivers.sh && \
    /tmp/install_drivers.sh && \
    install-php-extensions sqlsrv pdo_sqlsrv 

This is the output when running it interactively: image

aniruddha-dnb commented 1 year ago

This is what I did on an alpine-based Dockerfile to install it:

RUN wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.1.1-1_amd64.apk && \
    wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.1.1-1_amd64.apk && \
    apk add --allow-untrusted msodbcsql17_17.5.1.1-1_amd64.apk && \
    apk add --allow-untrusted mssql-tools_17.5.1.1-1_amd64.apk && \
    apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS unixodbc-dev && \
    pecl install pdo_sqlsrv && \
    docker-php-ext-enable pdo_sqlsrv && \
    apk del .phpize-deps && \
    rm msodbcsql17_17.5.1.1-1_amd64.apk && \
    rm mssql-tools_17.5.1.1-1_amd64.apk

I'm about to test if it works, but it compiled with no errors.

Its works for me as well , but I'm getting this error. Any Idea ? SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired File: /var/www/vendor/faapz/pdo/src/Database.php

inri13666 commented 1 year ago

@aniruddha-dnb please provide MsSQL version and keep in mind that latest versions require latest version of libraries

more info here https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver16&tabs=alpine18-install%2Calpine17-install%2Cdebian8-install%2Credhat7-13-install%2Crhel7-offline#17

As well as I see no info about your connection, seems you have incorrect config and try to connect to host IP

Please provide the ping or telnet output from docker image to the host what you are trying to connect