dotnet / Docker.DotNet

:whale: .NET (C#) Client Library for Docker API
https://www.nuget.org/packages/Docker.DotNet/
MIT License
2.26k stars 381 forks source link

Simple image pulling with CreateImageAsync throws error #600

Open Stadzior opened 1 year ago

Stadzior commented 1 year ago

Output of dotnet --info:

.NET SDK:
 Version:   7.0.100
 Commit:    e12b7af219

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.1.701 [C:\Program Files\dotnet\sdk]
  2.2.300 [C:\Program Files\dotnet\sdk]
  2.2.301 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.1.21103.13 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.1.21103.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.1.21102.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.5.21301.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.1.21103.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  arm64 [C:\Program Files\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation]
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

What version of Docker.DotNet?:

3.125.12

Steps to reproduce the issue: Just run below code as a ConsoleApp (ofc. you need docker deamon running):

using Docker.DotNet;
using Docker.DotNet.Models;

var dockerEngineUrl = new Uri("npipe://./pipe/docker_engine");
using var client = new DockerClientConfiguration(dockerEngineUrl).CreateClient();

const string imageName = "rabbitmq:3.11-management";
await PullImage(client, imageName);

async Task PullImage(IDockerClient client, string imageName)
{
    var imageAlreadyExists = await ImageAlreadyExists(client, imageName);
    if (!imageAlreadyExists)
    {
        Console.WriteLine($"Image named '{imageName}' not found. Pulling image. {DateTime.Now.ToLongTimeString()}");

        var imagesCreateParameters = new ImagesCreateParameters
        {
            FromImage = imageName
        };

        var progress = new Progress<JSONMessage>();
        progress.ProgressChanged += (sender, e) =>
        {
            Console.WriteLine($"From: {e.From}");
            Console.WriteLine($"Status: {e.Status}");
            Console.WriteLine($"Stream: {e.Stream}");
            Console.WriteLine($"ID: {e.ID}");
            Console.WriteLine($"Progress: {e.ProgressMessage}");
            Console.WriteLine($"Error: {e.ErrorMessage}");
        };

        await client.Images.CreateImageAsync(imagesCreateParameters, new AuthConfig(), progress);

        Console.WriteLine($"Image named '{imageName}' pulled. {DateTime.Now.ToLongTimeString()}");
    }
}

async Task<bool> ImageAlreadyExists(IDockerClient client, string imageName)
{
    var listImagesParameters = new ImagesListParameters { All = true };
    var images = await client.Images.ListImagesAsync(listImagesParameters);
    return images.Any(image => image.RepoTags.Contains(imageName));
}

What actually happened?: I've got the following exception:

Unhandled exception. Newtonsoft.Json.JsonReaderException: Error parsing undefined value. Path '', line 1, position 2. at Newtonsoft.Json.JsonTextReader.MatchAndSetAsync(String value, JsonToken newToken, Object tokenValue, CancellationToken cancellationToken) at Newtonsoft.Json.JsonTextReader.ParseValueAsync(CancellationToken cancellationToken) at Docker.DotNet.Models.StreamUtil.MonitorStreamForMessagesAsync[T](Task1 streamTask, DockerClient client, CancellationToken cancellationToken, IProgress1 progress) at Docker.DotNet.Models.StreamUtil.MonitorResponseForMessagesAsync[T](Task1 responseTask, DockerClient client, CancellationToken cancel, IProgress1 progress) at Program.<

$>g__PullImage|0_9(IDockerClient client, String imageName) in /source/Program.cs:line 303 at Program.<
$>g__CreateQueue|0_0(IDockerClient client, ICollection`1 fileNames) in /source/Program.cs:line 46 at Program.
$(String[] args) in /source/Program.cs:line 26 at Program.
(String[] args)

What did you expect to happen?: RabbitMq image pulled and visible in docker image list

Additional information:

HofmeisterAn commented 1 year ago

Relates to https://github.com/dotnet/Docker.DotNet/issues/595#issuecomment-1289056994.