Docker.DotNet icon indicating copy to clipboard operation
Docker.DotNet copied to clipboard

recieving single container from ContainerListResponse

Open aitchdot opened this issue 2 years ago • 2 comments

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.

aitchdot avatar Jun 06 '23 14:06 aitchdot

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

HofmeisterAn avatar Jun 07 '23 08:06 HofmeisterAn


            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.

aitchdot avatar Jun 07 '23 09:06 aitchdot