skip to Main Content

I have a sample console application running on .NET 4.8 Framework. Application is able to connect local SQL Server. But when I run the application inside the docker container, I am not able to establish connection between the application and SQL Server.

Can anyone please help me with a solution?

This is my code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("I am inside Docker!!!!");
        TestDBConnection();
        Console.ReadLine();
        Console.WriteLine("End");
    }

    public static void TestDBConnection()
    {
        try
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            Console.WriteLine("I am inside Docker!!!");

            Console.WriteLine("Connecting to Local DB");
            using (SqlConnection connection = new SqlConnection("Server=172.23.128.1\LOCALHOST, 49172;Initial Catalog=config;User ID=sa;Password=Compusol@123;MultipleActiveResultSets=False;Encrypt=True;Connection Timeout=30;"))
            {
                connection.Open();
                Console.WriteLine("Connected to Local DD open");                    
            }
        }
        catch (SqlException e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("nDone. Press enter.");
        Console.ReadLine();
    }
}

Docker Engine v20.10.14

Docker File

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8 AS runtime
WORKDIR /app
COPY . .
ENTRYPOINT ["DockerTestConnection.exe"]

I am publishing the application and running Docker inside the published folder. Publish folder will have all the application dependencies.

2

Answers


  1. When you are running your app inside a container, you will be better of using FQDNs instead of IP address to connect to your host Server instance. Depending on your particular setup, host OS and other factors, that name might vary. On Windows, it is {hostname}.mshome.net (or something like that). Then, your connection request will be routed to the host on a "Docker IP" address. You should consider reading up about the different networking and routing options available in a containerized world.

    For example, your HOST might have the IP address 172.23.128.1. But it also will have additional IP on the vEthernet adapter, You should use that IP instead.

    Login or Signup to reply.
  2. firstly you need to check the network type i assume it is a "NAT network" because based on the type of the network the connection could not be possible , then you need to verify the connection between your container and the sql server .
    you run this command changing the IP adress and the port if you are not using 1433 port of sql server

    Test-NetConnection -ComputerName xx.xxx.xx.xx -Port 1433 -InformationLevel "Detailed"
    

    then if the test succeeded then problem is about the connection string
    you can use this template it works for mer :

    "Server=xx.xxx.xx.xx\MSSQLSERVER,1433;Initial Catalog=dbname;User Id=xxxx;Password=xxxxx;"
    

    please verify the sqlserver instance you are using because i dont think that the instance name is localhost
    by default it’s MSSQLSERVER

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search