use of com.github.dockerjava.api.command.ListImagesCmd in project camel by apache.
the class DockerProducer method executeListImagesRequest.
/**
* Performs a list images request
*
* @param client
* @param message
* @return
*/
private ListImagesCmd executeListImagesRequest(DockerClient client, Message message) {
LOGGER.debug("Executing Docker Images List Request");
ListImagesCmd listImagesCmd = client.listImagesCmd();
String filter = DockerHelper.getProperty(DockerConstants.DOCKER_FILTER, configuration, message, String.class);
if (filter != null) {
listImagesCmd.withLabelFilter(filter);
}
Boolean showAll = DockerHelper.getProperty(DockerConstants.DOCKER_SHOW_ALL, configuration, message, Boolean.class);
if (showAll != null) {
listImagesCmd.withShowAll(showAll);
}
return listImagesCmd;
}
use of com.github.dockerjava.api.command.ListImagesCmd in project testcontainers-java by testcontainers.
the class RemoteDockerImage method resolve.
@Override
protected final String resolve() {
Profiler profiler = new Profiler("Rule creation - prefetch image");
Logger logger = DockerLoggerFactory.getLogger(imageName.toString());
profiler.setLogger(logger);
Profiler nested = profiler.startNested("Obtaining client");
DockerClient dockerClient = DockerClientFactory.instance().client();
try {
nested.stop();
profiler.start("Check local images");
int attempts = 0;
DockerClientException lastException = null;
while (true) {
// Does our cache already know the image?
if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
logger.trace("{} is already in image name cache", imageName);
break;
}
// Update the cache
ListImagesCmd listImagesCmd = dockerClient.listImagesCmd();
if (Boolean.parseBoolean(System.getProperty("useFilter"))) {
listImagesCmd = listImagesCmd.withImageNameFilter(imageName.toString());
}
List<Image> updatedImages = listImagesCmd.exec();
updatedImages.stream().map(Image::getRepoTags).filter(Objects::nonNull).flatMap(Stream::of).map(DockerImageName::new).collect(Collectors.toCollection(() -> AVAILABLE_IMAGE_NAME_CACHE));
// And now?
if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
logger.trace("{} is in image name cache following listing of images", imageName);
break;
}
// Log only on first attempt
if (attempts == 0) {
logger.info("Pulling docker image: {}. Please be patient; this may take some time but only needs to be done once.", imageName);
profiler.start("Pull image");
}
if (attempts++ >= 3) {
logger.error("Retry limit reached while trying to pull image: {}. Please check output of `docker pull {}`", imageName, imageName);
throw new ContainerFetchException("Retry limit reached while trying to pull image: " + imageName, lastException);
}
// The image is not available locally - pull it
try {
final PullImageResultCallback callback = new PullImageResultCallback();
dockerClient.pullImageCmd(imageName.getUnversionedPart()).withTag(imageName.getVersionPart()).exec(callback);
callback.awaitSuccess();
AVAILABLE_IMAGE_NAME_CACHE.add(imageName);
break;
} catch (DockerClientException e) {
lastException = e;
}
}
return imageName.toString();
} catch (DockerClientException e) {
throw new ContainerFetchException("Failed to get Docker client for " + imageName, e);
} finally {
profiler.stop().log();
}
}
use of com.github.dockerjava.api.command.ListImagesCmd in project dockerunit by qzagarese.
the class DefaultServiceBuilder method createAndStartContainer.
private String createAndStartContainer(CreateContainerCmd cmd, PullStrategy pullStrategy, DockerClient client) {
CompletableFuture<String> respFut = new CompletableFuture<>();
ListImagesCmd imagesCmd = client.listImagesCmd().withImageNameFilter(cmd.getImage());
List<Image> imagesList = imagesCmd.exec();
boolean imageAbsent = imagesList == null || imagesList.size() == 0;
CompletableFuture<Void> pullFut;
if (imageAbsent || pullStrategy.equals(PullStrategy.ALWAYS)) {
pullFut = pullImage(cmd, client);
} else {
pullFut = CompletableFuture.completedFuture(null);
}
pullFut.exceptionally(ex -> {
logger.warning("An error occurred while executing a docker pull operation: " + ex.getMessage());
return null;
}).thenRun(() -> {
String containerId = startContainer(cmd, client);
respFut.complete(containerId);
}).exceptionally(ex -> {
respFut.completeExceptionally(ex.getCause());
return null;
});
respFut.exceptionally(ex -> {
logger.severe("Cannot create container. Reason: " + ex.getMessage());
return null;
});
return respFut.join();
}
Aggregations