Search code examples
phpsql-serverdockerfiledebian

DockerFile PHP8.1 Debian SQL Server


I had some trouble with my DockerFile, I have to create a docker image with php8.1, Linux and SQL Server. I made the following Docker, but I can´t make a connection to my DB, for example if I want to use the slqcmd command, I have to edit bash, then if a try a connection doesn´t work, just give me this error

Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to 3332a686a938. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

I just starting in this, so any help is welcome

FROM debian:11

ENV DEBIAN_FRONTEND noninteractive

# ESTABLECER ZONA HORARIA
ENV     TZ=America/Santiago
RUN     ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

#Install update
RUN apt-get update
RUN apt-get upgrade -y

#Install facilitators
RUN apt-get -y install locate mlocate wget apt-utils curl apt-transport-https lsb-release \
             ca-certificates software-properties-common zip unzip vim rpl sendmail

ENV     PHP_MAX_SIZE="50M"

# Fix ‘add-apt-repository command not found’
RUN apt-get install software-properties-common

RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list

#Install update
RUN apt-get update

# Set Timezone
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
    && apt-get update \
    && apt-get install -y --no-install-recommends tzdata \
    && dpkg-reconfigure --frontend noninteractive tzdata

#intall Apache + PHP
RUN apt-get -y install apache2 libapache2-mod-php8.1 php8.1 php8.1-cli php8.1-common php8.1-opcache

#OpenSsl
RUN     apt-get update && \ 
    apt-get install -y libssl-dev

#PHP Install CURl
RUN apt-get -y install curl php8.1-curl

#PHP Intall DOM, Json, XML e Zip
RUN apt-get -y install php8.1-dom php8.1-xml php8.1-zip php8.1-soap php8.1-intl php8.1-xsl

#PHP Install MbString
RUN apt-get -y install php8.1-mbstring

#PHP Install GD
RUN apt-get -y install php8.1-gd

#PHP Install PDO SqLite
RUN apt-get -y install php8.1-pdo php8.1-pdo-sqlite php8.1-sqlite3

#PHP Install PDO MySQL
RUN apt-get -y install php8.1-pdo php8.1-pdo-mysql php8.1-mysql 

#PHP Install PDO PostGress
RUN apt-get -y install php8.1-pdo php8.1-pgsql

## -------- Config Apache ----------------
RUN a2dismod mpm_event
RUN a2dismod mpm_worker
RUN a2enmod  mpm_prefork
RUN a2enmod  rewrite
RUN a2enmod  php8.1

# Enable .htaccess reading
RUN LANG="en_US.UTF-8" rpl "AllowOverride None" "AllowOverride All" /etc/apache2/apache2.conf

## ------------- LDAP ------------------
#PHP Install LDAP
RUN apt-get -y install php8.1-ldap

#Apache2 enebla LDAP
RUN a2enmod authnz_ldap
RUN a2enmod ldap

## ------------- Add-ons ------------------
#Install GIT
RUN apt-get -y install -y git-core

#PHP Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

#PHP Install PHPUnit
#https://phpunit.  de/announcements/phpunit-9.html
RUN wget -O /usr/local/bin/phpunit-9.phar https://phar.phpunit.de/phpunit-9.phar; chmod +x /usr/local/bin/phpunit-9.phar; \
ln -s /usr/local/bin/phpunit-9.phar /usr/local/bin/phpunit

RUN apt-get -y install php8.1-dev php8.1-xml php8.1-intl

ENV ACCEPT_EULA=Y

RUN curl -s  https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl -s  https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list

RUN apt-get update

RUN apt-get install -y --no-install-recommends \
        locales \
        apt-transport-https \
    && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
    && locale-gen

# install MS ODBC 18
RUN apt-get -y install msodbcsql18 mssql-tools18

RUN echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bash_profile
RUN echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
RUN exec bash

RUN apt-get install -y --allow-downgrades odbcinst=2.3.7 odbcinst1debian2=2.3.7 unixodbc=2.3.7 unixodbc-dev=2.3.7

#RUN apt-get -y install unixodbc unixodbc-dev
RUN apt-get -y install gcc g++ make autoconf libc-dev pkg-config

##------------ Install Drive 5.10.1 for SQL Server -----------
RUN apt-get install php-pear
RUN pecl -vvv install sqlsrv-5.10.1
RUN pecl -vvv install pdo_sqlsrv-5.10.1

#instalacion de nano
RUN apt-get install nano

#For PHP CLI
RUN echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
RUN echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini

#For PHP WEB
RUN echo "extension=pdo_sqlsrv.so" >> /etc/php/8.1/apache2/conf.d/30-pdo_sqlsrv.ini
RUN echo "extension=sqlsrv.so" >> /etc/php/8.1/apache2/conf.d/20-sqlsrv.ini

#Config Apache
RUN a2dismod mpm_event
RUN a2enmod mpm_prefork
RUN a2enmod php8.1

## ------------- Finishing ------------------
RUN apt-get clean

#Creating index of files
RUN updatedb

EXPOSE 80
EXPOSE 443
CMD apachectl -D FOREGROUND


Solution

  • Ok I finnally find the solution, just a fix, the openssl.cnf doesn´t match with te permition that sql server require, so… i added “TrustServerCertificate” => true in my query on php, and it works thats the same if i put in the console -C at the end of my connection setting, like this:

    ./sqlcmd -S host -U user -P password -d database -C