Java Code Examples for com.spotify.docker.client.messages.ContainerConfig#Builder

The following examples show how to use com.spotify.docker.client.messages.ContainerConfig#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockerContainerManager.java    From simpleci with MIT License 6 votes vote down vote up
public String runContainer(ContainerConfig.Builder containerConfigBuilder, String containerName) throws DockerException {
    try {
        final ContainerConfig containerConfig = containerConfigBuilder.build();
        final ContainerCreation creation;

        creation = dockerClient.createContainer(containerConfig, containerName);

        final String id = creation.id();
        dockerClient.startContainer(id);
        return id;
    } catch (InterruptedException e) {
        logger.error("", e);
    }

    return null;
}
 
Example 2
Source File: TaskConfig.java    From helios with Apache License 2.0 6 votes vote down vote up
/**
 * Create docker container configuration for a job.
 *
 * @param imageInfo The ImageInfo object.
 *
 * @return The ContainerConfig object.
 */
public ContainerConfig containerConfig(final ImageInfo imageInfo,
                                       final Optional<String> dockerVersion) {
  final ContainerConfig.Builder builder = ContainerConfig.builder();

  builder.image(job.getImage());
  builder.cmd(job.getCommand());
  builder.hostname(job.getHostname());
  builder.env(containerEnvStrings());
  builder.exposedPorts(containerExposedPorts());
  builder.volumes(volumes().keySet());
  builder.labels(job.getLabels());

  for (final ContainerDecorator decorator : containerDecorators) {
    decorator.decorateContainerConfig(job, imageInfo, dockerVersion, builder);
  }

  return builder.build();
}
 
Example 3
Source File: SyslogRedirectingContainerDecoratorTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDockerVersionPre1_9() {
  final Optional<String> dockerVersion = Optional.of("1.6.0");

  final SyslogRedirectingContainerDecorator decorator =
      new SyslogRedirectingContainerDecorator(SYSLOG_HOST_PORT);

  final HostConfig.Builder hostBuilder = HostConfig.builder();
  decorator.decorateHostConfig(JOB, dockerVersion, hostBuilder);

  final ContainerConfig.Builder containerBuilder = ContainerConfig.builder();
  decorator.decorateContainerConfig(JOB, imageInfo, dockerVersion, containerBuilder);

  final ContainerConfig containerConfig = containerBuilder.build();
  assertThat(containerConfig.entrypoint(), hasItem("/helios/syslog-redirector"));

  final HostConfig hostConfig = hostBuilder.build();
  assertNull(hostConfig.logConfig());
  assertFalse(hostConfig.binds().isEmpty());
}
 
Example 4
Source File: SyslogRedirectingContainerDecoratorTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDockerVersionPost1_9() {
  final Optional<String> dockerVersion = Optional.of("1.12.1");

  final SyslogRedirectingContainerDecorator decorator =
      new SyslogRedirectingContainerDecorator(SYSLOG_HOST_PORT);

  final HostConfig.Builder hostBuilder = HostConfig.builder();
  decorator.decorateHostConfig(JOB, dockerVersion, hostBuilder);

  final ContainerConfig.Builder containerBuilder = ContainerConfig.builder();
  decorator.decorateContainerConfig(JOB, imageInfo, dockerVersion, containerBuilder);

  final ContainerConfig containerConfig = containerBuilder.build();
  assertThat(containerConfig.entrypoint(), not(hasItem("/helios/syslog-redirector")));

  final HostConfig hostConfig = hostBuilder.build();
  final LogConfig logConfig = hostConfig.logConfig();
  assertEquals("syslog", logConfig.logType());
  assertEquals(JOB.getId().toString(), logConfig.logOptions().get("tag"));
  assertEquals("udp://" + SYSLOG_HOST_PORT, logConfig.logOptions().get("syslog-address"));
}
 
Example 5
Source File: SyslogRedirectingContainerDecorator.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfigBuilder) {
  if (!useSyslogRedirector(dockerVersion)) {
    return;
  }

  final ContainerConfig imageConfig = imageInfo.config();

  // Inject syslog-redirector in the entrypoint to capture std out/err
  final String syslogRedirectorPath = Optional.fromNullable(job.getEnv().get("SYSLOG_REDIRECTOR"))
      .or("/helios/syslog-redirector");

  final List<String> entrypoint = Lists.newArrayList(syslogRedirectorPath,
      "-h", syslogHostPort,
      "-n", job.getId().toString(),
      "--");
  if (imageConfig.entrypoint() != null) {
    entrypoint.addAll(imageConfig.entrypoint());
  }
  containerConfigBuilder.entrypoint(entrypoint);

  final ContainerConfig containerConfig = containerConfigBuilder.build();

  // If there's no explicit container cmd specified, copy over the one from the image.
  // Only setting the entrypoint causes dockerd to not use the image cmd.
  if ((containerConfig.cmd() == null || containerConfig.cmd().isEmpty())
      && imageConfig.cmd() != null) {
    containerConfigBuilder.cmd(imageConfig.cmd());
  }

  final ImmutableSet.Builder<String> volumesBuilder = ImmutableSet.builder();
  if (containerConfig.volumes() != null) {
    volumesBuilder.addAll(containerConfig.volumes());
  }
  volumesBuilder.add("/helios");
  containerConfigBuilder.volumes(volumesBuilder.build());
}
 
Example 6
Source File: DockerContainer.java    From docker-elastic-agents-plugin with Apache License 2.0 4 votes vote down vote up
public static DockerContainer create(CreateAgentRequest request, PluginSettings settings, DockerClient docker,
                                     ConsoleLogAppender consoleLogAppender) throws InterruptedException, DockerException {
    String containerName = UUID.randomUUID().toString();

    HashMap<String, String> labels = labelsFrom(request);
    String imageName = image(request.properties());
    List<String> env = environmentFrom(request, settings, containerName);

    try {
        docker.inspectImage(imageName);
        if (settings.pullOnContainerCreate()) {
            consoleLogAppender.accept("Pulling a fresh version of " + imageName + ".");
            LOG.info("Pulling a fresh version of " + imageName + ".");
            docker.pull(imageName);
        }
    } catch (ImageNotFoundException ex) {
        consoleLogAppender.accept("Image " + imageName + " not found, attempting to download.");
        LOG.info("Image " + imageName + " not found, attempting to download.");
        docker.pull(imageName);
    }

    ContainerConfig.Builder containerConfigBuilder = ContainerConfig.builder();
    if (StringUtils.isNotBlank(request.properties().get("Command"))) {
        containerConfigBuilder.cmd(splitIntoLinesAndTrimSpaces(request.properties().get("Command")).toArray(new String[]{}));
    }

    final String hostConfig = request.properties().get("Hosts");
    final String reservedMemory = request.properties().get("ReservedMemory");
    final String maxMemory = request.properties().get("MaxMemory");
    final String cpus = request.properties().get("Cpus");
    final String volumeMounts = request.properties().get("Mounts");

    HostConfig.Builder hostBuilder = HostConfig.builder()
            .privileged(privileged(request.properties()))
            .extraHosts(new Hosts(hostConfig))
            .memoryReservation(new MemorySpecification(reservedMemory).getMemory())
            .memory(new MemorySpecification(maxMemory).getMemory());

    CpusSpecification cpusValue = new CpusSpecification(cpus);
    if (cpusValue.getCpus() != null) {
        hostBuilder
                .cpuPeriod(cpusValue.getCpuPeriod())
                .cpuQuota(cpusValue.getCpuQuota());
    }
    if (volumeMounts != null) {
        hostBuilder.appendBinds(Util.splitIntoLinesAndTrimSpaces(volumeMounts));
    }

    ContainerConfig containerConfig = containerConfigBuilder
            .image(imageName)
            .labels(labels)
            .env(env)
            .hostConfig(hostBuilder.build())
            .build();

    consoleLogAppender.accept(String.format("Creating container: %s", containerName));
    ContainerCreation container = docker.createContainer(containerConfig, containerName);
    String id = container.id();

    ContainerInfo containerInfo = docker.inspectContainer(id);

    LOG.debug("Created container " + containerName);
    consoleLogAppender.accept(String.format("Starting container: %s", containerName));
    docker.startContainer(containerName);
    consoleLogAppender.accept(String.format("Started container: %s", containerName));
    LOG.debug("container " + containerName + " started");
    return new DockerContainer(id, containerName, request.jobIdentifier(), containerInfo.created(), request.properties(), request.environment());
}
 
Example 7
Source File: AddExtraHostContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(final Job job, final ImageInfo imageInfo,
                                    final Optional<String> dockerVersion,
                                    final ContainerConfig.Builder containerConfig) {
  //do nothing
}
 
Example 8
Source File: BindVolumeContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfig) {
}
 
Example 9
Source File: NoOpContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
                                    ContainerConfig.Builder containerConfig) {
  //noop
}
 
Example 10
Source File: ContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
void decorateContainerConfig(Job job, ImageInfo imageInfo, Optional<String> dockerVersion,
ContainerConfig.Builder containerConfig);