简体   繁体   English

从在 Docker Windows 容器中运行的 .Net 4.8 控制台应用程序连接到本地 SQL Server

[英]Connect to local SQL Server from .Net 4.8 Console Application Running in Docker Windows container

I have a sample console application running on .NET 4.8 Framework.我有一个在 .NET 4.8 Framework 上运行的示例控制台应用程序。 Application is able to connect local SQL Server.应用程序能够连接本地 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.但是当我在 docker 容器中运行应用程序时,我无法在应用程序和 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 引擎 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.我正在发布应用程序并在发布的文件夹中运行 Docker。 Publish folder will have all the application dependencies. Publish 文件夹将包含所有应用程序依赖项。

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.当您在容器中运行应用程序时,最好使用 FQDN 而不是 IP 地址来连接到您的主机服务器实例。 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).在 Windows 上,它是 {hostname}.mshome.net(或类似的东西)。 Then, your connection request will be routed to the host on a "Docker IP" address.然后,您的连接请求将被路由到“Doc​​ker IP”地址上的主机。 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.例如,您的 HOST 可能具有 IP 地址 172.23.128.1。 But it also will have additional IP on the vEthernet adapter, You should use that IP instead.但它也会在 vEthernet 适配器上有额外的 IP,您应该使用该 IP。

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 .首先,您需要检查网络类型,我假设它是“NAT 网络”,因为根据网络类型,无法连接,然后您需要验证容器和 sql 服务器之间的连接。 you run this command changing the IP adress and the port if you are not using 1433 port of sql server如果您不使用 sql server 的 1433 端口,则运行此命令更改 IP 地址和端口

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 :那么如果测试成功,那么问题是关于连接字符串,您可以使用此模板,它适用于 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请验证您正在使用的 sqlserver 实例,因为我认为实例名称默认不是 localhost 它是MSSQLSERVER

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 windows Z05B6053C415A2134EAD6 容器中的 .NET 应用程序连接到在主机上运行的 SQL 服务器 - How do you connect to SQL Server running on the host from a .NET app in a windows docker container 在Docker容器中运行标准.NET控制台应用程序 - Running a standard .NET Console Application in docker container 使用 .NET 3.5 和 4.8 SDK 构建 Docker 容器? - Build Docker container with .NET 3.5 and 4.8 SDK? 我的 .net 框架 4.8 应用程序是否可以在 windows 服务器 .net 框架 4.5 上运行? - Is it possible for my .net framework 4.8 application to run on a windows server .net framework 4.5? 是否可以在使用 .NET 4.8 的 ASP.NET 应用程序中使用 SQL Server 2017 的 Geography 数据类型? - Is it possible to use Geography data type of SQL Server 2017 in ASP.NET application using .NET 4.8? 无法从.net核心应用程序连接到Sql Server - Cannot connect to Sql server from .net core application 如何从.NET应用程序连接到SQL Server Compact? - How to connect to a SQL Server Compact from a .NET application? 如何从在 Linux(docker 容器)上运行的 c#.net 核心在远程 Windows 网络路径上运行 .exe - How to run .exe on remote windows network path from c# .net core running on Linux (docker container) Azure 容器实例 - 不受支持的 windows 映像 .net 框架 4.8 - Azure Container Instance - Unsupported windows image .net framework 4.8 对在Docker容器上运行的C#点网络应用程序进行性能分析 - Profiling a C# dot net application running on a docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM