.Net Core Docker Image cannot access database when running in docker

1

I have a dotnet core application that tries to access to the database, when I run it in visual studio it works fine (probably because of my domain authentication) but when I try to build docker image and run it with docker run -it --rm -p 8080:80 --name console console

I'm getting this error, but I don't really understand why? Does anyone know how I can fix this?

The connection string looks like this: Data Source=DBTest; Initial Catalog=test;Integrated Security=True

it works fine locally on my machine when I run through visual studio.

Error:

System.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 acces sible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 0 - Access is denied.) ---> System.Com ponentModel.Win32Exception (5): Access is denied

c#
docker
asked on Stack Overflow Mar 15, 2019 by (unknown user) • edited Mar 15, 2019 by ingvar

2 Answers

2

You are (running) docker container does NOT understand connection strings like

localhost

localhost\MyInstance

You need something like this:

Data Source=host.docker.internal:1433;DatabaseName=MyDatabase;

or

"server=host.docker.internal\MyInstance;database=MyDatabase;"

You may need to use sql authentication as well, to jump "from docker" to your local machine.

You have to treat the docker running images AS ANOTHER MACHINE, so if you have a localhost\MyInstance sql server, you'll have to open up remote tcp connections to it as well.

General rule of thumb.

A container running on your machine...is NOT like local code..when it comes to hitting things like a db-server, local-sftp, etc.

If you do not use sql-authentication, you'll probably get an error like this:

Cannot authenticate using Kerberos. Ensure Kerberos has been initialized on the client with 'kinit' and a Service Principal Name has been registered for the SQL Server to allow Kerberos authentication.
ErrorCode=InternalError, Exception=Interop+NetSecurityNative+GssApiException: GSSAPI operation failed with error - Unspecified GSS failure.  Minor code may provide more information (SPNEGO cannot find mechanisms to negotiate).

BONUS:

Below is a bookmarked article I have for allowing remote connections to your LOCAL machine.

https://knowledgebase.apexsql.com/configure-remote-access-connect-remote-sql-server-instance-apexsql-tools/

answered on Stack Overflow Mar 15, 2019 by granadaCoder • edited Jul 16, 2020 by granadaCoder
0

The error message already gives you a good hint. The client application that you are spawning in a container named 'console' is either not in the same network as your database instance or your MSSQL server is rejecting remote connections.

See Matt. G's comment how to configure MSSQL to accept connections from remote clients.

Because you didn't specify a network when executing docker run the container is implicitly connected to your host by a bridge network interface. You can inspect the IP address of a running container with:

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
# more verbose:
# docker inspect <container_name>

You should also double check your connection string to actually point to the system hosting your database. In your case probably the IP address of your physical computer/docker host.

answered on Stack Overflow Mar 15, 2019 by Yannic Hamann • edited Mar 16, 2019 by Yannic Hamann

User contributions licensed under CC BY-SA 3.0