Search code examples
dockerasp.net-corenetwork-programmingsql-server-2016

Connect ASP.NET Core container to remote SQL Server


I am trying to connect my ASP.NET Core application to a remote SQL Server.

The application is deployed using IIS WScore 2016 image. When I run the application on my host, it's working, but in the container using this connection string :

Data Source=xx.xxx.xx.xx,1433;Initial Catalog=somedb;User Id=xxxxxx;Password=xxxxx;

or:

Server=xx.xx.xx.xx,PORT_NB;Database=DATABASE;User Id=USER;Password=PASSWORD

But no luck - I am using the default Docker network.

The error is like this:

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 - No such host is known.)

Notes:

  1. The server is allowing the remote connection
  2. All the connection strings are tested and can connect to the remote SQL Server from the application that runs on the host
  3. I have read the Docker documentation and they mentioned the IP forwarding but the example was on linux containers and I did not find any help about connecting the Windows containers to a remote SQL Server

Question

My concrete question is how to expose the container to the outside world and I can connection my container to the other remote services like a remote SQL Server?

Should I use host network or bridge with the IP forwarding?

Any help? Thanks


Solution

  • I changed the connection string to this :

     "ConnectionString": "Server=xx.xxx.xx.xx\\MSSQLSERVER,1433;Initial Catalog=Dbname;User Id=username;Password=xxxxxxx;"
    

    then i restarted the AppPoll and it worked