dotnet / Docker.DotNet

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

recieving single container from ContainerListResponse #641

Open hakimdotdev opened 1 year ago

hakimdotdev commented 1 year ago

Output of dotnet --info:

.NET SDK: Version: 7.0.302 Commit: 990cf98a27 What version of Docker.DotNet?:

Latest Nuget Steps to reproduce the issue:

 private async Task<ContainerListResponse> GetContainerFromName(string containerName)
    {
        var containers = await new DockerClientConfiguration().CreateClient().Containers.ListContainersAsync(
            new ContainersListParameters { All = true }
        );
        var container = containers.FirstOrDefault(c => c.Names.Contains(containerName));

        return container;
    }

public async Task<IActionResult> StartContainer(string container)
    {
        var containerObj = await GetContainerFromName(container);
        var i = await new DockerClientConfiguration().CreateClient().Containers.InspectContainerAsync(containerObj.ID);

Or Any other Linq Query **What actually happened?:*** Sequence is Empty

What did you expect to happen?: To be able to retrieve the First or really any container from the IList with query or somehow to be able to get .ID for StartAsync

var start = await new DockerClientConfiguration().CreateClient().Containers.StartContainerAsync(containerObj.ID, new ContainerStartParameters());

I'm trying to get a single container followed by its ID from name.Is there some way to easily achieve this, which I'm missing out on? I couldn't deduce anything off of the documentation available in the repo. Thanks in Advance.

HofmeisterAn commented 1 year ago

I am unable to recreate the issue. I am receiving the created containers. Is the variable containers empty, or does it simply not contain the container with the expected name?

Screenshot 2023-06-07 at 10 32 54
hakimdotdev commented 1 year ago

            var containers = await new DockerClientConfiguration().CreateClient().Containers.ListContainersAsync(new ContainersListParameters());
            _containers.AddRange(containers);

        var statuses = _containers.Select(c => $"{c.Names.FirstOrDefault()}: {c.State}");

        return statuses.ToList();

will return all containers with name and state properly but trying to get the object or just the id from the name will leave the passed object empty.

EDIT:

The name is passed properly, what had to be done is querying Names[0] not the IList itself.