Search in sources :

Example 6 with NotFoundException

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

the class DockerService2 method runDockerContainer.

/**
 **************************
 */
/**
 *** Container Methods ****
 */
/**
 **************************
 */
public String runDockerContainer(DockerClient dockerClient, String imageName, List<String> envs, String containerName, String networkName, Ports portBindings, int listenPort) throws TJobStoppedException {
    try {
        dockerClient.pullImageCmd(imageName).exec(new PullImageResultCallback()).awaitSuccess();
    } catch (InternalServerErrorException | NotFoundException ie) {
        if (imageExistsLocally(imageName, dockerClient)) {
            logger.info("Docker image exits locally.");
        } else {
            throw ie;
        }
    } catch (DockerClientException e) {
        logger.info("Error on Pulling " + imageName + " image. Probably because the user has stopped the execution");
        throw new TJobStoppedException();
    }
    CreateContainerResponse container = dockerClient.createContainerCmd(imageName).withName(containerName).withEnv(envs).withNetworkMode(networkName).withExposedPorts(ExposedPort.tcp(listenPort)).withPortBindings(portBindings).withPublishAllPorts(true).exec();
    dockerClient.startContainerCmd(container.getId()).exec();
    this.insertCreatedContainer(container.getId(), containerName);
    logger.info("Id del contenedor:" + container.getId());
    return container.getId();
}
Also used : 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) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse)

Example 7 with NotFoundException

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

the class ResourceReaper method stopContainer.

private void stopContainer(String containerId, String imageName) {
    boolean running;
    try {
        InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(containerId).exec();
        running = containerInfo.getState().getRunning();
    } catch (NotFoundException e) {
        LOGGER.trace("Was going to stop container but it apparently no longer exists: {}");
        return;
    } catch (DockerException e) {
        LOGGER.trace("Error encountered when checking container for shutdown (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
        return;
    }
    if (running) {
        try {
            LOGGER.trace("Stopping container: {}", containerId);
            dockerClient.killContainerCmd(containerId).exec();
            LOGGER.trace("Stopped container: {}", imageName);
        } catch (DockerException e) {
            LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
        }
    }
    try {
        dockerClient.inspectContainerCmd(containerId).exec();
    } catch (NotFoundException e) {
        LOGGER.trace("Was going to remove container but it apparently no longer exists: {}");
        return;
    }
    try {
        LOGGER.trace("Removing container: {}", containerId);
        try {
            dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).withForce(true).exec();
            LOGGER.debug("Removed container and associated volume(s): {}", imageName);
        } catch (InternalServerErrorException e) {
            LOGGER.trace("Exception when removing container with associated volume(s): {} (due to {})", imageName, e.getMessage());
        }
    } catch (DockerException e) {
        LOGGER.trace("Error encountered shutting down container (ID: {}) - it may not have been stopped, or may already be stopped: {}", containerId, e.getMessage());
    }
}
Also used : InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) DockerException(com.github.dockerjava.api.exception.DockerException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException)

Example 8 with NotFoundException

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

the class DockerClientFactory method runInsideDocker.

private <T> T runInsideDocker(DockerClient client, Consumer<CreateContainerCmd> createContainerCmdConsumer, BiFunction<DockerClient, String, T> block) {
    checkAndPullImage(client, TINY_IMAGE);
    CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE).withLabels(DEFAULT_LABELS);
    createContainerCmdConsumer.accept(createContainerCmd);
    String id = createContainerCmd.exec().getId();
    try {
        client.startContainerCmd(id).exec();
        return block.apply(client, id);
    } finally {
        try {
            client.removeContainerCmd(id).withRemoveVolumes(true).withForce(true).exec();
        } catch (NotFoundException | InternalServerErrorException ignored) {
            log.debug("", ignored);
        }
    }
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException)

Example 9 with NotFoundException

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

the class DockerImplTest method pullImageAsyncIfNeededWithError.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void pullImageAsyncIfNeededWithError() {
    final DockerImage image = new DockerImage("test:1.2.3");
    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec()).thenThrow(new NotFoundException("Image not found"));
    // 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));
    try {
        resultCallback.getValue().onComplete();
    } catch (Exception ignored) {
    }
    assertFalse(docker.imageIsDownloaded(image));
    assertTrue("Should return true, new pull scheduled", 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) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) 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