在Windows上通过命名pipe道访问Docker API

根据Docker for Windows FAQ ,“客户端可以通过命名pipe道连接到Docker引擎:npipe:////./pipe/docker_engine”

我一直试图通过命名pipe道连接到API无济于事:

public class DockerNamedPipeTest { private const string PIPE_PATH = "docker_engine"; public void Test() { using (NamedPipeClientStream pipeClient = new NamedPipeClientStream( ".", PIPE_PATH, PipeDirection.InOut, PipeOptions.WriteThrough, TokenImpersonationLevel.Impersonation)) { pipeClient.Connect(30); Send(pipeClient); Receive(pipeClient); } } public void Send(NamedPipeClientStream pipeClient) { if (pipeClient.IsConnected) { byte[] buffer = Encoding.UTF8.GetBytes("GET /containers/json"); pipeClient.Write(buffer, 0, buffer.Length); pipeClient.WaitForPipeDrain(); pipeClient.Flush(); } } public void Receive(NamedPipeClientStream pipeClient) { string result = string.Empty; if (pipeClient.IsConnected && pipeClient.CanRead) { do { byte b = (byte)pipeClient.ReadByte(); // <-- Hangs here result += Convert.ToChar(b).ToString(); } while (!pipeClient.IsMessageComplete); } Console.WriteLine(result); } } 

谁能告诉我我做错了什么?

微软的Docker客户端库支持命名pipe道,你看过吗?

这是一个例子:

 using Docker.DotNet; DockerClient client = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")) .CreateClient(); 

事实certificate,答案可以在Docker.DotNet代码的源代码中find,特别是在名为CloseWrite()的方法中的DockerPipeStream.cs类中:

https://github.com/Microsoft/Docker.DotNet/blob/master/src/Docker.DotNet/DockerPipeStream.cs

 // The Docker daemon expects a write of zero bytes to signal the end of writes. Use native // calls to achieve this since CoreCLR ignores a zero-byte write. 

我调整了这个方法到我的代码,代码不再挂起。

我现在得到一个400错误的请求,但至less现在我知道为什么与docker守护进程的通信挂了。 如果Docker for Windows常见问题解答提到了这个细微差别,那本来就不错。