com.github.dockerjava.api.model.Container Java Examples
The following examples show how to use
com.github.dockerjava.api.model.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: ITTestBase.java From hudi with Apache License 2.0 | 7 votes |
private TestExecStartResultCallback executeCommandInDocker(String containerName, String[] command, boolean expectedToSucceed) throws Exception { Container sparkWorkerContainer = runningContainers.get(containerName); ExecCreateCmd cmd = dockerClient.execCreateCmd(sparkWorkerContainer.getId()).withCmd(command).withAttachStdout(true) .withAttachStderr(true); ExecCreateCmdResponse createCmdResponse = cmd.exec(); TestExecStartResultCallback callback = new TestExecStartResultCallback(new ByteArrayOutputStream(), new ByteArrayOutputStream()); dockerClient.execStartCmd(createCmdResponse.getId()).withDetach(false).withTty(false).exec(callback) .awaitCompletion(); int exitCode = dockerClient.inspectExecCmd(createCmdResponse.getId()).exec().getExitCode(); LOG.info("Exit code for command : " + exitCode); LOG.error("\n\n ###### Stdout #######\n" + callback.getStdout().toString()); LOG.error("\n\n ###### Stderr #######\n" + callback.getStderr().toString()); if (expectedToSucceed) { assertEquals(0, exitCode, "Command (" + Arrays.toString(command) + ") expected to succeed. Exit (" + exitCode + ")"); } else { assertNotEquals(0, exitCode, "Command (" + Arrays.toString(command) + ") expected to fail. Exit (" + exitCode + ")"); } cmd.close(); return callback; }
Example #2
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 6 votes |
/** * Filters the set based on the container name * * @param pattern regular expression pattern of the container name * @return filtered set */ public DockerContainersUtil filterByName(String pattern) { if (this.containers == null) { return this; } List<Container> matched = new ArrayList<>(); for (Container container : containers) { String[] names = container.getNames(); for (String name : names) { // all names start with '/' if (name.substring(1).matches(pattern)) { matched.add(container); } } } return new DockerContainersUtil(matched); }
Example #3
Source File: DockerContainerManagementService.java From wecube-platform with Apache License 2.0 | 6 votes |
@Override public void startItem(ResourceItem item) { String containerName = item.getName(); DockerClient dockerClient = newDockerClient(item.getResourceServer().getHost()); List<Container> containers = dockerClient.listContainersCmd().withShowAll(true) .withFilter("name", Arrays.asList(containerName)).exec(); if (containers.isEmpty()) { throw new WecubeCoreException(String .format("Failed to start container with name [%s] : Container is not exists.", containerName)); } Container container = containers.get(0); if (!container.getState().equals("running")) { dockerClient.startContainerCmd(containerName).exec(); } else { log.warn("The container {} is already running.", containerName); } }
Example #4
Source File: DockerContainerManagementService.java From wecube-platform with Apache License 2.0 | 6 votes |
@Override public void deleteItem(ResourceItem item) { String containerName = item.getName(); DockerClient dockerClient = newDockerClient(item.getResourceServer().getHost()); List<Container> containers = dockerClient.listContainersCmd().withShowAll(true) .withFilter("name", Arrays.asList(containerName)).exec(); if (containers.isEmpty()) { log.warn("The container {} to be deleted is not existed.", containerName); return; } Container container = containers.get(0); if (!container.getState().equals("running")) { dockerClient.removeContainerCmd(containerName).exec(); } else { dockerClient.removeContainerCmd(containerName).withForce(true).exec(); } }
Example #5
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 6 votes |
@Test public void testNameFilter() throws Exception { String testUUID = testLabel.get("test"); String id1, id2; id1 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .withName("nameFilterTest1-" + testUUID) .exec() .getId(); id2 = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .withName("nameFilterTest2-" + testUUID) .exec() .getId(); List<Container> filteredContainers = dockerRule.getClient().listContainersCmd() .withShowAll(true) .withNameFilter(asList("nameFilterTest1-" + testUUID, "nameFilterTest2-" + testUUID)) .exec(); assertThat(filteredContainers.size(), is(2)); assertThat(filteredContainers.get(0).getId(), isOneOf(id1, id2)); assertThat(filteredContainers.get(1).getId(), isOneOf(id1, id2)); }
Example #6
Source File: DockerHelper.java From dynamo-cassandra-proxy with Apache License 2.0 | 6 votes |
private Container searchContainer(String name) { ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("running")); listContainersCmd.getFilters().put("name", Arrays.asList(name)); List<Container> runningContainers = null; try { runningContainers = listContainersCmd.exec(); } catch (Exception e) { e.printStackTrace(); logger.error("Unable to contact docker, make sure docker is up and try again."); System.exit(1); } if (runningContainers.size() >= 1) { //Container test = runningContainers.get(0); logger.info(String.format("The container %s is already running", name)); return runningContainers.get(0); } return null; }
Example #7
Source File: RemoveImageCmdIT.java From docker-java with Apache License 2.0 | 6 votes |
@Test public void removeImage() throws DockerException, InterruptedException { CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox").withCmd("sleep", "9999").exec(); LOG.info("Created container: {}", container.toString()); assertThat(container.getId(), not(is(emptyString()))); dockerRule.getClient().startContainerCmd(container.getId()).exec(); LOG.info("Committing container {}", container.toString()); String imageId = dockerRule.getClient().commitCmd(container.getId()).exec(); dockerRule.getClient().stopContainerCmd(container.getId()).exec(); dockerRule.getClient().removeContainerCmd(container.getId()).exec(); LOG.info("Removing image: {}", imageId); dockerRule.getClient().removeImageCmd(imageId).exec(); List<Container> containers = dockerRule.getClient().listContainersCmd().withShowAll(true).exec(); Matcher matcher = not(hasItem(hasField("id", startsWith(imageId)))); assertThat(containers, matcher); }
Example #8
Source File: DockerHandlerWithContainerTest.java From roboconf-platform with Apache License 2.0 | 6 votes |
@Test public void testDockerUtils_onLimits() { DockerUtils.deleteImageIfItExists( null, this.docker ); Assert.assertTrue( "No exception is thrown trying to delete a null image ID.", true ); DockerUtils.deleteImageIfItExists( "bla 11 4 2 bla", this.docker ); Assert.assertTrue( "No exception is thrown trying to delete something that does not exist.", true ); Container container = DockerUtils.findContainerByIdOrByName( "bla 11 4 2 bla", this.docker ); Assert.assertNull( container ); Image image = DockerUtils.findImageByIdOrByTag( null, this.docker ); Assert.assertNull( image ); image = DockerUtils.findImageByIdOrByTag( "invalid", this.docker ); Assert.assertNull( image ); }
Example #9
Source File: DockerUtils.java From roboconf-platform with Apache License 2.0 | 6 votes |
/** * Finds a container by ID or by name. * @param name the container ID or name (not null) * @param dockerClient a Docker client * @return a container, or null if none was found */ public static Container findContainerByIdOrByName( String name, DockerClient dockerClient ) { Container result = null; List<Container> containers = dockerClient.listContainersCmd().withShowAll( true ).exec(); for( Container container : containers ) { List<String> names = Arrays.asList( container.getNames()); // Docker containers are prefixed with '/'. // At least, those we created, since their parent is the Docker daemon. if( container.getId().equals( name ) || names.contains( "/" + name )) { result = container; break; } } return result; }
Example #10
Source File: DeploymentSystemTest.java From logstash with Apache License 2.0 | 6 votes |
@Test public void willStartNewExecutorIfOldExecutorFails() throws Exception { deployScheduler("logstash", null, true, null, false); Function<String, Stream<Container>> getLogstashExecutorsSince = containerId -> dockerClient .listContainersCmd() .withSince(containerId) .exec() .stream() .filter(container -> container.getImage().endsWith("/logstash-executor:latest")); await().atMost(1, TimeUnit.MINUTES).pollDelay(1, SECONDS).until(() -> { long count = getLogstashExecutorsSince.apply(cluster.getAgents().get(0).getContainerId()).count(); LOGGER.info("There are " + count + " executors since " + cluster.getAgents().get(0).getContainerId()); assertEquals(1, count); }); final String slaveToKillContainerId = getLogstashExecutorsSince.apply(cluster.getAgents().get(0).getContainerId()).findFirst().map(Container::getId).orElseThrow(() -> new RuntimeException("Unable to find logstash container")); dockerClient.killContainerCmd(slaveToKillContainerId).exec(); await().atMost(1, TimeUnit.MINUTES).pollDelay(1, SECONDS).until(() -> { assertEquals(1, getLogstashExecutorsSince.apply(slaveToKillContainerId).count()); }); }
Example #11
Source File: DockerService.java From vertx-service-discovery with Apache License 2.0 | 6 votes |
public DockerService(Container container, String host) { this.host = host; containerId = container.getId(); containerNames = Arrays.stream(container.getNames()).collect(Collectors.toList()); if (!containerNames.isEmpty()) { name = containerNames.get(0); } else { name = containerId; } for (ContainerPort port : container.getPorts()) { Record record = createRecord(container, port); if (record != null) { records.add(record); } } }
Example #12
Source File: DockerServiceImporter.java From vertx-service-discovery with Apache License 2.0 | 6 votes |
private boolean isNotRunning(String containerId, List<Container> running) { if (running == null) { // No running container return true; } for (Container container : running) { if (container.getId().equalsIgnoreCase(containerId)) { // Found in the running list return false; } } // Not found in the running list return true; }
Example #13
Source File: DockerBridgeTest.java From vertx-service-discovery with Apache License 2.0 | 6 votes |
@Before public void setUp() { init(); client = DockerClientBuilder.getInstance().build(); List<Container> running = client.listContainersCmd().withStatusFilter(Collections.singletonList("running")).exec(); if (running != null) { running.forEach(container -> client.stopContainerCmd(container.getId()).exec()); } vertx = Vertx.vertx(); discovery = ServiceDiscovery.create(vertx); bridge = new DockerServiceImporter(); discovery.registerServiceImporter(bridge, new JsonObject().put("scan-period", -1)); await().until(() -> bridge.started); }
Example #14
Source File: ContainerUtil.java From presto with Apache License 2.0 | 6 votes |
public static void killContainers(DockerClient dockerClient, Function<ListContainersCmd, ListContainersCmd> filter) { while (true) { ListContainersCmd listContainersCmd = filter.apply(dockerClient.listContainersCmd() .withShowAll(true)); List<Container> containers = listContainersCmd.exec(); if (containers.isEmpty()) { break; } for (Container container : containers) { try { dockerClient.removeContainerCmd(container.getId()) .withForce(true) .exec(); } catch (ConflictException | NotFoundException ignored) { } } } }
Example #15
Source File: InspectContainerCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@Test public void inspectContainerNodeProperty() throws DockerException { Map<String, String> label = Collections.singletonMap("inspectContainerNodeProperty", UUID.randomUUID().toString()); CreateContainerResponse container = dockerRule.getClient().createContainerCmd("busybox") .withLabels(label) .exec(); Container containerResult = dockerRule.getClient().listContainersCmd() .withShowAll(true) .withLabelFilter(label) .exec() .get(0); String name = containerResult.getNames()[0]; InspectContainerResponse containerInfo = dockerRule.getClient().inspectContainerCmd(container.getId()).exec(); InspectContainerResponse.Node node = containerInfo.getNode(); if (isSwarm(dockerRule.getClient())) { assertThat(node, is(notNullValue())); assertThat(node.getAddr(), is(notNullValue())); assertThat(node.getId(), is(notNullValue())); assertThat(node.getIp(), is(notNullValue())); assertThat(node.getLabels(), is(notNullValue())); assertThat(node.getLabels().get("com.github.dockerjava.test"), is("docker-java")); assertThat(node.getCpus(), is(greaterThanOrEqualTo(1))); assertThat(node.getMemory(), is(greaterThanOrEqualTo(64 * 1024 * 1024L))); assertThat("/" + node.getName() + containerInfo.getName(), is(name)); } else { assertThat(node, is(nullValue())); } }
Example #16
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@Test public void testNetworkFilter() throws Exception { String id; dockerRule.getClient().createNetworkCmd() .withName("TestFilterNetwork") .withDriver("bridge") .exec(); id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .withHostConfig(newHostConfig() .withNetworkMode("TestFilterNetwork")) .exec() .getId(); dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .exec(); List<Container> filteredContainers = dockerRule.getClient().listContainersCmd() .withShowAll(true) .withLabelFilter(testLabel) .withNetworkFilter(singletonList("TestFilterNetwork")) .exec(); assertThat(filteredContainers.size(), is(1)); assertThat(filteredContainers.get(0).getId(), is(id)); }
Example #17
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@Test public void testVolumeFilter() throws Exception { String id; dockerRule.getClient().createVolumeCmd() .withName("TestFilterVolume") .withDriver("local") .exec(); id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .withHostConfig(newHostConfig() .withBinds(new Bind("TestFilterVolume", new Volume("/test")))) .exec() .getId(); dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .exec(); List<Container> filteredContainers = dockerRule.getClient().listContainersCmd() .withShowAll(true) .withLabelFilter(testLabel) .withVolumeFilter(singletonList("TestFilterVolume")) .exec(); assertThat(filteredContainers.size(), is(1)); assertThat(filteredContainers.get(0).getId(), is(id)); }
Example #18
Source File: DockerHandler.java From roboconf-platform with Apache License 2.0 | 5 votes |
@Override public void terminateMachine( TargetHandlerParameters parameters, String machineId ) throws TargetException { this.logger.fine( "Terminating machine " + machineId ); try { cancelMachineConfigurator( machineId ); DockerClient dockerClient = DockerUtils.createDockerClient( parameters.getTargetProperties()); Container container = DockerUtils.findContainerByIdOrByName( machineId, dockerClient ); // The case "container == null" is possible. // Indeed, it may have been killed manually. This method will then // just mark the Roboconf instance as "not deployed" without throwing an exception. if( container != null ) { // Stop the container ContainerState state = DockerUtils.getContainerState( machineId, dockerClient ); if( state != null && ( extractBoolean( state.getRunning()) || extractBoolean( state.getPaused()))) dockerClient.killContainerCmd( container.getId()).exec(); dockerClient.removeContainerCmd( container.getId()).withForce( true ).exec(); // Delete the volume we used to share user data // (container names are prefixed by their parent, here "/"). // See https://github.com/moby/moby/issues/6705 String containerName = container.getNames()[ 0 ].substring( 1 ); File userDataDir = this.containerIdToVolume.remove( containerName ); Utils.deleteFilesRecursivelyAndQuietly( userDataDir ); } } catch( Exception e ) { throw new TargetException( e ); } }
Example #19
Source File: TestableDockerContainerWatchdog.java From docker-plugin with MIT License | 5 votes |
public static Container createMockedContainer(String containerId, String status, long createdOn, Map<String, String> labels) { Container result = Mockito.mock(Container.class); Mockito.when(result.getId()).thenReturn(containerId); Mockito.when(result.getStatus()).thenReturn(status); Mockito.when(result.getCreated()).thenReturn(createdOn); Mockito.when(result.getLabels()).thenReturn(labels); return result; }
Example #20
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@After public void tearDown() { //remove all containers created by this test List<Container> containers = dockerRule.getClient().listContainersCmd() .withLabelFilter(testLabel) .withShowAll(true) .exec(); for (Container container : containers) { removeContainer(container.getId()); } }
Example #21
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@Test public void testAncestorFilter() throws Exception { // ancestor filters are broken in swarm // https://github.com/docker/swarm/issues/1716 DockerAssume.assumeNotSwarm(dockerRule.getClient()); dockerRule.getClient().pullImageCmd("busybox") .withTag("1.24") .start() .awaitCompletion(); dockerRule.getClient().createContainerCmd("busybox:1.24") .withLabels(testLabel) .exec(); String imageId = dockerRule.getClient().inspectImageCmd(DEFAULT_IMAGE).exec().getId(); String id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .exec() .getId(); List<Container> filteredContainers = dockerRule.getClient().listContainersCmd() .withLabelFilter(testLabel) .withShowAll(true) .withAncestorFilter(singletonList(imageId)) .exec(); assertThat(filteredContainers.size(), is(1)); assertThat(filteredContainers.get(0).getId(), is(id)); }
Example #22
Source File: MesosClusterContainersFactory.java From minimesos with Apache License 2.0 | 5 votes |
private void restoreMapToPorts(MesosCluster cluster, Container container) { // Restore "map ports to host" attribute ContainerPort[] ports = container.getPorts(); if (ports != null) { for (ContainerPort port : ports) { if (port.getIp() != null && port.getPrivatePort() == MesosMasterConfig.MESOS_MASTER_PORT) { cluster.setMapPortsToHost(true); } } } }
Example #23
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 5 votes |
/** * @param containerId id of the container to retrieve * @return container or null */ public static Container getContainer(String containerId) { List<Container> containers = DockerClientFactory.build().listContainersCmd().withShowAll(true).exec(); Container container = null; if (containers != null && !containers.isEmpty()) { Optional<Container> optional = containers.stream().filter(c -> c.getId().equals(containerId)).findFirst(); if (optional.isPresent()) { container = optional.get(); } } return container; }
Example #24
Source File: ListContainersCmdIT.java From docker-java with Apache License 2.0 | 5 votes |
@Test public void testExitedFilter() throws Exception { dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .exec(); String id = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE) .withLabels(testLabel) .withCmd("sh", "-c", "exit 42") .exec() .getId(); dockerRule.getClient().startContainerCmd(id).exec(); Integer status = dockerRule.getClient().waitContainerCmd(id).start() .awaitStatusCode(); assertThat(status, is(42)); List<Container> filteredContainers = dockerRule.getClient().listContainersCmd() .withLabelFilter(testLabel) .withShowAll(true) .withExitedFilter(42) .exec(); assertThat(filteredContainers.size(), is(1)); assertThat(filteredContainers.get(0).getId(), is(id)); }
Example #25
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 5 votes |
/** * @return IP addresses of containers */ public Set<String> getIpAddresses() { Set<String> ips = new HashSet<>(); if (containers != null) { for (Container container : containers) { ips.add(getIpAddress(container.getId())); } } return ips; }
Example #26
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 5 votes |
/** * Removes all containers in the util object * * @param ignoreFailure - use <code>true</code> if you expect containers might be stopped by this time */ public DockerContainersUtil kill(boolean ignoreFailure) { if (containers != null) { for (Container container : containers) { try { DockerClientFactory.build().killContainerCmd(container.getId()).exec(); } catch (DockerException failure) { if (!ignoreFailure) { throw failure; } } } } return this; }
Example #27
Source File: ListContainersCmdExec.java From docker-java with Apache License 2.0 | 5 votes |
@Override protected List<Container> execute(ListContainersCmd command) { WebTarget webTarget = getBaseResource().path("/containers/json").queryParam("since", command.getSinceId()) .queryParam("before", command.getBeforeId()); webTarget = booleanQueryParam(webTarget, "all", command.hasShowAllEnabled()); webTarget = booleanQueryParam(webTarget, "size", command.hasShowSizeEnabled()); if (command.getLimit() != null && command.getLimit() >= 0) { webTarget = webTarget.queryParam("limit", String.valueOf(command.getLimit())); } if (command.getFilters() != null && !command.getFilters().isEmpty()) { webTarget = webTarget .queryParam("filters", FiltersEncoder.jsonEncode(command.getFilters())); } LOGGER.trace("GET: {}", webTarget); List<Container> containers = webTarget.request().accept(MediaType.APPLICATION_JSON) .get(new TypeReference<List<Container>>() { }); LOGGER.trace("Response: {}", containers); return containers; }
Example #28
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 5 votes |
/** * Filters the set based on the constainer name * * @param pattern regular expression pattern of the container name * @return filtered set */ public DockerContainersUtil filterByImage(String pattern) { if (this.containers == null) { return this; } List<Container> matched = new ArrayList<>(); for (Container container : containers) { if (container.getImage().matches(pattern)) { matched.add(container); } } return new DockerContainersUtil(matched); }
Example #29
Source File: DockerContainersUtil.java From minimesos with Apache License 2.0 | 5 votes |
/** * Removes all containers in the util object */ public void remove() { if (containers != null) { for (Container container : containers) { DockerClientFactory.build().removeContainerCmd(container.getId()).withForce(true).withRemoveVolumes(true).exec(); } } }
Example #30
Source File: DockerContainerWatchdogTest.java From docker-plugin with MIT License | 5 votes |
@Test public void testContainerExistsButSlaveIsMissingRemoveVolumesLabelMissing() throws IOException, InterruptedException { TestableDockerContainerWatchdog subject = new TestableDockerContainerWatchdog(); final String nodeName = "unittest-12345"; final String containerId = UUID.randomUUID().toString(); /* setup of cloud */ List<DockerCloud> listOfCloud = new LinkedList<>(); Map<String, String> labelMap = new HashMap<>(); labelMap.put(DockerContainerLabelKeys.NODE_NAME, nodeName); labelMap.put(DockerContainerLabelKeys.TEMPLATE_NAME, "unittesttemplate"); // NB The removeVolumes label is not set here List<Container> containerList = new LinkedList<>(); Container c = TestableDockerContainerWatchdog.createMockedContainer(containerId, "Running", 0L, labelMap); containerList.add(c); DockerAPI dockerApi = TestableDockerContainerWatchdog.createMockedDockerAPI(containerList); DockerCloud cloud = new DockerCloud("unittestcloud", dockerApi, new LinkedList<>()); listOfCloud.add(cloud); subject.setAllClouds(listOfCloud); /* setup of nodes */ LinkedList<Node> allNodes = new LinkedList<>(); Node n = TestableDockerContainerWatchdog.createMockedDockerTransientNode(UUID.randomUUID().toString(), nodeName, cloud, false); allNodes.add(n); subject.setAllNodes(allNodes); subject.runExecute(); List<String> containersRemoved = subject.getContainersRemoved(); Assert.assertEquals(0, containersRemoved.size()); Assert.assertEquals(0, subject.getAllRemovedNodes().size()); }