Pankaj Singh

Pankaj Singh

  • NA
  • 348
  • 94.8k

Not able to connect Sql Server(Local) from App in Docker Container

Oct 26 2020 9:23 AM
I have created a simple .NET Core 3.1 Console app to connect to SQL Server using ADO.NET. It is working fine with dotnet run and returns the result. But when I deployed this app on docker, I get a network-related error.
 
My console app code is as follows:
  1. using System;  
  2. using System. Data;  
  3. using System.Data.SqlClient;  
  4.   
  5. namespace app1  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.              SqlConnection con = new SqlConnection("Data Source=host.docker.internal,1433;Initial Catalog = SampleDb;Persist Security Info=False;User Id=sa;Password=password0;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;");  
  12.              con.Open();  
  13.              SqlDataAdapter ad = new SqlDataAdapter("select * from employee", con);  
  14.              con.Close();  
  15.              DataTable dt = new DataTable();  
  16.              ad.Fill(dt);  
  17.              for (int i = 0; i < dt.Rows.Count; i++)  
  18.              {  
  19.                  int a = Convert.ToInt32(dt.Rows[i][0]);  
  20.                  string b = dt.Rows[i][1].ToString();  
  21.                  string c = dt.Rows[i][2].ToString();  
  22.                  string d = dt.Rows[i][3].ToString();  
  23.                  int e = Convert.ToInt32(dt.Rows[i][4]);  
  24.                  Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}",a,b,c,d,e);  
  25.              }  
  26.         }  
  27.     }  
  28. }  
and Dockerfile code is:
  1. FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env  
  2. WORKDIR /app  
  3. COPY *.csproj ./  
  4. RUN dotnet restore  
  5. COPY . ./  
  6. RUN dotnet publish -c Release -o out  
  7. FROM mcr.microsoft.com/dotnet/core/sdk:3.1  
  8. WORKDIR /app  
  9. COPY --from=build-env /app/out .  
  10. ENTRYPOINT [ "dotnet","app1.dll" ]  
Error is :
 
Unhandled exception.
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: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
System.ComponentModel.Win32Exception (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at app1.Program.Main(String[] args) in C:\app\Program.cs:line 15 ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:10060,State:0,Class:20

Answers (1)