Search in sources :

Example 1 with NotFoundException

use of com.github.dockerjava.api.exception.NotFoundException 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 2 with NotFoundException

use of com.github.dockerjava.api.exception.NotFoundException in project elastest-torm by elastest.

the class DockerService2 method pullETExecImage.

public void pullETExecImage(String image, String name) throws TJobStoppedException {
    DockerClient dockerClient = this.getDockerClient();
    try {
        logger.debug("Try to Pulling {} Image ({})", name, image);
        dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion();
        logger.debug("{} image pulled succesfully!", name);
    } catch (InternalServerErrorException | NotFoundException | InterruptedException e) {
        if (imageExistsLocally(image, dockerClient)) {
            logger.info("Docker image exits locally.");
        } else {
            logger.error("Error pulling the {} image: {}", name, image, e.getMessage());
        }
    } catch (DockerClientException e) {
        logger.info("Error on Pulling {} image ({}). Probably because the user has stopped the execution", name, image);
        throw new TJobStoppedException();
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException)

Example 3 with NotFoundException

use of com.github.dockerjava.api.exception.NotFoundException in project elastest-torm by elastest.

the class DockerService2 method existsContainer.

public boolean existsContainer(String containerName) {
    DockerClient dockerClient = this.getDockerClient();
    boolean exists = true;
    try {
        dockerClient.inspectContainerCmd(containerName).exec();
    } catch (NotFoundException e) {
        exists = false;
    }
    return exists;
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) NotFoundException(com.github.dockerjava.api.exception.NotFoundException)

Example 4 with NotFoundException

use of com.github.dockerjava.api.exception.NotFoundException in project testcontainers-java by testcontainers.

the class DockerClientFactoryTest method runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally.

@Test
public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() {
    final DockerClientFactory dockFactory = DockerClientFactory.instance();
    try {
        // remove tiny image, so it will be pulled during next command run
        dockFactory.client().removeImageCmd(TestcontainersConfiguration.getInstance().getTinyImage()).withForce(true).exec();
    } catch (NotFoundException ignored) {
    // Do not fail if it's not pulled yet
    }
    dockFactory.runInsideDocker(cmd -> cmd.withCmd("sh", "-c", "echo 'SUCCESS'"), (client, id) -> client.logContainerCmd(id).withStdOut(true).exec(new LogToStringContainerCallback()).toString());
}
Also used : NotFoundException(com.github.dockerjava.api.exception.NotFoundException) LogToStringContainerCallback(org.testcontainers.dockerclient.LogToStringContainerCallback) Test(org.junit.Test)

Example 5 with NotFoundException

use of com.github.dockerjava.api.exception.NotFoundException in project vespa by vespa-engine.

the class DockerImplTest method pullImageAsyncIfNeededSuccessfully.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void pullImageAsyncIfNeededSuccessfully() {
    final DockerImage image = new DockerImage("test:1.2.3");
    InspectImageResponse inspectImageResponse = mock(InspectImageResponse.class);
    when(inspectImageResponse.getId()).thenReturn(image.asString());
    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec()).thenThrow(new NotFoundException("Image not found")).thenReturn(inspectImageResponse);
    // Array to make it final
    ArgumentCaptor<ResultCallback> resultCallback = ArgumentCaptor.forClass(ResultCallback.class);
    PullImageCmd pullImageCmd = mock(PullImageCmd.class);
    when(pullImageCmd.exec(resultCallback.capture())).thenReturn(null);
    final DockerClient dockerClient = mock(DockerClient.class);
    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);
    final DockerImpl docker = new DockerImpl(dockerClient);
    docker.setMetrics(new MetricReceiverWrapper(MetricReceiver.nullImplementation));
    assertTrue("Should return true, we just scheduled the pull", docker.pullImageAsyncIfNeeded(image));
    assertTrue("Should return true, the pull i still ongoing", docker.pullImageAsyncIfNeeded(image));
    assertTrue(docker.imageIsDownloaded(image));
    resultCallback.getValue().onComplete();
    assertFalse(docker.pullImageAsyncIfNeeded(image));
}
Also used : InspectImageCmd(com.github.dockerjava.api.command.InspectImageCmd) MetricReceiverWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) ResultCallback(com.github.dockerjava.api.async.ResultCallback) DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) InspectImageResponse(com.github.dockerjava.api.command.InspectImageResponse) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Test(org.junit.Test)

Aggregations

NotFoundException (com.github.dockerjava.api.exception.NotFoundException)9 DockerClient (com.github.dockerjava.api.DockerClient)5 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)4 PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)3 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)3 Test (org.junit.Test)3 ResultCallback (com.github.dockerjava.api.async.ResultCallback)2 InspectImageCmd (com.github.dockerjava.api.command.InspectImageCmd)2 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)2 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)2 MetricReceiverWrapper (com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper)2 HubIntegrationException (com.blackducksoftware.integration.hub.exception.HubIntegrationException)1 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)1 InspectImageResponse (com.github.dockerjava.api.command.InspectImageResponse)1 DockerException (com.github.dockerjava.api.exception.DockerException)1 Image (com.github.dockerjava.api.model.Image)1 LogToStringContainerCallback (org.testcontainers.dockerclient.LogToStringContainerCallback)1