Reputation:
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
Upvotes: 3
Views: 2857
Reputation: 27904
You are (running) a docker-image .. and the (running) docker-image does NOT understand connection strings like
localhost
localhost\MyInstance
You need the docker-running-image to be able to talk to your local-setup. You could use the IP address of your local machine. BUT fortunately, docker has a built in ALIAS for "the host machine that spawned me (the docker image)".
I want to connect from a container to a service on the host The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.
Below shows that alias, and how it would be used for a Ms-Sql-Server connection string.
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. (The docker-running-image will NOT understand "integrated security" in the local developer understanding of integrated-security)
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.
Upvotes: 5
Reputation: 5235
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.
Upvotes: 0