Search in sources :

Example 6 with DockerClientException

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

the class DockerService2 method endContainer.

/**
 ******************************
 */
/**
 *** End execution methods ****
 */
/**
 ******************************
 */
public void endContainer(String containerName) {
    DockerClient dockerClient = this.getDockerClient();
    if (existsContainer(containerName)) {
        String containerId = getContainerIdByName(containerName);
        int timeout = 60;
        try {
            logger.info("Stopping " + containerName + " container");
            dockerClient.stopContainerCmd(containerId).withTimeout(timeout).exec();
            // Wait
            dockerClient.waitContainerCmd(containerId).exec(new WaitContainerResultCallback()).awaitStatusCode();
        } catch (DockerClientException e) {
        // throw new TJobStoppedException();
        } catch (NotModifiedException e) {
            logger.info("Container " + containerName + " is already stopped");
        } catch (Exception e) {
            logger.info("Error during stop " + containerName + " container {}", e);
        } finally {
            try {
                logger.info("Removing " + containerName + " container");
                dockerClient.removeContainerCmd(containerId).exec();
            } catch (DockerClientException e) {
            // throw new TJobStoppedException();
            } catch (Exception e) {
                logger.info("Error during remove " + containerName + " container");
            }
            createdContainers.remove(containerId);
        }
    } else {
        logger.info("Could not end " + containerName + " container -> Not started.");
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) WaitContainerResultCallback(com.github.dockerjava.core.command.WaitContainerResultCallback) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NotModifiedException(com.github.dockerjava.api.exception.NotModifiedException)

Example 7 with DockerClientException

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

the class RemoteDockerImage method resolve.

@Override
protected final String resolve() {
    Profiler profiler = new Profiler("Rule creation - prefetch image");
    Logger logger = DockerLoggerFactory.getLogger(imageName.toString());
    profiler.setLogger(logger);
    Profiler nested = profiler.startNested("Obtaining client");
    DockerClient dockerClient = DockerClientFactory.instance().client();
    try {
        nested.stop();
        profiler.start("Check local images");
        int attempts = 0;
        DockerClientException lastException = null;
        while (true) {
            // Does our cache already know the image?
            if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
                logger.trace("{} is already in image name cache", imageName);
                break;
            }
            // Update the cache
            ListImagesCmd listImagesCmd = dockerClient.listImagesCmd();
            if (Boolean.parseBoolean(System.getProperty("useFilter"))) {
                listImagesCmd = listImagesCmd.withImageNameFilter(imageName.toString());
            }
            List<Image> updatedImages = listImagesCmd.exec();
            updatedImages.stream().map(Image::getRepoTags).filter(Objects::nonNull).flatMap(Stream::of).map(DockerImageName::new).collect(Collectors.toCollection(() -> AVAILABLE_IMAGE_NAME_CACHE));
            // And now?
            if (AVAILABLE_IMAGE_NAME_CACHE.contains(imageName)) {
                logger.trace("{} is in image name cache following listing of images", imageName);
                break;
            }
            // Log only on first attempt
            if (attempts == 0) {
                logger.info("Pulling docker image: {}. Please be patient; this may take some time but only needs to be done once.", imageName);
                profiler.start("Pull image");
            }
            if (attempts++ >= 3) {
                logger.error("Retry limit reached while trying to pull image: {}. Please check output of `docker pull {}`", imageName, imageName);
                throw new ContainerFetchException("Retry limit reached while trying to pull image: " + imageName, lastException);
            }
            // The image is not available locally - pull it
            try {
                final PullImageResultCallback callback = new PullImageResultCallback();
                dockerClient.pullImageCmd(imageName.getUnversionedPart()).withTag(imageName.getVersionPart()).exec(callback);
                callback.awaitSuccess();
                AVAILABLE_IMAGE_NAME_CACHE.add(imageName);
                break;
            } catch (DockerClientException e) {
                lastException = e;
            }
        }
        return imageName.toString();
    } catch (DockerClientException e) {
        throw new ContainerFetchException("Failed to get Docker client for " + imageName, e);
    } finally {
        profiler.stop().log();
    }
}
Also used : Profiler(org.slf4j.profiler.Profiler) DockerClient(com.github.dockerjava.api.DockerClient) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) ListImagesCmd(com.github.dockerjava.api.command.ListImagesCmd) Stream(java.util.stream.Stream) Logger(org.slf4j.Logger) Image(com.github.dockerjava.api.model.Image) ContainerFetchException(org.testcontainers.containers.ContainerFetchException)

Aggregations

DockerClientException (com.github.dockerjava.api.exception.DockerClientException)7 DockerClient (com.github.dockerjava.api.DockerClient)3 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)3 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)3 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)3 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)2 IOException (java.io.IOException)2 ListImagesCmd (com.github.dockerjava.api.command.ListImagesCmd)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1 Image (com.github.dockerjava.api.model.Image)1 WaitContainerResultCallback (com.github.dockerjava.core.command.WaitContainerResultCallback)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Stream (java.util.stream.Stream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Logger (org.slf4j.Logger)1 Profiler (org.slf4j.profiler.Profiler)1 ContainerFetchException (org.testcontainers.containers.ContainerFetchException)1 SAXException (org.xml.sax.SAXException)1