Search in sources :

Example 6 with CreateContainerResponse

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

the class ContainerLiveTest method whenHavingContainer_thenRunContainer.

@Test
public void whenHavingContainer_thenRunContainer() throws InterruptedException {
    // when
    CreateContainerResponse container = dockerClient.createContainerCmd("alpine:3.6").withCmd("sleep", "10000").exec();
    Thread.sleep(3000);
    // then
    dockerClient.startContainerCmd(container.getId()).exec();
    dockerClient.stopContainerCmd(container.getId()).exec();
}
Also used : CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) Test(org.junit.Test)

Example 7 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse in project elastest-torm by elastest.

the class DockerService2 method createTestContainer.

public void createTestContainer(DockerExecution dockerExec) throws TJobStoppedException {
    try {
        CreateContainerResponse testContainer = createContainer(dockerExec, "tjob");
        String testContainerId = testContainer.getId();
        dockerExec.setTestcontainer(testContainer);
        dockerExec.setTestContainerId(testContainerId);
    } catch (DockerClientException dce) {
        throw new TJobStoppedException();
    } catch (TJobStoppedException dce) {
        throw new TJobStoppedException();
    }
}
Also used : DockerClientException(com.github.dockerjava.api.exception.DockerClientException) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse)

Example 8 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse in project elastest-torm by elastest.

the class DockerService2 method startDockbeat.

public void startDockbeat(DockerExecution dockerExec) throws TJobStoppedException {
    TJobExecution tJobExec = dockerExec.gettJobexec();
    TJob tJob = tJobExec.getTjob();
    Long execution = dockerExec.getExecutionId();
    String containerName = getDockbeatContainerName(dockerExec);
    // Environment variables
    ArrayList<String> envList = new ArrayList<>();
    String envVar;
    // Get Parameters and insert into Env VarsĀ”
    String lsHostEnvVar = "LOGSTASHHOST" + "=" + logstashHost;
    if (tJob.isSelectedService("ems")) {
        envVar = "FILTER_CONTAINERS" + "=" + "\"^sut\\d*_.*_" + execution + "|^test\\d*_.*_" + execution + "\"";
        envList.add(envVar);
        // envVar = "FILTER_EXCLUDE" + "=" + "\"\"";
        // envList.add(envVar);
        envVar = "LOGSTASHPORT" + "=" + tJobExec.getEnvVars().get("ET_EMS_LSBEATS_PORT");
        envList.add(envVar);
        lsHostEnvVar = "LOGSTASHHOST" + "=" + tJobExec.getEnvVars().get("ET_EMS_LSBEATS_HOST");
    }
    envList.add(lsHostEnvVar);
    // dockerSock
    Volume volume1 = new Volume(dockerSock);
    // Pull Image
    this.pullETExecImage(dockbeatImage, "Dockbeat");
    // Create Container
    logger.debug("Creating Dockbeat Container...");
    CreateContainerResponse container = dockerExec.getDockerClient().createContainerCmd(dockbeatImage).withEnv(envList).withName(containerName).withBinds(new Bind(dockerSock, volume1)).withNetworkMode(dockerExec.getNetwork()).exec();
    dockerExec.getDockerClient().startContainerCmd(container.getId()).exec();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    this.insertCreatedContainer(container.getId(), containerName);
}
Also used : Bind(com.github.dockerjava.api.model.Bind) Volume(com.github.dockerjava.api.model.Volume) TJobExecution(io.elastest.etm.model.TJobExecution) ArrayList(java.util.ArrayList) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) TJob(io.elastest.etm.model.TJob)

Example 9 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse in project elastest-torm by elastest.

the class DockerServiceItTest method readLogInRabbit.

@Test
public void readLogInRabbit() throws Exception {
    String imageId = "alpine";
    if (!existsImage(imageId)) {
        log.info("Pulling image '{}'", imageId);
        dockerClient.pullImageCmd(imageId).exec(new PullImageResultCallback()).awaitSuccess();
    }
    String queueId = "test.default_log.1.log";
    String tag = "test_1_exec";
    WaitForMessagesHandler handler = connectToRabbitQueue(queueId);
    LogConfig logConfig = getLogConfig(tag);
    log.info("Creating container");
    CreateContainerResponse container = dockerClient.createContainerCmd(imageId).withCmd("/bin/sh", "-c", "while true; do echo hello; sleep 1; done").withTty(false).withLogConfig(logConfig).withNetworkMode("bridge").exec();
    String containerId = container.getId();
    try {
        log.info("Created container: {}", container.toString());
        long start = System.currentTimeMillis();
        dockerClient.startContainerCmd(containerId).exec();
        log.info("Waiting for logs messages in Rabbit");
        handler.waitForCompletion(5, TimeUnit.SECONDS);
        long duration = System.currentTimeMillis() - start;
        log.info("Log received in Rabbit in {} millis", duration);
    } catch (Exception ex) {
        log.info("Log NOT received in Rabbit");
        throw ex;
    } finally {
        log.info("Cleaning up resources");
        try {
            log.info("Removing container " + containerId);
            try {
                dockerClient.stopContainerCmd(containerId).exec();
            } catch (Exception ex) {
                log.warn("Error stopping container {}", containerId, ex);
            }
            dockerClient.removeContainerCmd(containerId).exec();
        } catch (Exception ex) {
            log.warn("Error on ending test execution {}", containerId, ex);
        }
    }
}
Also used : PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse) WaitForMessagesHandler(io.elastest.etm.test.util.StompTestUtils.WaitForMessagesHandler) TimeoutException(java.util.concurrent.TimeoutException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LogConfig(com.github.dockerjava.api.model.LogConfig) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 10 with CreateContainerResponse

use of com.github.dockerjava.api.command.CreateContainerResponse in project elastest-torm by elastest.

the class DockerService2 method runDockerContainer.

/**
 **************************
 */
/**
 *** Container Methods ****
 */
/**
 **************************
 */
public String runDockerContainer(DockerClient dockerClient, String imageName, List<String> envs, String containerName, String networkName, Ports portBindings, int listenPort) throws TJobStoppedException {
    try {
        dockerClient.pullImageCmd(imageName).exec(new PullImageResultCallback()).awaitSuccess();
    } catch (InternalServerErrorException | NotFoundException ie) {
        if (imageExistsLocally(imageName, dockerClient)) {
            logger.info("Docker image exits locally.");
        } else {
            throw ie;
        }
    } catch (DockerClientException e) {
        logger.info("Error on Pulling " + imageName + " image. Probably because the user has stopped the execution");
        throw new TJobStoppedException();
    }
    CreateContainerResponse container = dockerClient.createContainerCmd(imageName).withName(containerName).withEnv(envs).withNetworkMode(networkName).withExposedPorts(ExposedPort.tcp(listenPort)).withPortBindings(portBindings).withPublishAllPorts(true).exec();
    dockerClient.startContainerCmd(container.getId()).exec();
    this.insertCreatedContainer(container.getId(), containerName);
    logger.info("Id del contenedor:" + container.getId());
    return container.getId();
}
Also used : DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) 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