com.spotify.docker.client.messages.Container Java Examples
The following examples show how to use
com.spotify.docker.client.messages.Container.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DockerUtil.java From SuitAgent with Apache License 2.0 | 6 votes |
private static List<String> getAllHostContainerId(){ String cacheKey = "allHostContainerIds"; int cacheTime = 28;//28秒缓存周期,保证一次采集job只需要访问一次docker即可 List<String> ids = (List<String>) getCache(cacheKey); if (ids != null){ return ids; }else { ids = new ArrayList<>(); } synchronized (LockForGetAllHostContainerId){ try { List<Container> containerList = getContainers(DockerClient.ListContainersParam.allContainers()); for (Container container : containerList) { ids.add(container.id()); } } catch (Exception e) { log.error("",e); } } setCache(cacheKey,ids,cacheTime); return ids; }
Example #2
Source File: DefaultDockerClient.java From docker-client with Apache License 2.0 | 6 votes |
@Override public List<Container> listContainers(final ListContainersParam... params) throws DockerException, InterruptedException { WebTarget resource = resource() .path("containers").path("json"); resource = addParameters(resource, params); try { return request(GET, CONTAINER_LIST, resource, resource.request(APPLICATION_JSON_TYPE)); } catch (DockerRequestException e) { switch (e.status()) { case 400: throw new BadParamException(getQueryParamMap(resource), e); default: throw e; } } }
Example #3
Source File: SystemTestBase.java From helios with Apache License 2.0 | 6 votes |
protected List<Container> listContainers(final DockerClient dockerClient, final String needle) throws DockerException, InterruptedException { final List<Container> containers = dockerClient.listContainers(); final List<Container> matches = Lists.newArrayList(); for (final Container container : containers) { if (container.names() != null) { for (final String name : container.names()) { if (name.contains(needle)) { matches.add(container); break; } } } } return matches; }
Example #4
Source File: Reaper.java From helios with Apache License 2.0 | 6 votes |
private void reap0(final Supplier<Set<String>> activeSupplier) throws DockerException, InterruptedException { final List<String> candidates = Lists.newArrayList(); final List<Container> containers = docker.listContainers(); for (final Container container : containers) { for (final String name : container.names()) { if (name.startsWith(prefix)) { candidates.add(container.id()); } } } // Get the active set after we've enumerated candidates to ensure that active set is fresh. // If the active set is fetched before enumerating candidates it might be stale and we might // mistakenly classify a container as not being in the active set. final Set<String> active = activeSupplier.get(); for (final String candidate : candidates) { if (!active.contains(candidate)) { reap(candidate); } } }
Example #5
Source File: SingularityExecutorCleanup.java From Singularity with Apache License 2.0 | 6 votes |
private void cleanDocker(Set<String> runningTaskIds) { try { for (Container container : dockerUtils.listContainers()) { for (String name : container.names()) { if (name.startsWith(executorConfiguration.getDockerPrefix())) { if ( !runningTaskIds.contains( name.substring(executorConfiguration.getDockerPrefix().length()) ) ) { stopContainer(container); } } } } } catch (Exception e) { LOG.error("Could not get list of Docker containers", e); exceptionNotifier.notify( String.format("Error listing docker containers (%s)", e.getMessage()), e, Collections.<String, String>emptyMap() ); } }
Example #6
Source File: SingularityExecutorCleanup.java From Singularity with Apache License 2.0 | 6 votes |
private void stopContainer(Container container) { try { ContainerInfo containerInfo = dockerUtils.inspectContainer(container.id()); if (containerInfo.state().running()) { dockerUtils.stopContainer( container.id(), executorConfiguration.getDockerStopTimeout() ); LOG.debug("Forcefully stopped container {}", container.names()); } dockerUtils.removeContainer(container.id(), true); LOG.debug("Removed container {}", container.names()); } catch (Exception e) { LOG.error("Failed to stop or remove container {}", container.names(), e); exceptionNotifier.notify( String.format("Failed stopping container (%s)", e.getMessage()), e, Collections.<String, String>emptyMap() ); } }
Example #7
Source File: DockerUtil.java From SuitAgent with Apache License 2.0 | 5 votes |
/** * 获取主机上所有运行容器的proc信息 * @return */ public static List<ContainerProcInfoToHost> getAllHostContainerProcInfos(){ String cacheKey = "ALL_HOST_CONTAINER_PROC_INFOS"; List<ContainerProcInfoToHost> procInfoToHosts = (List<ContainerProcInfoToHost>) getCache(cacheKey); if (procInfoToHosts != null){ //返回缓存数据 return procInfoToHosts; }else { procInfoToHosts = new ArrayList<>(); } synchronized (LockForGetAllHostContainerProcInfos){ try { List<Container> containers = getContainers(DockerClient.ListContainersParam.withStatusRunning()); for (Container container : containers) { ContainerInfo info = getContainerInfo(container.id()); if (info != null) { String pid = String.valueOf(info.state().pid()); procInfoToHosts.add(new ContainerProcInfoToHost(container.id(),PROC_HOST_VOLUME + "/" + pid + "/root",pid)); } } } catch (Exception e) { log.error("",e); } } if (!procInfoToHosts.isEmpty()) { //设置缓存 setCache(cacheKey,procInfoToHosts); } return procInfoToHosts; }
Example #8
Source File: BaseTest.java From docker-swarm-elastic-agent-plugin with Apache License 2.0 | 5 votes |
protected List<Container> waitForContainerToStart(DockerService service, final int waitInSeconds) throws DockerException, InterruptedException { List<Container> containers = null; final AtomicInteger retry = new AtomicInteger(); do { containers = docker.listContainers(DockerClient.ListContainersParam.withLabel(SWARM_SERVICE_NAME, service.name()), DockerClient.ListContainersParam.allContainers()); Thread.sleep(1000); } while (containers.isEmpty() && retry.incrementAndGet() < waitInSeconds); if (containers.isEmpty()) { fail("Should start container."); } return containers; }
Example #9
Source File: DockerContainers.java From docker-elastic-agents-plugin with Apache License 2.0 | 5 votes |
@Override public void refreshAll(ClusterProfileProperties clusterProfileProperties) throws Exception { if (!refreshed) { DockerClient docker = docker(clusterProfileProperties); List<Container> containers = docker.listContainers(DockerClient.ListContainersParam.withLabel(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID)); for (Container container : containers) { register(DockerContainer.fromContainerInfo(docker.inspectContainer(container.id()))); } refreshed = true; } }
Example #10
Source File: ZooKeeperClusterIdTest.java From helios with Apache License 2.0 | 5 votes |
@Test public void testAgent() throws Exception { startDefaultMaster("--zk-cluster-id=" + zkClusterId); startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Create job and deploy it final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND); deployJob(jobId, testHost()); final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING); final String containerId = runningStatus.getContainerId(); // Delete the config node which contains the cluster ID and all the job definitions zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config"); // Sleep for a second so agent has a chance to react to deletion Thread.sleep(1000); // Make sure the agent didn't stop the job try (final DockerClient docker = getNewDockerClient()) { final List<Container> containers = docker.listContainers(); final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) { @Override protected boolean matchesSafely(Container container) { return container.id().equals(containerId); } }; assertContainersMatch(containers, containerIdMatcher); } }
Example #11
Source File: DockerUtils.java From Singularity with Apache License 2.0 | 5 votes |
public List<Container> listContainers() throws DockerException { Callable<List<Container>> callable = new Callable<List<Container>>() { @Override public List<Container> call() throws Exception { return dockerClient.listContainers(); } }; try { return callWithRetriesAndTimeout(callable); } catch (Exception e) { throw new DockerException(e); } }
Example #12
Source File: DockerUtil.java From SuitAgent with Apache License 2.0 | 4 votes |
/** * 获取容器列表 * @param containersParam * @return */ public static List<Container> getContainers(DockerClient.ListContainersParam containersParam) { String cacheKey = "getContainer" + containersParam.value(); final List<Container> containers = (List<Container>) CacheByTimeUtil.getCache(cacheKey); if (containers != null) { return containers; } try { int timeOut = 45; final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1); //阻塞队列异步执行 ExecuteThreadUtil.execute(() -> { try { List<Container> containerList = docker.listContainers(containersParam); setCache(cacheKey, containerList); blockingQueue.offer(containerList); } catch (Throwable t) { blockingQueue.offer(t); } }); //超时 Object result = BlockingQueueUtil.getResult(blockingQueue, timeOut, TimeUnit.SECONDS); blockingQueue.clear(); if (result instanceof List) { return (List<Container>) result; }else if (result == null) { log.error("docker 容器 List 获取{}秒超",timeOut); return new ArrayList<>(); }else if (result instanceof Throwable) { log.error("docker 容器 List 获取异常",result); return new ArrayList<>(); }else { log.error("未知结果类型:{}",result); return new ArrayList<>(); } } catch (Exception e) { log.error("",e); return new ArrayList<>(); } }
Example #13
Source File: ZooKeeperClusterIdTest.java From helios with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") private void assertContainersMatch(final List<Container> containers, final CustomTypeSafeMatcher<Container> containerIdMatcher) { assertThat(containers, hasItems(new CustomTypeSafeMatcher[]{ containerIdMatcher })); }
Example #14
Source File: NamespaceTest.java From helios with Apache License 2.0 | 4 votes |
@Test public void test() throws Exception { startDefaultMaster(); final String id = "test-" + Integer.toHexString(new SecureRandom().nextInt()); final String namespace = "helios-" + id; final HeliosClient client = defaultClient(); startDefaultAgent(testHost(), "--id=" + id); // Create a job final Job job = Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setCommand(IDLE_COMMAND) .build(); final JobId jobId = job.getId(); final CreateJobResponse created = client.createJob(job).get(); assertEquals(CreateJobResponse.Status.OK, created.getStatus()); // Wait for agent to come up awaitHostRegistered(client, testHost(), LONG_WAIT_SECONDS, SECONDS); awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Deploy the job on the agent final Deployment deployment = Deployment.of(jobId, START); final JobDeployResponse deployed = client.deploy(deployment, testHost()).get(); assertEquals(JobDeployResponse.Status.OK, deployed.getStatus()); awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS); try (final DockerClient docker = getNewDockerClient()) { final List<Container> containers = docker.listContainers(); Container jobContainer = null; for (final Container container : containers) { for (final String name : container.names()) { if (name.startsWith("/" + namespace)) { jobContainer = container; } } } assertNotNull(jobContainer); } }
Example #15
Source File: DockerClient.java From docker-client with Apache License 2.0 | 2 votes |
/** * List docker containers. * * @param params Container listing and filtering options. * @return A list of containers. * @throws BadParamException * if one or more params were bad (400) * @throws DockerException if a server error occurred (500) * @throws InterruptedException If the thread is interrupted */ List<Container> listContainers(ListContainersParam... params) throws DockerException, InterruptedException;