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

The following examples show how to use com.spotify.docker.client.messages.HostConfig#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: SamlIdpITConfigFactory.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private static Builder keycloakConfigBuilder(final String image,
                                             final String userName,
                                             final String password,
                                             final List<String> portMappingPorts,
                                             final DockerCommandLineConfig config)
{
  HostConfig.Builder hostConfig = HostConfig.builder()
      .portBindings(portMappingPorts.stream()
          .collect(toMap(o -> o, b -> singletonList(randomPort(PORT_MAPPING_IP)))));

  return DockerContainerConfig.builder()
      .image(image)
      .env("KEYCLOAK_USER=" + userName, "KEYCLOAK_PASSWORD=" + password)
      .withHostConfigBuilder(hostConfig.appendBinds(config.getPathBinds()))
      .withDockerClientBuilder(defaultDockerClientBuilder().readTimeoutMillis(SECONDS.toMillis(5000)));
}
 
Example 2
Source File: SyslogRedirectingContainerDecorator.java    From helios with Apache License 2.0 6 votes vote down vote up
@Override
public void decorateHostConfig(Job job, Optional<String> dockerVersion,
                               HostConfig.Builder hostConfigBuilder) {
  final HostConfig hostConfig = hostConfigBuilder.build();
  if (useSyslogRedirector(dockerVersion)) {
    final List<String> binds = Lists.newArrayList();
    if (hostConfig.binds() != null) {
      binds.addAll(hostConfig.binds());
    }
    binds.add("/usr/lib/helios:/helios:ro");
    hostConfigBuilder.binds(binds);
  } else {
    final ImmutableMap.Builder<String, String> logOpts = ImmutableMap.builder();

    logOpts.put("syslog-address", "udp://" + syslogHostPort);
    logOpts.put("syslog-facility", "local0"); // match the behavior of syslog-redirector

    logOpts.put("tag", job.getId().toString());

    hostConfigBuilder.logConfig(LogConfig.create("syslog", logOpts.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: DockerClientITConfigFactory.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static DockerContainerConfig.Builder nginxConfigBuilder(final String image,
                                                                final List<String> portMappingPorts,
                                                                final DockerCommandLineConfig config)
{
  HostConfig.Builder hostConfig = HostConfig.builder()
      .portBindings(portMappingPorts.stream()
          .collect(toMap(o -> o, b -> singletonList(randomPort(PORT_MAPPING_IP)))));

  return DockerContainerConfig.builder()
      .image(image)
      .withHostConfigBuilder(hostConfig.appendBinds(config.getPathBinds()))
      .withDockerClientBuilder(defaultDockerClientBuilder().readTimeoutMillis(SECONDS.toMillis(5000)));
}
 
Example 6
Source File: TaskConfig.java    From helios with Apache License 2.0 5 votes vote down vote up
/**
 * Create a container host configuration for the job.
 *
 * @return The host configuration.
 */
public HostConfig hostConfig(final Optional<String> dockerVersion) {
  final List<String> securityOpt = job.getSecurityOpt();
  final HostConfig.Builder builder = HostConfig.builder()
      .binds(binds())
      .portBindings(portBindings())
      .dns(dns)
      .securityOpt(securityOpt.toArray(new String[securityOpt.size()]))
      .runtime(job.getRuntime())
      .networkMode(job.getNetworkMode());

  if (!job.getRamdisks().isEmpty()) {
    builder.tmpfs(job.getRamdisks());
  }

  final Resources resources = job.getResources();
  if (resources != null) {
    builder.memory(resources.getMemory());
    builder.memorySwap(resources.getMemorySwap());
    builder.cpusetCpus(resources.getCpuset());
    builder.cpuShares(resources.getCpuShares());
  }

  builder.capAdd(ImmutableList.copyOf(job.getAddCapabilities()));
  builder.capDrop(ImmutableList.copyOf(job.getDropCapabilities()));

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

  return builder.build();
}
 
Example 7
Source File: BindVolumeContainerDecorator.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public void decorateHostConfig(Job job, Optional<String> dockerVersion,
                               HostConfig.Builder hostConfigBuilder) {
  final List<String> b = Lists.newArrayList();

  final HostConfig hostConfig = hostConfigBuilder.build();

  if (hostConfig.binds() != null) {
    b.addAll(hostConfig.binds());
  }

  b.addAll(this.binds);

  hostConfigBuilder.binds(b);
}
 
Example 8
Source File: AddExtraHostContainerDecoratorTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleTest() {
  final List<String> hosts = ImmutableList.of("one:one", "two:two");
  final AddExtraHostContainerDecorator decorator = new AddExtraHostContainerDecorator(hosts);

  final HostConfig.Builder hostBuilder = HostConfig.builder();
  decorator.decorateHostConfig(null, Optional.absent(), hostBuilder);

  final HostConfig config = hostBuilder.build();
  assertThat(config.extraHosts(), equalTo(hosts));
}
 
Example 9
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 10
Source File: DockerContainerConfig.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public Builder withHostConfigBuilder(HostConfig.Builder hostConfigBuilder) {
  this.hostConfigBuilder = hostConfigBuilder;
  return this;
}
 
Example 11
Source File: DockerContainerConfig.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static HostConfig.Builder defaultHostConfigBuilder() {
  return HostConfig.builder()
      .portBindings(stream(PORT_MAPPING_PORTS)
          .collect(toMap(o -> o, b -> singletonList(randomPort(PORT_MAPPING_IP)))));
}
 
Example 12
Source File: DockerContainerConfig.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public HostConfig.Builder getHostConfigBuilder() {
  return hostConfigBuilder;
}
 
Example 13
Source File: AddExtraHostContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateHostConfig(final Job job, final Optional<String> dockerVersion,
                               final HostConfig.Builder hostConfig) {
  hostConfig.extraHosts(this.extraHosts);
}
 
Example 14
Source File: NoOpContainerDecorator.java    From helios with Apache License 2.0 4 votes vote down vote up
@Override
public void decorateHostConfig(Job job, Optional<String> dockerVersion,
                               HostConfig.Builder hostConfig) {
  //noop
}
 
Example 15
Source File: ContainerDecorator.java    From helios with Apache License 2.0 votes vote down vote up
void decorateHostConfig(Job job, Optional<String> dockerVersion, HostConfig.Builder hostConfig);