com.github.dockerjava.api.command.ListContainersCmd Java Examples

The following examples show how to use com.github.dockerjava.api.command.ListContainersCmd. 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: DockerHelper.java    From dynamo-cassandra-proxy with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: TestableDockerContainerWatchdog.java    From docker-plugin with MIT License 6 votes vote down vote up
public static DockerAPI createMockedDockerAPI(List<Container> containerList) {
    DockerAPI result = Mockito.mock(DockerAPI.class);
    DockerClient client = Mockito.mock(DockerClient.class);
    Mockito.when(result.getClient()).thenReturn(client);
    DockerServerEndpoint dockerServerEndpoint = Mockito.mock(DockerServerEndpoint.class);
    Mockito.when(dockerServerEndpoint.getUri()).thenReturn("tcp://mocked-docker-host:2375");
    Mockito.when(result.getDockerHost()).thenReturn(dockerServerEndpoint);
    ListContainersCmd listContainerCmd = Mockito.mock(ListContainersCmd.class);
    Mockito.when(client.listContainersCmd()).thenReturn(listContainerCmd);
    Mockito.when(listContainerCmd.withShowAll(true)).thenReturn(listContainerCmd);
    Mockito.when(listContainerCmd.withLabelFilter(Matchers.anyMap())).thenAnswer( new Answer<ListContainersCmd>() {
        @Override
        public ListContainersCmd answer(InvocationOnMock invocation) throws Throwable {
            Map<String, String> arg = invocation.getArgumentAt(0, Map.class);
            String jenkinsInstanceIdInFilter = arg.get(DockerContainerLabelKeys.JENKINS_INSTANCE_ID);
            Assert.assertEquals(UNITTEST_JENKINS_ID, jenkinsInstanceIdInFilter);
            return listContainerCmd;
        }
    });
    Mockito.when(listContainerCmd.exec()).thenReturn(containerList);
    return result;
}
 
Example #3
Source File: ContainerUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: ListContainersCmdExec.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@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 #5
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
public ListContainersCmd withLimit(Integer limit) {
    checkNotNull(limit, "limit was not specified");
    checkArgument(limit > 0, "limit must be greater 0");
    this.limit = limit;
    return this;
}
 
Example #6
Source File: ListContainersWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager workItemManager) {

    Map<String, Object> results = new HashMap<>();

    try {

        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        String statusFilter = (String) workItem.getParameter("StatusFilter");

        if (dockerClient == null) {
            DockerClientConnector connector = new DockerClientConnector();
            dockerClient = connector.getDockerClient();
        }

        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd()
                .withShowAll(true).withShowSize(true);

        if (statusFilter != null && statusFilter.trim().length() > 0) {
            listContainersCmd = listContainersCmd.withStatusFilter(statusFilter);
        }

        List<Container> containers = listContainersCmd.exec();

        results.put(RESULTS_DOCUMENT,
                    containers);

        workItemManager.completeWorkItem(workItem.getId(),
                                         results);
    } catch (Exception e) {
        logger.error("Unable to get list of containers: " + e.getMessage());
        handleException(e);
    }
}
 
Example #7
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withNameFilter(Collection<String> name) {
    return withFilter("name", name);
}
 
Example #8
Source File: DelegatingDockerClient.java    From docker-plugin with MIT License 4 votes vote down vote up
@Override
public ListContainersCmd listContainersCmd() {
    return getDelegate().listContainersCmd();
}
 
Example #9
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withStatusFilter(Collection<String> status) {
    checkNotNull(status, "status was not specified");
    this.filters.withFilter("status", status);
    return this;
}
 
Example #10
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withFilter(String filterName, Collection<String> filterValues) {
    checkNotNull(filterValues, filterName + " was not specified");
    this.filters.withFilter(filterName, filterValues);
    return this;
}
 
Example #11
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withExitedFilter(Integer exited) {
    checkNotNull(exited, "exited was not specified");
    this.filters.withFilter("exited", exited.toString());
    return this;
}
 
Example #12
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withLabelFilter(Map<String, String> labels) {
    checkNotNull(labels, "labels was not specified");
    this.filters.withLabels(labels);
    return this;
}
 
Example #13
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withLabelFilter(Collection<String> labels) {
    return withFilter("label", labels);
}
 
Example #14
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withNetworkFilter(Collection<String> network) {
    return withFilter("network", network);
}
 
Example #15
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withVolumeFilter(Collection<String> volume) {
    return withFilter("volume", volume);
}
 
Example #16
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withAncestorFilter(Collection<String> ancestor) {
    return withFilter("ancestor", ancestor);
}
 
Example #17
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withIdFilter(Collection<String> id) {
    return withFilter("id", id);
}
 
Example #18
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withBefore(String before) {
    checkNotNull(before, "before was not specified");
    this.beforeId = before;
    return this;
}
 
Example #19
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withSince(String since) {
    checkNotNull(since, "since was not specified");
    this.sinceId = since;
    return this;
}
 
Example #20
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withShowSize(Boolean showSize) {
    this.showSize = showSize;
    return this;
}
 
Example #21
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd withShowAll(Boolean showAll) {
    this.showAll = showAll;
    return this;
}
 
Example #22
Source File: ListContainersCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public ListContainersCmdImpl(ListContainersCmd.Exec exec) {
    super(exec);
}
 
Example #23
Source File: AbstractDockerCmdExecFactory.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public ListContainersCmd.Exec createListContainersCmdExec() {
    return new ListContainersCmdExec(getBaseResource(), getDockerClientConfig());
}
 
Example #24
Source File: DockerClientImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
/**
 * * CONTAINER API *
 */

@Override
public ListContainersCmd listContainersCmd() {
    return new ListContainersCmdImpl(getDockerCmdExecFactory().createListContainersCmdExec());
}
 
Example #25
Source File: DockerClient.java    From docker-java with Apache License 2.0 votes vote down vote up
/**
 * * CONTAINER API *
 */

ListContainersCmd listContainersCmd();