use of com.github.dockerjava.api.command.ListContainersCmd in project camel by apache.
the class DockerProducer method executeListContainersRequest.
/**
* Produces a list containers request
*
* @param client
* @param message
* @return
*/
private ListContainersCmd executeListContainersRequest(DockerClient client, Message message) {
LOGGER.debug("Executing Docker List Container Request");
ListContainersCmd listContainersCmd = client.listContainersCmd();
String before = DockerHelper.getProperty(DockerConstants.DOCKER_BEFORE, configuration, message, String.class);
if (before != null) {
listContainersCmd.withBefore(before);
}
Integer limit = DockerHelper.getProperty(DockerConstants.DOCKER_LIMIT, configuration, message, Integer.class);
if (limit != null) {
listContainersCmd.withLimit(limit);
}
Boolean showAll = DockerHelper.getProperty(DockerConstants.DOCKER_SHOW_ALL, configuration, message, Boolean.class);
if (showAll != null) {
listContainersCmd.withShowAll(showAll);
}
Boolean showSize = DockerHelper.getProperty(DockerConstants.DOCKER_SHOW_SIZE, configuration, message, Boolean.class);
if (showSize != null) {
listContainersCmd.withShowSize(showSize);
}
String since = DockerHelper.getProperty(DockerConstants.DOCKER_SINCE, configuration, message, String.class);
if (since != null) {
listContainersCmd.withSince(since);
}
return listContainersCmd;
}
use of com.github.dockerjava.api.command.ListContainersCmd in project engineblock by engineblock.
the class DockerMetricsHelper method searchContainer.
private Container searchContainer(String name, String reload) {
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));
logger.info(String.format("Hupping config"));
if (reload != null) {
post(reload, null, false);
}
return runningContainers.get(0);
}
return null;
}
use of com.github.dockerjava.api.command.ListContainersCmd in project engineblock by engineblock.
the class DockerMetricsHelper method startDocker.
private String startDocker(String IMG, String tag, String name, List<Integer> ports, List<String> volumeDescList, List<String> envList, List<String> cmdList, String reload) {
ListContainersCmd listContainersCmd = dockerClient.listContainersCmd().withStatusFilter(List.of("exited"));
listContainersCmd.getFilters().put("name", Arrays.asList(name));
List<Container> stoppedContainers = null;
try {
stoppedContainers = listContainersCmd.exec();
for (Container stoppedContainer : stoppedContainers) {
String id = stoppedContainer.getId();
logger.info("Removing exited container: " + id);
dockerClient.removeContainerCmd(id).exec();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Unable to contact docker, make sure docker is up and try again.");
logger.error("If docker is installed make sure this user has access to the docker group.");
logger.error("$ sudo gpasswd -a ${USER} docker && newgrp docker");
System.exit(1);
}
Container containerId = searchContainer(name, reload);
if (containerId != null) {
return containerId.getId();
}
Info info = dockerClient.infoCmd().exec();
dockerClient.buildImageCmd();
String term = IMG.split("/")[1];
// List<SearchItem> dockerSearch = dockerClient.searchImagesCmd(term).exec();
List<Image> dockerList = dockerClient.listImagesCmd().withImageNameFilter(IMG).exec();
if (dockerList.size() == 0) {
dockerClient.pullImageCmd(IMG).withTag(tag).exec(new PullImageResultCallback()).awaitSuccess();
dockerList = dockerClient.listImagesCmd().withImageNameFilter(IMG).exec();
if (dockerList.size() == 0) {
logger.error(String.format("Image %s not found, unable to automatically pull image." + " Check `docker images`", IMG));
System.exit(1);
}
}
logger.info("Search returned" + dockerList.toString());
List<ExposedPort> tcpPorts = new ArrayList<>();
List<PortBinding> portBindings = new ArrayList<>();
for (Integer port : ports) {
ExposedPort tcpPort = ExposedPort.tcp(port);
Ports.Binding binding = new Ports.Binding("0.0.0.0", String.valueOf(port));
PortBinding pb = new PortBinding(binding, tcpPort);
tcpPorts.add(tcpPort);
portBindings.add(pb);
}
List<Volume> volumeList = new ArrayList<>();
List<Bind> volumeBindList = new ArrayList<>();
for (String volumeDesc : volumeDescList) {
String volFrom = volumeDesc.split(":")[0];
String volTo = volumeDesc.split(":")[1];
Volume vol = new Volume(volTo);
volumeList.add(vol);
volumeBindList.add(new Bind(volFrom, vol));
}
CreateContainerResponse containerResponse;
if (envList == null) {
containerResponse = dockerClient.createContainerCmd(IMG + ":" + tag).withCmd(cmdList).withExposedPorts(tcpPorts).withHostConfig(new HostConfig().withPortBindings(portBindings).withPublishAllPorts(true).withBinds(volumeBindList)).withName(name).exec();
} else {
long user = new UnixSystem().getUid();
containerResponse = dockerClient.createContainerCmd(IMG + ":" + tag).withEnv(envList).withExposedPorts(tcpPorts).withHostConfig(new HostConfig().withPortBindings(portBindings).withPublishAllPorts(true).withBinds(volumeBindList)).withName(name).withUser("" + user).exec();
}
dockerClient.startContainerCmd(containerResponse.getId()).exec();
return containerResponse.getId();
}
Aggregations