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();
}
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();
}
}
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);
}
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);
}
}
}
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();
}
Aggregations