use of com.spotify.docker.client.messages.Image in project docker-client by spotify.
the class DefaultDockerClientTest method testListImages.
@Test
public void testListImages() throws Exception {
sut.pull(BUSYBOX_LATEST);
final List<Image> images = sut.listImages();
assertThat(images.size(), greaterThan(0));
// Verify that image contains valid values
Image busybox = null;
for (final Image image : images) {
if (image.repoTags() != null && image.repoTags().contains(BUSYBOX_LATEST)) {
busybox = image;
}
}
assertNotNull(busybox);
assertThat(busybox.virtualSize(), greaterThan(0L));
assertThat(busybox.created(), not(isEmptyOrNullString()));
assertThat(busybox.id(), not(isEmptyOrNullString()));
assertThat(busybox.repoTags(), notNullValue());
assertThat(busybox.repoTags().size(), greaterThan(0));
assertThat(BUSYBOX_LATEST, isIn(busybox.repoTags()));
if (dockerApiVersionLessThan("1.22")) {
assertThat(busybox.parentId(), not(isEmptyOrNullString()));
}
final List<Image> imagesWithDigests = sut.listImages(digests());
assertThat(imagesWithDigests.size(), greaterThan(0));
busybox = null;
for (final Image image : imagesWithDigests) {
if (image.repoTags() != null && image.repoTags().contains(BUSYBOX_LATEST)) {
busybox = image;
}
}
assertNotNull(busybox);
if (dockerApiVersionLessThan("1.22")) {
assertThat(busybox.repoDigests(), notNullValue());
}
// Using allImages() should give us more images
final List<Image> allImages = sut.listImages(allImages());
assertThat(allImages.size(), greaterThan(images.size()));
// Including just dangling images should give us fewer images
final List<Image> danglingImages = sut.listImages(danglingImages());
assertThat(danglingImages.size(), lessThan(images.size()));
// Specifying both allImages() and danglingImages() should give us only dangling images
final List<Image> allAndDanglingImages = sut.listImages(allImages(), danglingImages());
assertThat(allAndDanglingImages.size(), equalTo(danglingImages.size()));
// Can list by name
final List<Image> imagesByName = sut.listImages(byName(BUSYBOX));
assertThat(imagesByName.size(), greaterThan(0));
final Set<String> repoTags = Sets.newHashSet();
for (final Image imageByName : imagesByName) {
if (imageByName.repoTags() != null) {
repoTags.addAll(imageByName.repoTags());
}
}
assertThat(BUSYBOX_LATEST, isIn(repoTags));
}
use of com.spotify.docker.client.messages.Image in project repairnator by Spirals-Team.
the class AbstractPoolManager method findDockerImage.
public String findDockerImage(String imageName) {
try {
this.initDockerClient();
List<Image> allImages = this.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);
}
}
use of com.spotify.docker.client.messages.Image in project repairnator by Spirals-Team.
the class Launcher method findDockerImage.
private String findDockerImage() {
try {
docker = DefaultDockerClient.fromEnv().build();
List<Image> allImages = docker.listImages(DockerClient.ListImagesParam.allImages());
String imageId = null;
for (Image image : allImages) {
if (image.repoTags() != null && image.repoTags().contains(this.config.getDockerImageName())) {
imageId = image.id();
break;
}
}
if (imageId == null) {
throw new RuntimeException("There was a problem when looking for the docker image with argument \"" + this.config.getDockerImageName() + "\": no image has been found.");
}
return imageId;
} catch (DockerCertificateException | InterruptedException | DockerException e) {
throw new RuntimeException("Error while looking for the docker image", e);
}
}
use of com.spotify.docker.client.messages.Image in project zalenium by zalando.
the class DockerContainerClient method createContainer.
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
String containerName = generateContainerName(zaleniumContainerName, nodePort);
loadMountedFolders(zaleniumContainerName);
// In some environments the created containers need to be labeled so the platform can handle them. E.g. Rancher.
loadSeleniumContainerLabels();
loadPullSeleniumImageFlag();
loadIsZaleniumPrivileged(zaleniumContainerName);
loadStorageOpts(zaleniumContainerName);
List<String> binds = generateMountedFolderBinds();
binds.add("/dev/shm:/dev/shm");
String noVncPort = envVars.get("NOVNC_PORT");
String networkMode = getZaleniumNetwork(zaleniumContainerName);
List<String> extraHosts = new ArrayList<>();
extraHosts.add(String.format("%s:%s", DOCKER_FOR_MAC_LOCALHOST_NAME, DOCKER_FOR_MAC_LOCALHOST_IP));
// Allows "--net=host" work. Only supported for Linux.
if (DOCKER_NETWORK_HOST_MODE_NAME.equalsIgnoreCase(networkMode)) {
envVars.put("SELENIUM_HUB_HOST", "127.0.0.1");
envVars.put("SELENIUM_NODE_HOST", "127.0.0.1");
envVars.put("PICK_ALL_RANDOM_PORTS", "true");
try {
String hostName = dockerClient.info().name();
extraHosts.add(String.format("%s:%s", hostName, "127.0.1.0"));
} catch (DockerException | InterruptedException e) {
logger.debug(nodeId + " Error while getting host name", e);
}
}
// Reflect extra hosts of the hub container
final List<String> hubExtraHosts = getContainerExtraHosts(zaleniumContainerName);
extraHosts.addAll(hubExtraHosts);
HostConfig hostConfig = HostConfig.builder().appendBinds(binds).networkMode(networkMode).extraHosts(extraHosts).autoRemove(true).storageOpt(storageOpt).privileged(isZaleniumPrivileged).build();
List<String> flattenedEnvVars = envVars.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toList());
flattenedEnvVars.addAll(zaleniumHttpEnvVars);
final String[] exposedPorts = { nodePort, noVncPort };
ContainerConfig.Builder builder = ContainerConfig.builder().image(image).env(flattenedEnvVars).exposedPorts(exposedPorts).hostConfig(hostConfig);
if (seleniumContainerLabels.size() > 0) {
builder.labels(seleniumContainerLabels);
}
final ContainerConfig containerConfig = builder.build();
try {
if (pullSeleniumImage) {
List<Image> images = dockerClient.listImages(DockerClient.ListImagesParam.byName(image));
if (images.size() == 0) {
// If the image has no tag, we add latest, otherwise we end up pulling all the images with that name.
String imageToPull = image.lastIndexOf(':') > 0 ? image : image.concat(":latest");
dockerClient.pull(imageToPull, new AnsiProgressHandler());
}
}
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while checking (and pulling) if the image is present", e);
ga.trackException(e);
}
try {
final ContainerCreation container = dockerClient.createContainer(containerConfig, containerName);
dockerClient.startContainer(container.id());
return new ContainerCreationStatus(true, containerName, nodePort);
} catch (DockerException | InterruptedException e) {
logger.warn(nodeId + " Error while starting a new container", e);
ga.trackException(e);
return new ContainerCreationStatus(false);
}
}
Aggregations