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