public int GetContainerCount(IMatchlist states) { var count = 0; foreach (var container in Containers) { // created A container that has been created (e.g. with docker create) but not started // restarting A container that is in the process of being restarted // running A currently running container // paused A container whose processes have been paused // exited A container that ran and completed ("stopped" in other contexts, although a created container is technically also "stopped") // dead A container that the daemon tried and failed to stop (usually due to a busy device or resource used by the container) // removing if (states.Match(container.State.Status)) { count++; } } return(count); }
private void StopAndRemoveContainers(DockerImageNode image) { var imageShortId = DockerImageNode.GetImageShortId(image.InspectResponse.ID); // Remove containers in black list states foreach (var container in image.Containers) { var status = _dockerClient.Containers.InspectContainerAsync(container.ID).Result.State.Status; // If container state changed and not in black list states, we should skip deleting if (!_containerStateBlacklist.Match(status)) { throw new Exception($"Postponed deletion for image: {imageShortId} due to container status change"); } else { Console.WriteLine($"[{DateTime.UtcNow.ToString()} UTC] Stop and remove container: {container.ID} using image: {imageShortId}"); _dockerClient.Containers.StopContainerAsync(container.ID, new ContainerStopParameters()).Wait(); _dockerClient.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters() { Force = false, RemoveLinks = false, RemoveVolumes = false }).Wait(); } } }