Esempio n. 1
0
func deleteImage(docker *dockerclient.DockerClient, imageIDs []string) []error {
	var errors []error
	if !noConfirm {
		var answer string
		fmt.Printf("This will delete %v images\n", len(imageIDs))
		fmt.Printf("Do you want to continue? (yes/no)\n")
		fmt.Scanln(&answer)
		if answer != "yes" {
			fmt.Printf("%v", answer)
			return nil
		}
	}

	for _, imageID := range imageIDs {
		imageDelete, deleteErr := docker.RemoveImage(imageID)
		if deleteErr != nil {
			errors = append(errors, deleteErr)
		}
		for _, id := range imageDelete {
			if id.Deleted != "" {
				log.Printf("DELETED: %v", id.Deleted)
			}
			if id.Untagged != "" {
				log.Printf("UNTAGGED: %#v", id.Untagged)
			}
		}
	}
	if len(errors) > 0 {
		return errors
	}
	return nil
}
Esempio n. 2
0
func removeImages(client *dockerclient.DockerClient, images []*dockerclient.Image) error {
	for _, image := range images {
		log.Print("rmi ", image.Id)
		if !*dry {
			if _, err := client.RemoveImage(image.Id); err != nil {
				debug("Couldn't remove image: %s", err)
			}
		}
	}
	return nil
}
Esempio n. 3
0
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
}