0

I'm building a dotnet app using Rider and Docker under macOS. For the database, I use sqledge docker image. If I start the service and send a request, I receive a successful response. When I containerize the dotnet app, I receive the following error:

    An error occurred using the connection to database 'FundraiserAPI' on server 'localhost'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'Raiserflow.CampaignOrgAPI.DbContext.FundraiserDbContext'.
      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: Could not open a connection to SQL Server)

It's a common error that says that my dockerized service doesn't have access to the SQL container.

One of the ways to fix the error is to Enable TCP/IP port

As macOS doesn't support SQL Server Configuration Manager, do you know any other alternatives to enable the TCP/IP port of a SQL server in a docker container?

Attaching a screenshot of my 2 containers and the ports they're running.

enter image description here

And my connection string:

"DefaultConnection": "Server=localhost;Database=FundraiserAPI;Initial Catalog=FundraiserAPI;User=sa;Password=123123;MultipleActiveResultSets=True;"

Or if you have any other alternative solution, that would be great!

  • 1
    You need to give the `sqledge` container a [routable IP](https://stackoverflow.com/a/43244221/61305) so that the other container can reach it, and use that IP in your connection string instead of `localhost`. Think of each container as its own entity, with its own range of ports, that should make it clearer why `localhost` doesn't make sense from inside a container - the call is coming from inside the house! To any container, `localhost` can't possibly _also_ represent a _different_ container on the host. – Aaron Bertrand Feb 09 '23 at 13:13
  • Actually [this is probably a much better link](https://stackoverflow.com/a/41437077/61305). – Aaron Bertrand Feb 09 '23 at 13:22
  • Your answer makes me think if the setup will be much more efficient if I use Kubernetes and add these 2 containers in one common Pod. – Stanimir Yakimov Feb 09 '23 at 13:30
  • Possibly. I can barely spell kuberwhatever. – Aaron Bertrand Feb 09 '23 at 13:35
  • Avoid avoid... k8s is another level of difficulty, it won't solve your `localhost` problem. In Docker multiple-container orchestration of "applications" is best handled by way of compositions (see docker-compose.yml files) where containers are grouped together inside a Docker network and can use that network's DNS services to refer to each other by name. So, in a composition, your API could connect using `Server=tcp:sqledge,1433;`. But because you have standalone containers here you'll want to try the special name for the Docker host itself, `Server=tcp:host.docker.internal,1433;`. – AlwaysLearning Feb 09 '23 at 13:36

0 Answers0