Search in sources :

Example 1 with Image

use of com.github.dockerjava.api.model.Image in project tutorials by eugenp.

the class ImageLiveTest method givenListOfImages_whenTagImage_thenListMustIncrement.

@Test
public void givenListOfImages_whenTagImage_thenListMustIncrement() {
    // given
    List<Image> images = dockerClient.listImagesCmd().exec();
    Image image = images.get(0);
    // when
    dockerClient.tagImageCmd(image.getId(), "baeldung/alpine", "3.6.v2").exec();
    // then
    List<Image> imagesNow = dockerClient.listImagesCmd().exec();
    assertThat(imagesNow.size(), is(greaterThan(images.size())));
}
Also used : Image(com.github.dockerjava.api.model.Image) Test(org.junit.Test)

Example 2 with Image

use of com.github.dockerjava.api.model.Image in project hub-docker-inspector by blackducksoftware.

the class DockerClientManager method pullImage.

public String pullImage(final String imageName, final String tagName) throws HubIntegrationException {
    logger.info(String.format("Pulling image %s:%s", imageName, tagName));
    final DockerClient dockerClient = hubDockerClient.getDockerClient();
    final Image alreadyPulledImage = getLocalImage(dockerClient, imageName, tagName);
    if (alreadyPulledImage == null) {
        // Only pull if we dont already have it
        final PullImageCmd pull = dockerClient.pullImageCmd(imageName).withTag(tagName);
        try {
            pull.exec(new PullImageResultCallback()).awaitSuccess();
        } catch (final NotFoundException e) {
            final String msg = String.format("Pull failed: Image %s:%s not found. Please check the image name/tag", imageName, tagName);
            logger.error(msg);
            throw new HubIntegrationException(msg, e);
        }
        final Image justPulledImage = getLocalImage(dockerClient, imageName, tagName);
        if (justPulledImage == null) {
            final String msg = String.format("Pulled image %s:%s not found in image list.", imageName, tagName);
            logger.error(msg);
            throw new HubIntegrationException(msg);
        }
        return justPulledImage.getId();
    } else {
        logger.info("Image already pulled");
        return alreadyPulledImage.getId();
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Image(com.github.dockerjava.api.model.Image) HubIntegrationException(com.blackducksoftware.integration.hub.exception.HubIntegrationException)

Example 3 with Image

use of com.github.dockerjava.api.model.Image in project hub-docker-inspector by blackducksoftware.

the class DockerClientManager method getLocalImage.

private Image getLocalImage(final DockerClient dockerClient, final String imageName, final String tagName) {
    Image alreadyPulledImage = null;
    final List<Image> images = dockerClient.listImagesCmd().withImageNameFilter(imageName).exec();
    for (final Image image : images) {
        for (final String tag : image.getRepoTags()) {
            if (tag.contains(tagName)) {
                alreadyPulledImage = image;
                break;
            }
        }
        if (alreadyPulledImage != null) {
            break;
        }
    }
    return alreadyPulledImage;
}
Also used : Image(com.github.dockerjava.api.model.Image)

Example 4 with Image

use of com.github.dockerjava.api.model.Image in project vespa by vespa-engine.

the class DockerImageGarbageCollector method filterOutImagesUsedByContainers.

private Map<String, Image> filterOutImagesUsedByContainers(Map<String, Image> dockerImagesByImageId, List<com.github.dockerjava.api.model.Container> containerList) {
    Map<String, Image> filteredDockerImagesByImageId = new HashMap<>(dockerImagesByImageId);
    for (com.github.dockerjava.api.model.Container container : containerList) {
        String imageToSpare = container.getImageId();
        do {
            // May be null if two images have have the same parent, the first image will remove the parent, the
            // second will get null.
            Image sparedImage = filteredDockerImagesByImageId.remove(imageToSpare);
            imageToSpare = sparedImage == null ? "" : sparedImage.getParentId();
        } while (!imageToSpare.isEmpty());
    }
    return filteredDockerImagesByImageId;
}
Also used : HashMap(java.util.HashMap) Container(com.github.dockerjava.api.model.Container) Image(com.github.dockerjava.api.model.Image)

Example 5 with Image

use of com.github.dockerjava.api.model.Image in project vespa by vespa-engine.

the class DockerImageGarbageCollector method filterOutRecentImages.

private Map<String, Image> filterOutRecentImages(Map<String, Image> dockerImageByImageId) {
    Map<String, Image> filteredDockerImagesByImageId = new HashMap<>(dockerImageByImageId);
    final Instant now = Instant.now();
    filteredDockerImagesByImageId.keySet().forEach(imageId -> {
        if (!lastTimeUsedByImageId.containsKey(imageId))
            lastTimeUsedByImageId.put(imageId, now);
    });
    lastTimeUsedByImageId.entrySet().stream().filter(entry -> Duration.between(entry.getValue(), now).minus(MIN_AGE_IMAGE_GC).isNegative()).map(Map.Entry::getKey).forEach(image -> {
        String imageToSpare = image;
        do {
            Image sparedImage = filteredDockerImagesByImageId.remove(imageToSpare);
            imageToSpare = sparedImage == null ? "" : sparedImage.getParentId();
        } while (!imageToSpare.isEmpty());
    });
    return filteredDockerImagesByImageId;
}
Also used : HashMap(java.util.HashMap) Instant(java.time.Instant) Image(com.github.dockerjava.api.model.Image) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Image (com.github.dockerjava.api.model.Image)10 DockerClient (com.github.dockerjava.api.DockerClient)3 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 InspectImageResponse (com.github.dockerjava.api.command.InspectImageResponse)2 ListImagesCmd (com.github.dockerjava.api.command.ListImagesCmd)2 PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)2 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)2 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)2 Stream (java.util.stream.Stream)2 HubIntegrationException (com.blackducksoftware.integration.hub.exception.HubIntegrationException)1 ExecCreateCmdResponse (com.github.dockerjava.api.command.ExecCreateCmdResponse)1 ExecStartCmd (com.github.dockerjava.api.command.ExecStartCmd)1 InspectContainerCmd (com.github.dockerjava.api.command.InspectContainerCmd)1 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)1 InspectExecResponse (com.github.dockerjava.api.command.InspectExecResponse)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1 AuthConfig (com.github.dockerjava.api.model.AuthConfig)1 Container (com.github.dockerjava.api.model.Container)1