Dockerfile php8.1 sqlserver debian

Hi everyone, i had some trouble with my dockerfile, i have to create a docker image with php8.1, linux so and sql server.
I made the follow docker, but I can´t make a conection to my DB, for example if i want to use de slqcmd command, i have to edit bash, then if a try a conection 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

and sorry for my english :slight_smile:

FROM debian:11

ENV DEBIAN_FRONTEND noninteractive

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


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

RUN wget -O /etc/apt/trusted.gpg.d/php.gpg
RUN echo "deb $(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

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 | 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; 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


RUN curl -s | apt-key add - \
    && curl -s > /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

RUN echo >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
RUN echo >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini

RUN echo "" >> /etc/php/8.1/apache2/conf.d/30-pdo_sqlsrv.ini
RUN echo "" >> /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


Please, format your post according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

Example code block:

echo "I am a code."
echo "An athletic one, and I wanna run."

1 Like

How do you try to access SQL server and where is it running? I don’t see it in the Dockerfile, but if it is there, it shouldn’t be. A container is not a virtual machine so it won’t run in the php container so easily. And shouldn’t.

For PHP, you should use the official PHP image and run sqlserver in a separate container.

I use sqlcmd with My credencial, and the console send me that error in the first post :confused:

That doesn’t answer my question. Since we don’t know where your SQL server is running, on what IP address it is lisstening and how exactly you use sqlcmd to connect to it (remotely, locally) we can’t intepret the error message.

Ohh i see, sorry .
I have a server running with sql server, i can access from other images to that DB, actually i have a other images, but is older and i need to upgrade that, in that image i have a system that consume data, and if i go to the console and try to conect with sqlcmd i don’t have any problem.

So, with the new image i cant do anything :confused: doing the same things.

I cant stay with the older version cause doesnt have support and have a lot of critical vunerabilties.

I’m really new in the Dockers world :sweat_smile:

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

maybe this gonna help someone