Docker rm returning ContainerNotFoundException after successfully removing container

  c#, docker, docker-api, docker-engine, rm

I am using a C# API that talks to DockerEngine (Docker.DotNet) to do some things, but notably in this case, removed a stopped container. Here is my code:

foreach (var container in RunningContainers)
                if (container.State == "dead" || container.State == "exited")
                    long exitType = Client.Containers.InspectContainerAsync(container.ID).Result.State.ExitCode;

                    if (exitType != 0)
                    Client.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters()).Wait();

Note that in this case "RunningContainers" still includes the stopped containers and hasn’t had it’s state refreshed yet.

Here’s the bug. The API successfully calls out to DockerEngine, the container is removed from my local Docker Containers. Then shortly after the code moves on, a Docker "No such container" response is received, throwing an exception.

Any ideas what’s going on here?

Source: Docker Questions