在Windows容器(同一主机)中使用命名管道

问题描述 投票:1回答:1

我想有2个Windows容器-通过命名管道(而不是匿名管道)在同一主机上运行(使用Windows 10客户端计算机和Windows的docker)。但是,我无法使其正常工作。

我的命名管道服务器类是here in GitHub。简而言之,这些代码来自Microsoft Docs:

    private  void ServerThread(object data)
    {
        NamedPipeServerStream pipeServer =
            new NamedPipeServerStream(this.pipeName, PipeDirection.InOut, numThreads);
        int threadId = Thread.CurrentThread.ManagedThreadId;
        pipeServer.WaitForConnection();
        try
        {
            StreamString ss = new StreamString(pipeServer);
            ss.WriteString("I am the one true server!");
            string message = ss.ReadString();

            ss.WriteString($"Server message {DateTime.Now.ToLongTimeString()}");
        }
        catch (IOException e)
        {
            Console.WriteLine("ERROR: {0}", e.Message);
        }
        pipeServer.Close();
    }

并且客户端代码(同样来自Microsoft Docs)位于相同的GitHub repo中。本质上,代码如下:

    private static void RunCore(string ip, string pipeName)
    {
        NamedPipeClientStream pipeClient =
                new NamedPipeClientStream(ip, pipeName,
                    PipeDirection.InOut, PipeOptions.None,
                    TokenImpersonationLevel.Impersonation);
        pipeClient.Connect();

        StreamString ss = new StreamString(pipeClient);
        if (ss.ReadString() == "I am the one true server!")
        {
            ss.WriteString("Message from client " + DateTime.Now.ToString());
            Console.Write(ss.ReadString());
        }
        else
        {
            Console.WriteLine("Server could not be verified.");
        }
        pipeClient.Close();
    }

整个项目在this GitHub directory中。

如果现在在本地计算机上运行此客户端,则客户端可以访问服务器(在客户端和服务器上都可以看到控制台消息)。然后,使用以下泊坞文件将可执行文件放入容器中:

FROM mcr.microsoft.com/dotnet/framework/runtime:4.8
WORKDIR /app
COPY ./bin/release/ ./
ENTRYPOINT ["C:\\app\\Namedpipe.exe"]

现在,在Windows 10客户端计算机上(使用Windows Docker),我以以下方式启动服务器:

docker run -it -v \\.\pipe\helloworld:\\.\pipe\helloworld named-pipe-net-framework:latest

至此,我已验证我的主机中有一个命名管道(名称为'helloworld')(使用pipelist.exe)。然后我以客户端模式在容器中吃午餐:

docker run -it -v \\.\pipe\helloworld:\\.\pipe\helloworld  named-pipe-net-framework:latest

但是客户端永远无法到达管道(这需要花费很长时间才能冻结,然后失败)。但是,我已经在客户端容器中使用了Powershell进行了午餐(使用docker exec),并且可以运行pipelist.exe并看到可用的命名管道“ helloworld”。但是代码不起作用。谁能给我一些指示,为什么这不起作用?

docker containers ipc named-pipes windows-container
1个回答
0
投票

[我发现,只有客户端管道才能从容器内连接到主机上打开的服务器管道。

[它也仅对我有用(仅对我而言),当我使用C ++ WIN32 API创建此类管道时,当使用.NET的NamedPipeClientStream时,出现了相同的冻结。无论使用哪种技术,在容器中创建服务器管道都会导致错误,可能是因为容器中的映射已将其占用。

[这实际上意味着不能通过命名管道直接连接2个容器,因为它们都是客户端管道,它们只能连接到主机上的服务器管道。

我相信Windows中命名管道的映射是通过在容器(服务器管道)和主机(客户端管道)上使用几个额外的管道,以及消息通过4个连接的管道链传播来实现的: serverhost-clienthost-servercontainer-clientcontainer。

[花了几天时间在互联网上搜索相同的信息,没有运气。关于Windows容器中命名管道行为的几乎所有情况都是关于从容器内部访问Docker API。甚至可以开始怀疑它是否可以支持其他功能。

© www.soinside.com 2019 - 2024. All rights reserved.