DotNetCore Docker container could not connect to mssql - sql-server

I have containerized our DotNetCore application.
Our application code connects to different SQL Server instances
We are observing a weird error that our Application is not able to connect to a particular SQL Server instance.
Any connection trying to hit the SQL will go forever in Sending form.
I tried the below to identify the error.
Debug my Docker container locally and install SQL CMD to check if I am able to connect to SQL Server
Below is the error that is occurring
root#aabcd:/app# sqlcmd -S abcinstnacename,3431 -U username -p -Q "SELECT ##VERSION"
Password:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
SQL Server Version that is causing the error
Microsoft SQL Server 2016 (SP2-CU2-GDR) (KB4458621) - 13.0.5201.2 (X64)
In Docker file below are the base images I am using, will that cause any issue ?
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
Also tried adding below line in my Dockerfile with no luck!
RUN sed -i '/^ssl_conf = ssl_sect$/s/^/#/' /etc/ssl/openssl.cnf
Please share your thoughts on this

Related

Cannot connect to custom SQL Server docker image

we're currently using Windows containers and, as there are no official SQL Server images for Windows, we are building our own.
We've successfully created the image and by running via
docker run --name SQLServer2022preview -e ACCEPT_EULA=Y -e sa_password=PaSSw0rd -p 1433:1433 -d mycompany:winsrv1809-sql2022preview
I can connect and work as expected to localhost,1433. However, this image will act as a base and we need to build another image based on this on runtime. When doing so, is where it can't connect. We're getting this message
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2]. .
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. 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..
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; sqlcmd -i "C:\restore.sql"' returned a non-zero code: 1
This is the docker file
FROM mycompany:winsrv1809-sql2022preview
EXPOSE 1433
EXPOSE 4338
COPY ./databases/DB1.bak C:/
COPY ./HAS_Database/restore.sql C:/
RUN sqlcmd -i "C:\\restore.sql"
and restore.sql
RESTORE DATABASE DB1
FROM DISK = N'C:\DB1.bak'
WITH FILE = 1,
MOVE N'DB1_Imported'
TO N'C:\DB1.mdf',
MOVE N'DB1_log'
TO N'C:\DB1_log.ldf',
NOUNLOAD,
STATS = 5;
GO
There's no login errors in sql log looking into the container (which automatically closes after aprox 30 sec) so I guess that it may be relative to port forwarding. Can anyone help?
I tried port forwarding in the start step, with no luck. I'm a docker newbie

SQL Server access via SSH tunnel: SSL Provider error certificate verify failed

I have set up an SSH tunnel to an SQL Server instance running on AWS RDS. I was using Microsoft ODBC Driver 17 for SQL Server driver. My trouble was that I could not reliably login to the database. Sometimes I get login timeouts.
I upgraded the driver to Microsoft ODBC Driver 18 for SQL Server and now I can't connect at all and I immediately get this error.
File "/Users/me/.venv/lib/python3.10/site-packages/mssql/base.py", line 353, in get_new_connection
conn = Database.connect(connstr, **args)
django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: [error:0A000086:SSL routines::certificate verify failed:unable to get local issuer certificate] (-1) (SQLDriverConnect)')
I feel like maybe I have never been connecting to the DB properly but only since the new driver do I get a proper error.
I use the app Core Tunnel to do the SSH command to start the tunnel on my local Mac. The command it uses is:
ssh -N -i "/Users/me/Library/Group Containers/E2343S7W4U.io.coressh.ssh/.ssh/privatekey/mysshkey"
-L localhost:1433:my-database.rds.amazonaws.com:1433
-o ServerAliveInterval=15 -o ExitOnForwardFailure=yes
-o ServerAliveCountMax=3 -p 22 eric#12.34.56.78
where 12.34.56.78 is the public IP of the EC2 instance that has access to the database.
After the tunnel is running, I point my Django app to localhost:1433 to try to access the database and that is when the error occurs. Like I said, this setup worked with the old driver but wasn't reliable, and the login timeout error it would sometimes give was on the same line of mssql/base.py.

