Search in sources :

Example 11 with Image

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));
}
Also used : Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Image(com.spotify.docker.client.messages.Image) RemovedImage(com.spotify.docker.client.messages.RemovedImage) Test(org.junit.Test)

Example 12 with Image

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);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) Image(com.spotify.docker.client.messages.Image)

Example 13 with Image

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);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) Image(com.spotify.docker.client.messages.Image)

Example 14 with Image

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);
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerMount(com.spotify.docker.client.messages.ContainerMount) Arrays(java.util.Arrays) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) Environment(de.zalando.ep.zalenium.util.Environment) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) URL(java.net.URL) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) LoggerFactory(org.slf4j.LoggerFactory) GoogleAnalyticsApi(de.zalando.ep.zalenium.util.GoogleAnalyticsApi) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) ArrayList(java.util.ArrayList) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) Map(java.util.Map) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerException(com.spotify.docker.client.exceptions.DockerException) Logger(org.slf4j.Logger) DockerSeleniumStarterRemoteProxy(de.zalando.ep.zalenium.proxy.DockerSeleniumStarterRemoteProxy) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ImmutableMap(com.google.common.collect.ImmutableMap) Collectors(java.util.stream.Collectors) Container(com.spotify.docker.client.messages.Container) List(java.util.List) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Image(com.spotify.docker.client.messages.Image) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HostConfig(com.spotify.docker.client.messages.HostConfig) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Image(com.spotify.docker.client.messages.Image) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) AnsiProgressHandler(com.spotify.docker.client.AnsiProgressHandler) HostConfig(com.spotify.docker.client.messages.HostConfig)

Aggregations

Image (com.spotify.docker.client.messages.Image)14 DockerClient (com.spotify.docker.client.DockerClient)7 Test (org.junit.Test)7 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)6 Container (com.spotify.docker.client.messages.Container)4 DockerException (com.spotify.docker.client.exceptions.DockerException)3 RemovedImage (com.spotify.docker.client.messages.RemovedImage)3 Long.toHexString (java.lang.Long.toHexString)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)2 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)2 ImageInfo (com.spotify.docker.client.messages.ImageInfo)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)2 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)2 Before (org.junit.Before)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1