0

Hello I've recently added my existing .Net 5.0 Webapi to a docker container and it cannot connect to SqlServer on my local machine which the connection string is

"ConnectionStrings": {
        "default": "Server=.\\SomeInstance;Database=TestDatabase;User Id=SomeUser;Password=SomePassword"
    }

The exact error that I get is:

'One or more errors occurred. (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: 25 - Connection string is not valid))'

When I change it to "Server=10.0.0.108\\SomeInstance;Database=TestDatabase;UserId=SomeUser;Password=SomePassword It works correctly.

How can I keep the connection string generic to work with multiple developers/machines and still use Docker.

I have tried using Server=localhost\\SomeInstance it does not work.

2
  • So you are running webapi docker container and you also running sqlserver on the same machine host and you are trying to connect from the webapi container to the host machine's SQL? Commented Jul 10, 2021 at 13:14
  • yes, that is correct Commented Jul 10, 2021 at 13:22

2 Answers 2

2

I was able to figure this out.

Server=host.docker.internal\\SomeInstance;Database=TestDatabase;UserId=SomeUser;Password=SomePassword did what i was trying to do.

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

Comments

0

It doesn't work because the container is running its own networking stack and cannot address the host by localhost (unless you are using host driver for networking).

If you would like to keep the connection string generic, what you can do is you can add a custom host entry to the /etc/hosts by running the container with the flag --add-host [Hostname]:[IPAddress] and then add the name in the connection string. This way you can manipulate the ip for the server to connect to at runtime.

Alternatively you can also use a special name host.docker.internal to address the host machine from the container.

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.