Java Code Examples for com.spotify.docker.client.DockerClient#listImages()

The following examples show how to use com.spotify.docker.client.DockerClient#listImages() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerHelper.java    From just-ask with Apache License 2.0 6 votes vote down vote up
private static void predownloadImagesIfRequired() throws DockerException, InterruptedException {

        DockerClient client = getClient();
        LOG.warning("Commencing download of images.");
        Collection<MappingInfo> images = getInstance().getMapping().values();

        ProgressHandler handler = new LoggingBuildHandler();
        for (MappingInfo image : images) {
            List<Image> foundImages = client.listImages(DockerClient.ListImagesParam.byName(image.getTarget()));
            if (! foundImages.isEmpty()) {
                LOG.warning(String.format("Skipping download for Image [%s] because it's already available.",
                    image.getTarget()));
                continue;
            }
            client.pull(image.getTarget(), handler);
        }
    }
 
Example 2
Source File: DockerHelper.java    From repairnator with MIT License 6 votes vote down vote up
public static String findDockerImage(String imageName, DockerClient docker) {
    if (docker == null) {
        throw new RuntimeException("Docker client has not been initialized. No docker image can be found.");
    }

    try {
        List<Image> allImages = docker.listImages(DockerClient.ListImagesParam.allImages());

        String imageId = null;
        for (Image image : allImages) {
            if (image.repoTags() != null && image.repoTags().contains(imageName)) {
                imageId = image.id();
                break;
            }
        }

        if (imageId == null) {
            throw new RuntimeException("There was a problem when looking for the docker image with argument \""+imageName+"\": no image has been found.");
        }
        return imageId;
    } catch (InterruptedException|DockerException e) {
        throw new RuntimeException("Error while looking for the docker image",e);
    }
}
 
Example 3
Source File: RedisContainer.java    From pay-publicapi with MIT License 6 votes vote down vote up
RedisContainer(DockerClient docker, String host) throws DockerException, InterruptedException {

        this.docker = docker;
        this.host = host;

        failsafeDockerPull(docker);
        docker.listImages(DockerClient.ListImagesParam.create("name", REDIS_IMAGE));

        final HostConfig hostConfig = HostConfig.builder().logConfig(LogConfig.create("json-file")).publishAllPorts(true).build();
        ContainerConfig containerConfig = ContainerConfig.builder()
                .image(REDIS_IMAGE)
                .hostConfig(hostConfig)
                .build();
        containerId = docker.createContainer(containerConfig).id();
        docker.startContainer(containerId);
        port = hostPortNumber(docker.inspectContainer(containerId));
        registerShutdownHook();
        waitForRedisToStart();
    }
 
Example 4
Source File: RemoveImageMojo.java    From docker-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected void execute(final DockerClient docker)
    throws MojoExecutionException, DockerException, InterruptedException {
  final String[] imageNameParts = parseImageName(imageName);
  if (imageTags == null) {
    imageTags = new ArrayList<>(1);
    imageTags.add(imageNameParts[1]);
  } else if (removeAllTags) {
    getLog().info("Removal of all tags requested, searching for tags");
    // removal of all tags requested, loop over all images to find tags
    for (final Image currImage : docker.listImages()) {
      getLog().debug("Found image: " + currImage.toString());
      String[] parsedRepoTag;
      if (currImage.repoTags() != null) {
        for (final String repoTag : currImage.repoTags()) {
          parsedRepoTag = parseImageName(repoTag);
          // if repo name matches imageName then save the tag for deletion
          if (Objects.equal(parsedRepoTag[0], imageNameParts[0])) {
            imageTags.add(parsedRepoTag[1]);
            getLog().info("Adding tag for removal: " + parsedRepoTag[1]);
          }
        }
      }
    }
  }
  imageTags.add(imageNameParts[1]);

  final Set<String> uniqueImageTags = new HashSet<>(imageTags);
  for (final String imageTag : uniqueImageTags) {
    final String currImageName =
        imageNameParts[0] + ((isNullOrEmpty(imageTag)) ? "" : (":" + imageTag));
    getLog().info("Removing -f " + currImageName);

    try {
      // force the image to be removed but don't remove untagged parents
      for (final RemovedImage removedImage : docker.removeImage(currImageName, true, false)) {
        getLog().info("Removed: " + removedImage.imageId());
      }
    } catch (ImageNotFoundException | NotFoundException e) {
      // ignoring 404 errors only
      getLog().warn("Image " + imageName + " doesn't exist and cannot be deleted - ignoring");
    }
  }
}