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

0

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

  • An SQL Server running in a Docker
  • An .NET Core 2.2 (IdentityServer) application running in a docker
  • An instance of jwilder.nginx-proxy serving as a reverse-proxy for every web application on the machine
  • A multitude of other .NET Core apps

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:

  • Using the --expose 20 command on the IdentityServer docker
  • Opening mapping the port 20 inside the container to some port outside -p 45264:20 in addition to the already exposed port 80
  • I originally worked on using the port 1433 on both sides of the mapping but since it didn't work I tried using an other port on the outside (20). Didn't change anything

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?

sql-server
docker
asp.net-core
asked on Stack Overflow Feb 25, 2019 by Mathieu VIALES • edited Feb 25, 2019 by Mathieu VIALES

4 Answers

1

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

answered on Stack Overflow Feb 25, 2019 by grapes
1

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.

answered on Stack Overflow Mar 4, 2019 by zigmund
0

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;"
answered on Stack Overflow Feb 25, 2019 by prisar • edited Feb 25, 2019 by prisar
0

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.

answered on Stack Overflow Apr 11, 2019 by Mathieu VIALES

User contributions licensed under CC BY-SA 3.0