2

I'm currently working on a .NET Core project. When I run the application on my local machine (Windows) I can connect to a SQL Server database using my windows credentials.

I am attempting to shift into using a devcontainer within Visual Studio Code, but when I run the same application in the container (Linux) I get an error:

An exception of type 'Microsoft.Data.SqlClient.SqlException' occurred in Microsoft.Data.SqlClient.dll but was not handled in user code:

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: 35 - An internal exception was caught)

I'm not sure where to go from here. What did I miss?

3
  • Does this answer your question? Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"? Commented Mar 4, 2020 at 16:49
  • 1
    @Igor: No. Like I said, from within windows (host) the code runs perfectly fine. Within my container (linux) something is not set up properly and it fails. Commented Mar 4, 2020 at 17:04
  • 2
    You may need to use SQL authentication rather than Windows authentication. Windows authentication only works on the same machine or machines joined to the same domain or forest. Putting code in a container effectively puts on a different machine or domain. Commented Mar 4, 2020 at 17:10

1 Answer 1

0

I encounter the same problem with a devcontainer where one container runs mssql server and the other the application. I can not add a connection to SQL Server in VSCode, nor the application can establish a connection.

SSMS can connect after I have port 1433 published for the database container, but the application container still fails.

A quick fix to get a tutorial working, is to use the IP address of the database container:

Data Source=172.20.0.2,1433;Initial Catalog=Beam;Integrated Security=False;User ID=sa;Password=@#^fcIen&*asd;TrustServerCertificate=True

The issue is that when a container tries to access localhost, just like any other machine it's trying to communicate with itself.

Within Docker Compose networks, use the service name or a custom host name to communicate with other containers as explained in this answer.

A related solution is to use the syntax <ip>,<port> in the connection string, explained here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.