C
C#3d ago
jiheielf

Docker Container Can't connect to remote SQL Server

Currently I have a Worker service that will interact with a sql server on a remote server. when it is deployed to my local machine, it works. But once I deploy it as a docker container, it fails the connection and printout this error
Main loop Exception: Microsoft.Data.SqlClient.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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
Main loop Exception: Microsoft.Data.SqlClient.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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
I have tried to search online from stackoverflow and in docker community, I have checked the firewall and tried different combinations compare to their post but it still did not work out...
27 Replies
taner.
taner.3d ago
R u using the localhost in the connection string?
jiheielf
jiheielfOP3d ago
No it is a remote sql server, have to connect it using the ip address
taner.
taner.3d ago
Okay is the ip correct from the container? And r u using WSL for it? Or via powershell?
jiheielf
jiheielfOP3d ago
Sorry I'm a beginner on Docker, what does that mean?
taner.
taner.3d ago
Like from where do u start ur container?
jiheielf
jiheielfOP3d ago
from my windows terminal, when I run the container I set the --network = host and it is a linux container
taner.
taner.3d ago
Stack Overflow
Connect to mysql in a docker container from the host
In a nutshell I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '...
jiheielf
jiheielfOP3d ago
Let me have a check Sorry just to double check if I didn't get it wrong, does that mean I need to firstly set up the connection from the container internal linux environment to be able to connect to the remote sql server or the remote ip address and then my program as a container can then be able to using EF core to interact with it?
taner.
taner.3d ago
u connect to the Linux environment with the ip address And user name and pwd that u defined in the docker container U don’t connect directly with the ms sql server in the container U connect with the container direct So in the connection string u need to define the ip from the Linux environment with the port E.g. 111.111.111.111,1433
jiheielf
jiheielfOP3d ago
Sorry I didn't understand it, in my code the connection string is this "Data Source=xxx.xx.xxx.xx,1433;initial Catalog=xxxx;User ID=xxxx;Password=xxxxxx;MultipleActiveResultSets=true" but the Data Source should be another format that needs to be compatable in the container's internal linux environment? Or does it mean I need to start up a sql server container that connects to the remote server and then connect my program onto this new sql server container...?
taner.
taner.3d ago
Medium
Executing a SQL Server Docker Container and connecting to a .Net
In this article, I will show you how to create a SQL Server docker container and how to use it in the .Net project with DDD created in…
taner.
taner.3d ago
i made a mistake im running my containers in linux subsystem thats why i need the ip from the enviroment look at this guide
jiheielf
jiheielfOP3d ago
Hi, I have just checked this, but this exmaple seems trying to make a new container that connects to a local sql server rather than a remote one. My remote sql server is running in a windows server on another machine...
taner.
taner.3d ago
ohh okayyy is the server in the same network?
jiheielf
jiheielfOP3d ago
Yes
taner.
taner.3d ago
u have a windos server and at that server u are running a docker container?
jiheielf
jiheielfOP3d ago
Sql server is on a windows server, docker is held on another windows11 machine but in the same network Containers are Linux contianers
taner.
taner.3d ago
and why r u using the docker container? what is the reason behind this setup i need the details
jiheielf
jiheielfOP3d ago
Company's decision..
taner.
taner.3d ago
u can just connect direct to the windows server the benefit for the docker is unclear for me unless u want to run it on a diff os
jiheielf
jiheielfOP3d ago
But I'm getting error when I try to connect to the sql server from the container...
taner.
taner.3d ago
then the firewall blocks the connection
jiheielf
jiheielfOP3d ago
I think I have configured it already, otherwise the program I deployed locally shouldn't be able to connect to it..?
taner.
taner.3d ago
double check it can u ping the ip from the windows server from ur container?
jiheielf
jiheielfOP3d ago
When I go into the terminal of the container I wasn't able to ping to the ip, but when I do it in on my docker host machine, it was all fine
taner.
taner.3d ago
Stack Overflow
How to get a Docker container's IP address from the host
Is there a command I can run to get the container's IP address right from the host after a new container is created? Basically, once Docker creates the container, I want to roll my own code deploy...
jorgedotnet
jorgedotnet3d ago
Maybe in your Windows11 you need to manually type your windows server host addressC:\Windows\System32\drivers\etc

Did you find this page helpful?