Mathieu VIALES
Mathieu VIALES

Reputation: 4772

Can't reach docker-hosted SQL server from web app docker on same host

On the same virutal machine (remote, ubuntu), I have

I am able to connect to all of my websites using both machine IP + port and domain name, which means the reverse proxy works as expected and the dockers are well-configured

I am able to connect to the SQL Server using SSMS from my local machine, which means that the SQL Server docker properly forwards the TCP connection on port 1433

The IdentityServer .NET Core 2 web application is able to connect to the SQL Server when run on my local machine.

The remote-docker IdentityServer application can't reach the SQL Server instance with the following error (shortened for clarity - removed stack trace)

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 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) at [...]

I know that the SQL server is running and reachable from the internet, and I know that the application's code is not at fault because I tested both.

So I deduced it had to be the IdentityServer docker that was blocking the connexion. So I tried:

Here is the connexion string used by the IdentityServer (sensitive data hidden):

Data Source=***.***.***.***,20;Initial Catalog=Identity;Persist Security Info=True;User ID=**;Password=******************

Why can't my IdentityServer docker reach the SQL Server docker while the SQL Server itself is perfectly reachable? How can I make this setup work?

Upvotes: 0

Views: 519

Answers (4)

Mathieu VIALES
Mathieu VIALES

Reputation: 4772

I ended up giving up on getting this to work using a single host, so I simply decided to have the SQL Server run in a separate machine.

Upvotes: 0

zigmund
zigmund

Reputation: 61

As I understood you run Sql Server and IdentityServer (which has connection problem) in separate docker containers.

If this is so then referring to localhost (i.e. 127.0.0.1) is not correct. Because in this case IdentityServer tries to connect to itself. This would work if the IdentityServer have run on the host machine, since you forward SQL server port to it. But in your case, you should connect to the SQL server container IP instead. Considering all above I see three options for you to solve this:

  1. You can get ip address of SQL Server container by running docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <sql_container_name_or_id>
  2. Run SQL container with static IP via docker run --ip <static_ip_value> <sql_container_name_or_id> and then use static ip you have specified in connection string.
  3. Run SQL container with specified host name via docker run --hostname <sql_host_name> <sql_container_name_or_id> and then use specified hostname in the connection string.

It is up to you which way to go.

Upvotes: 1

prisar
prisar

Reputation: 3195

Use tcp, 127.0.0.1 and host port to connect. Mention in the identity server docker settings that it depends on sql database server container. Like this,

identityservice:
   ...
   depends_on:
      - sqldataservice

This way the database container will be made available first.

"ConnectionString": "Server=tcp:127.0.0.1,8433;Database=dbname;User Id=sa;Password=abc@1234;"

Upvotes: 0

grapes
grapes

Reputation: 8636

When wrapping SQL server into Docker, the first thing to anticipate is the way you connect. SQL Server prefers named pipes and you have to explicitly set mode to tcp.

If connection is done locally, don't use localhost, change it to 127.0.0.1. Also writing explicit tcp: prefix may help, like this: Server=tcp:x.y.z.q,1433

Upvotes: 1

Related Questions