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(); }
/** * Counts the number of instances currently running in Docker that are using * the specified image. * <p> * <b>WARNING:</b> This method can be slow so it should be called sparingly. * * @param imageName * If null, then all instances belonging to this Jenkins instance * are counted. Otherwise, only those started with the specified * image are counted. */ public int countContainersInDocker(final String imageName) throws Exception { final Map<String, String> labelFilter = new HashMap<>(); labelFilter.put(DockerContainerLabelKeys.JENKINS_INSTANCE_ID, DockerTemplateBase.getJenkinsInstanceIdForContainerLabel()); if (imageName != null) { labelFilter.put(DockerContainerLabelKeys.CONTAINER_IMAGE, imageName); } final List<?> containers; try(final DockerClient client = dockerApi.getClient()) { containers = client.listContainersCmd().withLabelFilter(labelFilter).exec(); } final int count = containers.size(); return count; }
private void doStop() { client.listContainersCmd().withStatusFilter("running").withLabelFilter(label).exec().stream() .filter(c -> c.getId() != null) .forEach(c -> client.stopContainerCmd(c.getId()).exec()); }
containerList = client.listContainersCmd() .withShowAll(true) .withLabelFilter(labelFilter) .exec(); } catch (Exception e) {