use of com.github.dockerjava.api.command.CreateContainerCmd in project vespa by vespa-engine.
the class CreateContainerCommandImpl method createCreateContainerCmd.
private CreateContainerCmd createCreateContainerCmd() {
List<Bind> volumeBinds = volumeBindSpecs.stream().map(Bind::parse).collect(Collectors.toList());
final CreateContainerCmd containerCmd = docker.createContainerCmd(dockerImage.asString()).withCpuShares(containerResources.cpuShares).withMemory(containerResources.memoryBytes).withName(containerName.asString()).withHostName(hostName).withLabels(labels).withEnv(environmentAssignments).withBinds(volumeBinds).withUlimits(ulimits).withCapAdd(new ArrayList<>(addCapabilities)).withCapDrop(new ArrayList<>(dropCapabilities)).withPrivileged(privileged);
networkMode.filter(mode -> !mode.toLowerCase().equals("host")).ifPresent(mode -> containerCmd.withMacAddress(generateMACAddress(hostName, ipv4Address, ipv6Address)));
networkMode.ifPresent(containerCmd::withNetworkMode);
ipv4Address.ifPresent(containerCmd::withIpv4Address);
ipv6Address.ifPresent(containerCmd::withIpv6Address);
entrypoint.ifPresent(containerCmd::withEntrypoint);
return containerCmd;
}
use of com.github.dockerjava.api.command.CreateContainerCmd in project hazelcast by hazelcast.
the class PostgresCdcNetworkIntegrationTest method initPostgres.
private PostgreSQLContainer<?> initPostgres(Network network, Integer fixedExposedPort) {
PostgreSQLContainer<?> postgres = namedTestContainer(new PostgreSQLContainer<>(AbstractPostgresCdcIntegrationTest.DOCKER_IMAGE).withDatabaseName("postgres").withUsername("postgres").withPassword("postgres"));
if (fixedExposedPort != null) {
Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings(new PortBinding(Ports.Binding.bindPort(fixedExposedPort), new ExposedPort(POSTGRESQL_PORT)));
postgres = postgres.withCreateContainerCmdModifier(cmd);
}
if (network != null) {
postgres = postgres.withNetwork(network);
}
postgres.start();
return postgres;
}
use of com.github.dockerjava.api.command.CreateContainerCmd in project dockerunit by qzagarese.
the class DefaultServiceBuilder method createInstance.
private ServiceInstance createInstance(ServiceDescriptor descriptor, DockerClient client, int i) {
String containerId = null;
Status status = null;
String statusDetails = null;
CreateContainerCmd cmd = null;
try {
cmd = client.createContainerCmd(descriptor.getImage().value());
cmd = computeContainerName(descriptor, i, cmd);
cmd = executeOptionBuilders(descriptor, cmd);
if (descriptor.getCustomisationHook() != null) {
cmd = executeCustomisationHook(descriptor.getCustomisationHook(), descriptor.getInstance(), cmd);
}
containerId = createAndStartContainer(cmd, descriptor.getImage().pull(), client);
status = Status.STARTED;
statusDetails = "Started.";
} catch (Throwable t) {
if (t instanceof CompletionException) {
if (t.getCause() != null && t.getCause() instanceof ContainerException) {
containerId = ((ContainerException) t.getCause()).getContainerId();
statusDetails = t.getCause().getCause() != null ? t.getCause().getCause().getMessage() : null;
} else {
statusDetails = t.getCause() != null ? t.getCause().getMessage() : null;
}
} else {
statusDetails = t.getMessage();
}
status = Status.ABORTED;
}
return ServiceInstance.builder().containerName(cmd.getName()).containerId(containerId).status(status).statusDetails(statusDetails).build();
}
use of com.github.dockerjava.api.command.CreateContainerCmd in project dockerunit by qzagarese.
the class DefaultServiceBuilder method executeOptionBuilders.
private CreateContainerCmd executeOptionBuilders(ServiceDescriptor descriptor, CreateContainerCmd cmd) {
for (Annotation a : descriptor.getOptions()) {
Class<? extends ExtensionInterpreter<?>> builderType = a.annotationType().getAnnotation(ExtensionMarker.class).value();
ExtensionInterpreter<?> builder = null;
Method buildMethod = null;
try {
builder = builderType.newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot instantiate " + ExtensionInterpreter.class.getSimpleName() + " of type " + builderType.getSimpleName() + " to handle annotation " + a.annotationType().getSimpleName() + " that has been detected on class " + descriptor.getInstance().getClass().getName(), e);
}
try {
buildMethod = builderType.getDeclaredMethod("build", new Class<?>[] { ServiceDescriptor.class, CreateContainerCmd.class, a.annotationType() });
cmd = (CreateContainerCmd) buildMethod.invoke(builder, descriptor, cmd, a);
} catch (Exception e) {
throw new RuntimeException("An error occurred while invoking the build method on builder class " + builderType.getName(), e);
}
}
return cmd;
}
use of com.github.dockerjava.api.command.CreateContainerCmd 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