如何使用`Docker.DotNet`库连接到本地Docker服务?

时间:2018-01-02 03:10:11

标签: c# .net docker .net-core docker-api

如何使用 Linux 环境中的Docker.DotNet库连接到我的本地Docker服务(考虑到我使用的是.Net Core 2.0)?

我认为它与/var/run/docker.sock文件有某种关系,但我无法弄清楚如何实现这一点。

2 个答案:

答案 0 :(得分:2)

已报告此问题,根据here中的讨论,以下内容适用于linux:

DockerClient client = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock"))
     .CreateClient()

答案 1 :(得分:0)

我创建了一种帮助程序类,该类根据底层操作系统提供默认的本地api uri:

public static class Docker
{
    static Docker()
    {
        DefaultLocalApiUri = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) 
            ? new Uri("npipe://./pipe/docker_engine")
            : new Uri("unix:/var/run/docker.sock");
    }

    public static Uri DefaultLocalApiUri { get; }
}