Search in sources :

Example 11 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse in project tutorials by eugenp.

the class ContainerLiveTest method givenContainer_whenCommittingContainer_thenMustReturnImageId.

@Test
public void givenContainer_whenCommittingContainer_thenMustReturnImageId() {
    // given
    CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec();
    // when
    String imageId = dockerClient.commitCmd(container.getId()).withEnv("SNAPSHOT_YEAR=2018").withMessage("add git support").withCmd("sleep", "10000").withRepository("alpine").withTag("3.6.v2").exec();
    // then
    assertThat(imageId, is(not(null)));
}
Also used : CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) Test(org.junit.Test)

Example 12 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse 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

CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)12 Test (org.junit.Test)6 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)2 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)2 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)2 ArrayList (java.util.ArrayList)2 CreateContainerCmd (com.github.dockerjava.api.command.CreateContainerCmd)1 InspectContainerResponse (com.github.dockerjava.api.command.InspectContainerResponse)1 StartContainerCmd (com.github.dockerjava.api.command.StartContainerCmd)1 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)1 Bind (com.github.dockerjava.api.model.Bind)1 Container (com.github.dockerjava.api.model.Container)1 LogConfig (com.github.dockerjava.api.model.LogConfig)1 Volume (com.github.dockerjava.api.model.Volume)1 ContainerException (com.github.qzagarese.dockerunit.exception.ContainerException)1 TJob (io.elastest.etm.model.TJob)1 TJobExecution (io.elastest.etm.model.TJobExecution)1 WaitForMessagesHandler (io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1