Search in sources :

Example 1 with InternalServerErrorException

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

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

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

use of com.github.dockerjava.api.exception.InternalServerErrorException 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)

Aggregations

InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)4 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)4 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)2 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)2 DockerClient (com.github.dockerjava.api.DockerClient)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 DockerException (com.github.dockerjava.api.exception.DockerException)1