Search in sources :

Example 1 with DockerException

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

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

the class ContainerState method isHealthy.

/**
 * @return has the container health state 'healthy'?
 */
default boolean isHealthy() {
    if (getContainerId() == null) {
        return false;
    }
    try {
        InspectContainerResponse inspectContainerResponse = getCurrentContainerInfo();
        String healthStatus = inspectContainerResponse.getState().getHealth().getStatus();
        return healthStatus.equals(STATE_HEALTHY);
    } catch (DockerException e) {
        return false;
    }
}
Also used : InspectContainerResponse(com.github.dockerjava.api.command.InspectContainerResponse) DockerException(com.github.dockerjava.api.exception.DockerException)

Aggregations

InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)2 DockerException (com.github.dockerjava.api.exception.DockerException)2 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)1