Which port should I use for SQL Server on Docker with runner in bitbucket?

I'm using a python-mssql image to restore a .bak file on Bitbucket. This is the yaml file that I create:
image: diegonachon/mssql_python_sqlcmd
env:
- ACCEPT_EULA = Y
- SA_PASSWORD = 5tr0ngP455
pipelines:
custom:
run:
- step:
name: restauration
runs-on:
- 'self.hosted'
size: 8x
script:
- pip install pysftp
- pip install pyodbc
- pip install psycopg2
- mkdir /bak_db/
- python dowload_bak_file.py
- python restore_file.py
In download_bak_file.py I download a .bak file that I'll restore. Then, in restore_db.py, I restore it with this code:
import os
os.system(''' sqlcmd -S localhost -U SA -Q'''
''' "Restore DATABASE data_base FROM DISK = '/bak_db/data_base.bak' \
WITH MOVE 'data_base' TO '/bak_db/data_base.mdf', \
MOVE 'data_base' TO '/bak_db/data_base.ldf' " \
-P 5tr0ngP455 ''')
But I get these errors:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AF9.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. 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.
What I've seen, is a port problem, so I assume that I should change "localhost" with something else. Is it or I'm missing something?
Never use localhost in docker configs. In dockers, localhost is inside the docker container itself. If you want to connect to a database, you should use its service name or machine IP.

How to connect to SQL Server Developer (windows edition) in a container on Win10 from outside the container

I got the docker image for SQL Server Developer edition and started it up:
PS C:\WINDOWS\system32> docker ps -aa
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81347866fc2b microsoft/mssql-server-windows-developer "powershell -Command…" About an hour ago Up About an hour (healthy) 0.0.0.0:1433->3341/tcp trusting_mccarthy
PS C:\WINDOWS\system32>
It's listening on port 3341 as you can see. the IP is:
PS C:\WINDOWS\system32> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 8
172.17.234.173
HOwever I cannot connect to it from the host:
PS C:\WINDOWS\system32> sqlcmd -S 172.17.234.173,3341
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. 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..
Must be doing something wrong. any hints?
PS: I can't connect to 1433 either:
PS P:\> sqlcmd -S 172.17.234.173,1433
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: An attempt was made to access a socket in a way forbidden by its access permissions.
.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. 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..
PS P:\>
Docker Run command used:
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" `
-p 1433:1433 --name sql1 `
-d mcr.microsoft.com/mssql-server-windows-developer
note I've started with 1433:3341 as well. No joy either way

Connect host machine's SQL Server 2017 from homestead vagrant

I'm trying to connect my local/host machine SQL Server from Laravel vagrant homestead. I've been googling all day trying to connect the host machine's database installing different PHP packages but so far have no lucks. I thought I would remove PHP at this point and try to establish the connection inside homestead using sqlcmd instead.
So, I have installed sqlcmd in homestead and tried to execute the following command to connect to the host machine's database.
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
However, I received the following error:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection because an error was encountered during handshakes before login. Common causes include client attempting to connect to an unsupported version of SQL Server, server too busy to accept new connections or a resource limitation (memory or maximum allowed connections) on the server..
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2746.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Client unable to establish connection due to prelogin failure.
However, If I use sqlcmd on my host machine and execute the following command, I am able to access the database.
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In my Homestead.yaml, I have set up port forwarding from:
ports:
- send: 1433
to: 1433
You say this doesn't work:
sqlcmd -S 10.0.2.2,1433 -U sa -P 'password' -Q "sp_databases"
Yet this does:
sqlcmd -S LAPTOP-ACQES\MSSQL2017 -U sa -P Asdf1234 -Q "sp_databases"
In the former, you are using the IP, but no instance name.
Try:
sqlcmd -S 10.0.2.2\MSSQL2017 -U sa -P 'password' -Q "sp_databases"
Also:
Ensure TCP/IP is enabled in SQL Network Configuration.
Verify SQL Server Browser is running.
Allow remote connections in SQL Server properties.
Lock SQL Network configuration to port 1433 on all adapters, and disable dynamic ports (then restart services).

Resources