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); } } }
public void removeContainer(String containerId, boolean removeVolumes) { this.readWriteLock.readLock().lock(); try { this.dockerClient.removeContainerCmd(containerId).withRemoveVolumes(removeVolumes).exec(); } finally { this.readWriteLock.readLock().unlock(); } }
private boolean isRunning() { final List<Container> all = client.listContainersCmd().withShowAll(true).withLabelFilter(label).exec(); final List<Container> running = client.listContainersCmd().withStatusFilter("running").withLabelFilter(label).exec(); all.stream() .filter(c -> running.isEmpty() || !running.get(0).getId().equals(c.getId())) .forEach(c -> client.removeContainerCmd(c.getId()).withForce(true).exec()); return !running.isEmpty(); }
.awaitStatusCode(); docker.removeContainerCmd(container.getId()).exec(); if (exitCode != 0) { throw new PluginException("Build Project Error ExitCode: " + exitCode);
/** * Produces a remove container request * * @param client * @param message * @return */ private RemoveContainerCmd executeRemoveContainerRequest(DockerClient client, Message message) { LOGGER.debug("Executing Docker Remove Container Request"); String containerId = DockerHelper.getProperty(DockerConstants.DOCKER_CONTAINER_ID, configuration, message, String.class); ObjectHelper.notNull(containerId, "Container ID must be specified"); RemoveContainerCmd removeContainerCmd = client.removeContainerCmd(containerId); Boolean force = DockerHelper.getProperty(DockerConstants.DOCKER_FORCE, configuration, message, Boolean.class); if (force != null) { removeContainerCmd.withForce(force); } Boolean removeVolumes = DockerHelper.getProperty(DockerConstants.DOCKER_REMOVE_VOLUMES, configuration, message, Boolean.class); if (removeVolumes != null) { removeContainerCmd.withRemoveVolumes(removeVolumes); } return removeContainerCmd; }
public void removeContainer(String containerId, boolean removeVolumes) { this.readWriteLock.readLock().lock(); try { this.dockerClient.removeContainerCmd(containerId).withRemoveVolumes(removeVolumes).exec(); } finally { this.readWriteLock.readLock().unlock(); } }
private void terminateContainer(DockerCloud dc, DockerClient client, Container container) { boolean gracefulFailed = false; try { terminateContainerGracefully(dc, container); } catch (TerminationException e) { gracefulFailed = true; } catch (ContainerIsTaintedException e) { LOGGER.warn("Container {} has been tampered with; skipping cleanup", container.getId(), e); return; } if (gracefulFailed) { try { Instant start = clock.instant(); client.removeContainerCmd(container.getId()).withForce(true).exec(); Instant stop = clock.instant(); executionStatistics.addContainerRemovalForce(Duration.between(start, stop).toMillis()); } catch (RuntimeException e) { LOGGER.warn("Forced termination of container {} failed with RuntimeException", container.getId(), e); executionStatistics.addContainerRemovalFailed(); } } }
@Override public void execute(Run<?, ?> build, Launcher launcher, TaskListener listener) throws DockerException { final PrintStream llog = listener.getLogger(); LOG.info("Stopping all containers"); llog.println("Stopping all containers"); for (DockerLaunchAction.Item containerItem : getLaunchAction(build).getRunning()) { LOG.info("Stopping container {}", containerItem.id); llog.println("Stopping container " + containerItem.id); containerItem.client.stopContainerCmd(containerItem.id).exec(); if (remove) { LOG.info("Removing container {}", containerItem.id); llog.println("Removing container " + containerItem.id); containerItem.client.removeContainerCmd(containerItem.id).exec(); } } }
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("Removing container: {}", containerId); try { dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).withForce(true).exec(); LOGGER.debug("Removed container and associated volume(s): {}", imageName); } catch (InternalServerErrorException e) {
@Override public void stopAndRemoveContainer(String containerId) { dockerClient.stopContainerCmd(containerId).exec(); dockerClient.removeContainerCmd(containerId).withRemoveVolumes(true).exec(); }
private ServiceInstance destroyInstance(ServiceInstance i, DockerClient client) { if(i.getContainerId() != null) { RemoveContainerCmd cmd = client.removeContainerCmd(i.getContainerId()).withForce(true); try { cmd.exec(); return i.withStatus(Status.TERMINATED); } catch (NotFoundException e) { logger.warning("No container with id " + i.getContainerId() + " found"); return i.withStatus(Status.TERMINATION_FAILED) .withStatusDetails(e.getMessage()); } } else { return i.withStatus(Status.TERMINATION_FAILED) .withStatusDetails("No container id found."); } }
@Override public void delete(ExecutionContext context) { super.delete(context); if (containerId == null) { throw new ProviderResourcesNotFoundException("Container has not been created yet"); } log.info("Deleting container with id " + containerId, context); connection.getDockerClient().removeContainerCmd(containerId).exec(); log.info("Deleted container with id " + containerId, context); containerId = null; }
} finally { try { client.removeContainerCmd(id).withRemoveVolumes(true).withForce(true).exec(); } catch (NotFoundException | InternalServerErrorException ignored) { log.debug("", ignored);
/** * removes a {@link Container} * * @param id - id of {@link Container} * @param withRemoveVolumes - true or false, Remove the volumes associated to the container */ public void removeContainer(String id, Boolean withRemoveVolumes) throws NotFoundException, NotModifiedException { dockerClient.removeContainerCmd(id).withForce(true).withRemoveVolumes(withRemoveVolumes).exec(); }
public void removeContainer(String containerName) { log.debug("Removing container {}", containerName); boolean removed = false; int count = 0; do { try { count++; getClient().removeContainerCmd(containerName).withRemoveVolumes(true).exec(); log.trace("*** Only for debugging: After Docker.removeContainer({}). Times: {}", containerName, count); removed = true; } catch (Throwable e) { if (count == 10) { log.trace("*** Only for debugging: Exception {} -> Docker.removeContainer({}).", containerName, e.getMessage()); } try { log.trace("Waiting for removing {}. Times: {}", containerName, count); Thread.sleep(WAIT_CONTAINER_POLL_TIMEOUT); } catch (InterruptedException e1) { // Nothing to do } } } while (!removed && count <= 10); }
@Override public void terminateVM(String instanceId) throws IaasException { try { this.docker.killContainerCmd(instanceId).exec(); this.docker.removeContainerCmd(instanceId).exec(); } catch(Exception e) { throw new IaasException(e); } }
public Void call() throws Exception { DockerClient client = DockerCommand.getClient(descriptor, cfgData.dockerUrlRes, cfgData.dockerVersionRes, cfgData.dockerCertPathRes, null); client.removeContainerCmd(id).withForce(force).withRemoveVolumes(removeVolumes).exec(); return null; }
public void removeContainer(String containerName) { log.debug("Removing container {}", containerName); boolean removed = false; int count = 0; do { try { count++; getClient().removeContainerCmd(containerName).withRemoveVolumes(true).exec(); log.trace("*** Only for debugging: After Docker.removeContainer({}). Times: {}", containerName, count); removed = true; } catch (Throwable e) { if (count == 10) { log.trace("*** Only for debugging: Exception {} -> Docker.removeContainer({}).", containerName, e.getMessage()); } try { log.trace("Waiting for removing {}. Times: {}", containerName, count); Thread.sleep(WAIT_CONTAINER_POLL_TIMEOUT); } catch (InterruptedException e1) { // Nothing to do } } } while (!removed && count <= 10); }