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())));
}
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();
}
}
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;
}
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;
}
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;
}
Aggregations