Search in sources :

Example 6 with Container

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;
}
Also used : CreateContainerCmd(com.github.dockerjava.api.command.CreateContainerCmd) Container(com.github.dockerjava.api.model.Container) ArrayList(java.util.ArrayList) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse)

Aggregations

Container (com.github.dockerjava.api.model.Container)6 DockerContainer (io.elastest.epm.client.json.DockerContainerInfo.DockerContainer)2 SutSpecification (io.elastest.etm.model.SutSpecification)2 DockerClient (com.github.dockerjava.api.DockerClient)1 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)1 Image (com.github.dockerjava.api.model.Image)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1