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();
}
}
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();
}
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());
}
}
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);
}
}
}
Aggregations