Docker.Dotnet 如何拉取镜像

Chr*_*ian 5 c# docker .net-core

我想使用Docker.DotNet拉取并运行一个镜像,但我很绝望,我不知道如何使用这个库来做到这一点。

谁能帮我?

我查看了https://github.com/microsoft/Docker.DotNet上的教程,但这没有帮助。

 public class DockerService
    {
        private readonly DockerClient DockerClient;

        public DockerService()
        {
            if (OperatingSystem.IsWindows())
            {
                DockerClient = new DockerClientConfiguration(new Uri("npipe://./pipe/docker_engine")).CreateClient();
            }
            else if (OperatingSystem.IsLinux())
            {
                DockerClient = new DockerClientConfiguration(new Uri("unix:///var/run/docker.sock")).CreateClient();
            }
            else
            {
                throw new Exception("unknown operation system");
            }
        }

        public void StartCompose()
        {
            var progress = new Progress<JSONMessage>();
            var task = PullImage(
                new ImagesCreateParameters()
                {
                    FromImage = "mcr.microsoft.com/dotnet/core/sdk",
                    Tag = "latest"
                },null,
                progress);
             task.Wait();

        }


        private Task PullImage(ImagesCreateParameters imagesCreateParameters, AuthConfig authConfig,
            Progress<JSONMessage> progress)
        {
            return DockerClient.Images.CreateImageAsync(imagesCreateParameters, authConfig, progress);
        }

    }
Run Code Online (Sandbox Code Playgroud)

我期望启动 task.wait() 时会发生一些事情?但它运行了几分钟却没有任何反应。

Han*_*ian 3

您的代码下载该映像并将其添加到本地 Docker 注册表中。docker images从命令行运行该命令,您应该看到图像现在就在那里。