本文整理汇总了Golang中github.com/samalba/dockerclient.DockerClient.ListContainers方法的典型用法代码示例。如果您正苦于以下问题:Golang DockerClient.ListContainers方法的具体用法?Golang DockerClient.ListContainers怎么用?Golang DockerClient.ListContainers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/samalba/dockerclient.DockerClient
的用法示例。
在下文中一共展示了DockerClient.ListContainers方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetContainers
func (n *Node) GetContainers(do *dockerclient.DockerClient) ([]dockerclient.Container, error) {
containers, err := do.ListContainers(false, false, "")
if err != nil {
log.Fatal(err)
}
return containers, err
}
示例2: GetInjectorsForPolicy
func GetInjectorsForPolicy(client *docker.DockerClient, name string) ([]injector, error) {
is := []injector{}
cs, err := client.ListContainers(true, false, fmt.Sprintf(`{"label":["%s=%s"]}`, AGENT_LABEL, name))
if err != nil {
return is, err
}
for _, c := range cs {
is = append(is, InjectorFromContainer(c))
}
return is, nil
}
示例3: runGC
func runGC(dockerClient *dockerclient.DockerClient, filters ...string) (bool, error) {
done := true
images, err := dockerClient.ListImages(true)
if err != nil {
return true, err
}
imagesToSave := make(map[string]bool)
for _, image := range images {
for _, repoTag := range image.RepoTags {
for _, regexFilter := range filters {
if match, _ := regexp.MatchString(regexFilter, repoTag); match {
log.Printf("Image %v matches regexp /%s/ to keep\n", image.Id, regexFilter)
imagesToSave[image.Id] = true
}
}
}
}
for _, i := range images {
if i.ParentId != "" {
log.Printf("Image %s has children\n", i.ParentId)
imagesToSave[i.ParentId] = true
}
}
containers, err := dockerClient.ListContainers(true, false, "")
if err != nil {
return true, err
}
for _, c := range containers {
info, _ := dockerClient.InspectContainer(c.Id)
log.Printf("Image %s in use by container %v\n", info.Image, c.Id)
imagesToSave[info.Image] = true
}
for _, image := range images {
if !imagesToSave[image.Id] {
log.Printf("Deleting image with image id %s %v\n", image.Id, image.RepoTags)
done = false
_, err = dockerClient.RemoveImage(image.Id)
if err != nil {
log.Println("Failed to delete image: ", err)
}
}
}
log.Println("Done with images GC")
return done, nil
}
示例4: ListContainersDetailed
func ListContainersDetailed(dockerClient *dockerclient.DockerClient) ([]*dockerclient.ContainerInfo, error) {
containers, err := dockerClient.ListContainers(true, false, "")
if err != nil {
return nil, err
}
var result = make([]*dockerclient.ContainerInfo, len(containers))
for i, container := range containers {
containerInfo, err := dockerClient.InspectContainer(container.Id)
if err != nil {
return nil, err
}
result[i] = containerInfo
}
return result, nil
}
示例5: list_containers
func list_containers(docker *dockerclient.DockerClient) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get only running containers
containers, err := docker.ListContainers(false, false, "")
if err != nil {
log.Fatal(err)
}
dat, err := json.MarshalIndent(&containers, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, string(dat))
}
}
示例6: listDevices
func listDevices(docker *dc.DockerClient) []string {
containers, err := docker.ListContainers(false)
if err != nil {
panic(err)
}
res := make([]string, 0, 10)
for _, c := range containers {
name := arrayToStr(c.Names)
if strings.HasPrefix(name, "/device") {
// found a device container
res = append(res, name[len("/device"):])
}
}
return res
}
示例7: supprDocker
func supprDocker(docker *dockerclient.DockerClient, dockerName string) {
var searchDockerName string = "{\"name\":[\"" + dockerName + "\"]}"
//l4g.Info("%v\n", searchDockerName)
// Get only running containers
containers, err := docker.ListContainers(true, true, searchDockerName)
if err != nil {
log.Fatal(err)
}
for _, c := range containers {
l4g.Info(c.Id, c.Names)
if err := docker.RemoveContainer(c.Id, true, false); err != nil {
//l4g.Info("cannot stop container: %s", err)
var RS string = fmt.Sprintf("cannot stop container: %s", err)
l4g.Info(RS)
writeSDLstr(RS)
}
}
}
示例8: GetAcceptanceTestContainerInfo
func GetAcceptanceTestContainerInfo(docker *dockerclient.DockerClient, containerType string) *dockerclient.ContainerInfo {
// Get only running containers
containers, err := docker.ListContainers(false, false, "")
if err != nil {
log.Fatal(err)
}
//Loop through them until we find a match
for _, c := range containers {
xtContainerType, ok := c.Labels["xt-container-type"]
if ok && xtContainerType == containerType {
//Grab the information for the container
info, err := docker.InspectContainer(c.Id)
if err != nil {
log.Fatal(err)
}
return info
}
}
return nil
}
示例9: getExpired
func getExpired(client *dockerclient.DockerClient) (expiredContainers []*dockerclient.ContainerInfo, expiredImages []*dockerclient.Image, err error) {
now := time.Now()
// Containers
containers, err := client.ListContainers(true, false, "") // true = all containers
if err != nil {
return nil, nil, err
}
usedVolumeContainers := map[string]int{}
usedImages := map[string]int{}
oldContainers := []*dockerclient.ContainerInfo{}
for _, c := range containers {
debug("< container: %s", c.Id)
container, err := client.InspectContainer(c.Id)
if err != nil {
debug(" + Couldn't inspect container %s, skipping: %s", c.Id, err)
continue
}
// Increment reference counter refering to how many containers use volume container and image
if len(container.HostConfig.VolumesFrom) > 0 {
for _, vc := range container.HostConfig.VolumesFrom {
usedVolumeContainers[vc]++
}
}
debug(" + Container %s uses image %s", c.Id, container.Image)
usedImages[container.Image]++
if container.State.Running {
debug(" + Container is still running")
continue
}
debug(" + not running")
if container.State.FinishedAt == time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) && !*unsafe {
debug(" + Container %s has empty FinishedAt field, skipping", c.Id)
continue
}
created, err := time.Parse(time.RFC3339, container.Created)
if err != nil {
return nil, nil, err
}
debug(" + Container and image threshold %s", now.Add(-*ageContainer))
if created.After(now.Add(-*ageContainer)) {
debug(" + Creation time is not old enough: %s", created)
continue
}
debug(" + Creation time is older than %s", *ageContainer)
if container.State.FinishedAt.After(now.Add(-*ageContainer)) {
debug(" + Exit time is not old enough: %s", container.State.FinishedAt)
continue
}
debug(" + Exit time is older than %s", *ageContainer)
// Decrement reference counter for old containers
if len(container.HostConfig.VolumesFrom) > 0 {
for _, vc := range container.HostConfig.VolumesFrom {
usedVolumeContainers[vc]--
}
}
usedImages[container.Image]--
oldContainers = append(oldContainers, container)
}
for _, c := range oldContainers {
name := c.Name[1:] // Remove leading /
if usedVolumeContainers[name] > 0 {
continue
}
expiredContainers = append(expiredContainers, c)
}
log.Printf("Found %d expired containers", len(expiredContainers))
// Images
images, err := client.ListImages(true)
if err != nil {
return nil, nil, err
}
for _, image := range images {
debug("< image id: %s", image.Id)
ctime := time.Unix(image.Created, 0)
if ctime.After(now.Add(-*ageImage)) {
continue
}
debug(" + older than %s", *ageImage)
if usedImages[image.Id] > 0 {
debug(" + in use, skipping")
continue
}
expiredImages = append(expiredImages, image)
}
log.Printf("Found %d expired images", len(expiredImages))
return expiredContainers, expiredImages, nil
}