use of com.github.dockerjava.api.model.Container in project hub-docker-inspector by blackducksoftware.
the class DockerClientManager method ensureContainerRunning.
private String ensureContainerRunning(final DockerClient dockerClient, final String imageId, final String extractorContainerName, final String hubPassword, final String hubApiToken) {
String oldContainerId;
final List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
final Container extractorContainer = getRunningContainer(containers, extractorContainerName);
if (extractorContainer != null) {
logger.debug(String.format("Extractor container status: %s", extractorContainer.getStatus()));
oldContainerId = extractorContainer.getId();
if (extractorContainer.getStatus().startsWith("Up")) {
logger.debug("The extractor container is running; stopping it");
dockerClient.stopContainerCmd(oldContainerId).exec();
}
logger.debug("The extractor container exists; removing it");
dockerClient.removeContainerCmd(oldContainerId).exec();
}
logger.debug(String.format("Creating container %s from image %s", extractorContainerName, imageId));
final CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(imageId).withStdinOpen(true).withTty(true).withName(extractorContainerName).withCmd("/bin/bash");
final List<String> envAssignments = new ArrayList<>();
envAssignments.add(String.format("BD_HUB_PASSWORD=%s", hubPassword));
envAssignments.add(String.format("BD_HUB_TOKEN=%s", hubApiToken));
if ((StringUtils.isBlank(config.getHubProxyHost())) && (!StringUtils.isBlank(config.getScanCliOptsEnvVar()))) {
envAssignments.add(String.format("SCAN_CLI_OPTS=%s", config.getScanCliOptsEnvVar()));
} else {
}
createContainerCmd.withEnv(envAssignments);
final CreateContainerResponse containerResponse = createContainerCmd.exec();
final String containerId = containerResponse.getId();
dockerClient.startContainerCmd(containerId).exec();
logger.info(String.format("Started container %s from image %s", containerId, imageId));
return containerId;
}
Aggregations