Connect Azure Database through docker windows container - sql-server

I have a .netframework 4.7.2 application with the azure database. I have created a docker windows container for this application. When I launch this application in the Docker container on my local machine, it gives an error:
[Win32Exception (0x80004005): The network path was not found]
[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
ConnectionString:
connectionString="Data Source=tcp:servername.database.windows.net,1433;Database=databasename;User ID=testuser;Password=testpassword;Trusted_Connection=False;MultipleActiveResultSets=True;Encrypt=True" providerName="System.Data.SqlClient"
Docker File:
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
ARG source
WORKDIR /inetpub/wwwroot
COPY ./bin/Release/Publish/ .
My IP Address is already added in the azure database and when I connect through the SQL management studio from the local machine it works without any issue. Also, It works fine
with visual studio.
I believe my docker has internet access. When I ping google.com inside the docker container, it returns the result
ping google
C:\inetpub\wwwroot>ping google.com
Pinging google.com [172.217.166.14] with 32 bytes of data:
Reply from 172.217.166.14: bytes=32 time=19ms TTL=118
Reply from 172.217.166.14: bytes=32 time=19ms TTL=118
Reply from 172.217.166.14: bytes=32 time=19ms TTL=118
Reply from 172.217.166.14: bytes=32 time=19ms TTL=118
Ping statistics for 172.217.166.14:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 19ms, Maximum = 19ms, Average = 19ms
C:\inetpub\wwwroot>
Can someone guide me on how can I connect my application to the Azure database inside the docker container?

Related

netsh port proxy doesn't always work for SQL Server

I have a Mac running Windows 10 21H1 in Parallels. On macOS I have SQL Server running in a Docker container, exposed on port 55556.
In Windows I'm trying to forward localhost,55555 to the SQL Server in Docker.
For this I tried netsh interface portproxy add v4tov4 listenaddress=localhost listenport=55555 connectaddress=10.211.55.2 connectport=55556, but this only works when connecting in DataGrip (in the Windows VM, using a JDBC driver); I can see all the databases, query them, etc.
SQL Management studio always gives the following error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - The wait operation timed out.) (Microsoft SQL Server, Error: 258)
Is there something about the driver that SQL Management studio uses that ignores this port forwarding?
I figured it out: portproxy with listen address localhost doesn't work, but 127.0.0.1 does.

Connect IIS in DMZ to SQL Server on intranet

I'm trying to connect IIS which is running on a server in the DMZ to a SQL Server instance on the company intranet. But, I'm getting the following error:
An unhandled exception occurred while processing the request.
Win32Exception: Access is denied.
Unknown location
SqlException: A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Do I need specific ports opened up on one of the servers? Or is there something else I need to do?
Considering your SQL server is running, start with the networking. On which port is your SQL instance listening (default 1433)?
If you don't know, open the SQL Server Configuration Manager (on the SQL Server). Navigate to the SQL Server Network Configuration > Protocols for %instancename% > TCP/IP > IP Addresses. Then, test if something is blocking this connection; on your IIS in PowerShell:
Test-NetConnection -ComputerName sqlservername -Port 1433
If this doesn't work, check the Inbound Rules for Windows Firewall on your SQL Server. Make sure you work on the right network profile and eventually speak to the one managing your network systems in your company.
Maybe your IIS can't resolve the SQL Servers hostname? Try using the IP and check your DNS configuration.
There might also be something off with your connection string. Mind posting it here?

How do I connect to a local Windows SQL Server Instance from WSL?

I'm new to WSL, and running Ubuntu. I have a local instance of SQL Server 2017 installed in Windows, and want to connect to it from WSL. I have remote connections enabled, however, I can't seem to get connected from ubuntu locally.
I installed the db tools for ubuntu, and I'm using sqlcmd:
sqlcmd -S localhost -U sa -P <my password>
This keeps failing. How do I format/configure this to allow SQL Server in Windows to be available to Ubuntu?
Thanks!
EDIT
I'm using the default instance of SQL Server
Here's the error I'm getting
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 0x2749.
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..
Pinging localhost:
$ ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=128 time=0.248 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=128 time=0.497 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=128 time=0.517 ms
Launch SQL Server Configuration Manager
Click on SQL Server Network Configuration
Right click on TCP/IP and click "Enable"
Go to your Services and Restart SQL Server
The solution provided by TheTFo for me did not fully work. I had to take it a step further by ensuring that the appropriate IP Address was enabled as well. Refer to Step 6 of this resource for instructions on how to do so.

I created a mssql-server-linux container in Docker, but I can't connect from outside the container

I created a mssql-server-linux container in Docker that maps to port 4000 on the host.
Now, I'm connecting to this database, but I'm having an error.
How can i solve this problem?
Error message
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or could not be accessed. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (provider:named Pipes provider, error:40-Unable to open connection to SQL Server)
enter image description here
I suggest to check your server port in iptables if it is in ACCEPT state. Maybe your server is blocking or rejecting others.
And try to ping your server ip to check if you can reach the server.
This is the first troubleshooting when i tried connecting Docker containers in MongoDB.
Thanks!

What would prevent code running in a Docker container from connecting to a database on a separate server?

I have a .NET Core 1.1 app running in a Docker container on Ubuntu 14.04, and it fails to connect to the SQL Server database running on a separate server.
The error is:
Unhandled Exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 25 - Connection string is not valid)
I have deployed the same image with the same command line on another Ubuntu 14.04 server, and it connects fine.
A console app running on the problem server (outside of Docker) can connect with the same connection string.
As far as I can see from the documentation, an app running in a container has access to the external network by default, so what could be blocking this connection?
an app running in a container has access to the external network by default
It could have access only if a valid IP address is assigned to the container. Sometimes the IP which Docker choose for the container can conflict with external networks.
By default, containers run in bridge network, so look at it:
docker network inspect bridge
Find the container and check its IP.
To resolve conflicts, it is possible to customize bridge network and set bip parameter to change the network's IP range (config file location depends on host's OS):
"bip": "192.168.1.5/24"
Or create a new docker network.
Or experiment with net=host option: docker run network settings
Does this help?
Connect to SQL Server database from a docker container
Also, Googling this "docker connect to sql server database" seems to return a lot of helpful results.

Resources