Cannot access external database hosted on a server from within the container


I have created a .Net Core app and I have generated a Dockerfile using Visual Studio. The app is able to access databases hosted in a server on cloud when the ip address username and password are provided to it, when I run it using Visual Studio 2015.

But the problem arises when I try and use the same app in a Docker Container.

The app inside the container is unable to find the ip address for some reason even though I am able to ping the server from my Docker quick start terminal.

The error I get when I inspect the container goes something like this

“A network-related or instance-specific error occurred while establishing 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”

I get this error when I try to connect when the app is in the container but the app is able to connect to the sql server when the app is running inside VS 2015

So is there any solution where I can check if the container is able to access SQL Server. If so what is it?

If an example can be provided about how we can get containers to access servers outside it would be of great help.

Thanks.

Are you saying your docker container and database are on the same server, and the docker container cannot access the DB? If that it’s the case make sure you are using the actual IP address of the server they are both on and not any local address.

Also try doing a
docker exec -it $docker_app_container_name bash
To get a shell inside your docker container. Once there try pinging the db

Hi Thank you for your reply.

The Container is hosted on Dockerquickstart terminal and the DB is a Microsoft SQL server that is hosted on a different IP address so what the app does is connects to the MSSQL server when the credentials as shown in the picture are provided. I will try pinging the DB from inside the container and let you know

Thanks again

Edit:

I have tried pinging and I am getting a response but for some reason I am unable to connect to the Database from the app itself when I am running it on my host machine

Hi I have resolved this issue, the problem seems to occur when the server name is provided in this format

Servername : 25.25.25.25\SQL2008

but when I provide the app with the format below it seems to work, any idea why it is so

Servername : server1 #Without the “”

If I put in the server name without backslash it seems to be able to connect.

Thanks for your help

Hi glad you figured it out. I have 0 experience with SQL server. But some quick googling suggested that the \something part can only be done locally. Meaning from your own computer you can do ip\something but from a container, even if it is ‘on your computer’, from inside the containers point of view it acts and seems your computer as a seperate entity. So you do not need that last bit. Just a guess. Really not familiar with SQL

Read this link:

https://docs.microsoft.com/en-us/sql/relational-databases/lesson-2-connecting-from-another-computer
In resume, to access non default instance of Sql Server from network you must access using the port number.

You will find the port number in SQL Server Configuration Manager.

You must enable remote connections in your SQLServer instance and enable incoming connections to the port in Windows Firewall.

To be able to connect, you must provide de data base name like “iporname,port” in connectionstring.