com.github.dockerjava.api.model.HostConfig Java Examples

The following examples show how to use com.github.dockerjava.api.model.HostConfig. 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: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("Duplicates")
@Test
public void createContainerWithShmPidsLimit() throws DockerException {
    assumeThat("API version should be >= 1.23", dockerRule, isGreaterOrEqual(VERSION_1_23));

    HostConfig hostConfig = new HostConfig().withPidsLimit(2L);
    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(hostConfig).withCmd("true").exec();

    LOG.info("Created container {}", container.toString());

    assertThat(container.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();

    assertThat(inspectContainerResponse.getHostConfig().getPidsLimit(), is(hostConfig.getPidsLimit()));
}
 
Example #2
Source File: DockerTemplateBaseTest.java    From docker-plugin with MIT License 6 votes vote down vote up
private static void testFillContainerSecurityOpts(final String imageName, final List<String> securityOptsToSet,
        final boolean securityOptsIsExpectedToBeSet, final List<String> expectedSecurityOpts) {
    // Given
    final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class);
    final HostConfig mockHostConfig = mock(HostConfig.class);
    when(mockCmd.getHostConfig()).thenReturn(mockHostConfig);
    final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName);
    instanceUnderTest.setSecurityOpts(securityOptsToSet);

    // When
    instanceUnderTest.fillContainerConfig(mockCmd);

    // Then
    if (securityOptsIsExpectedToBeSet) {
        verify(mockHostConfig, times(1)).withSecurityOpts(expectedSecurityOpts);
    } else {
        verify(mockHostConfig, never()).withSecurityOpts(anyList());
    }
}
 
Example #3
Source File: DockerTemplateBaseTest.java    From docker-plugin with MIT License 6 votes vote down vote up
private static void testFillContainerGroupAdd(
        String imageName,
        String extraGroupsStringToSet,
        boolean groupAddIsExpectedToBeSet,
        String... expectedGroupsSet) {
    // Given
    final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class);
    final HostConfig mockHostConfig = mock(HostConfig.class);
    when(mockCmd.getHostConfig()).thenReturn(mockHostConfig);
    final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName);
    instanceUnderTest.setExtraGroupsString(extraGroupsStringToSet);

    // When
    instanceUnderTest.fillContainerConfig(mockCmd);

    // Then
    if (groupAddIsExpectedToBeSet) {
        verify(mockHostConfig, times(1)).withGroupAdd(Arrays.asList(expectedGroupsSet));
    } else {
        verify(mockHostConfig, never()).withGroupAdd(anyListOf(String.class));
    }
}
 
Example #4
Source File: DockerTemplateBaseTest.java    From docker-plugin with MIT License 6 votes vote down vote up
private static void testFillContainerShmSize(final String imageName, final Integer shmSizeToSet,
        final boolean shmSizeIsExpectedToBeSet, final Long expectedShmSizeSet) {
    // Given
    final CreateContainerCmd mockCmd = mock(CreateContainerCmd.class);
    final HostConfig mockHostConfig = mock(HostConfig.class);
    when(mockCmd.getHostConfig()).thenReturn(mockHostConfig);
    final DockerTemplateBase instanceUnderTest = new DockerTemplateBase(imageName);
    instanceUnderTest.setShmSize(shmSizeToSet);

    // When
    instanceUnderTest.fillContainerConfig(mockCmd);

    // Then
    if (shmSizeIsExpectedToBeSet) {
        verify(mockHostConfig, times(1)).withShmSize(expectedShmSizeSet);
    } else {
        verify(mockHostConfig, never()).withShmSize(anyLong());
    }
}
 
Example #5
Source File: CreateContainerCommandImpl.java    From vespa with Apache License 2.0 5 votes vote down vote up
private CreateContainerCmd createCreateContainerCmd() {
    List<Bind> volumeBinds = volumeBindSpecs.stream().map(Bind::parse).collect(Collectors.toList());

    final HostConfig hostConfig = new HostConfig()
            .withSecurityOpts(new ArrayList<>(securityOpts))
            .withBinds(volumeBinds)
            .withUlimits(ulimits)
            // Docker version 1.13.1 patch 94 changed default pids.max for the Docker container's cgroup
            // from max to 4096. -1L reinstates "max". File: /sys/fs/cgroup/pids/docker/CONTAINERID/pids.max.
            .withPidsLimit(-1L)
            .withCapAdd(addCapabilities.toArray(new Capability[0]))
            .withCapDrop(dropCapabilities.toArray(new Capability[0]))
            .withDnsOptions(dnsOptions)
            .withPrivileged(privileged);

    containerResources.ifPresent(cr -> hostConfig
            .withCpuShares(cr.cpuShares())
            .withMemory(cr.memoryBytes())
            // MemorySwap is the total amount of memory and swap, if MemorySwap == Memory, then container has no access swap
            .withMemorySwap(cr.memoryBytes())
            .withCpuPeriod(cr.cpuQuota() > 0 ? (long) cr.cpuPeriod() : null)
            .withCpuQuota(cr.cpuQuota() > 0 ? (long) cr.cpuQuota() : null));

    final CreateContainerCmd containerCmd = docker
            .createContainerCmd(dockerImage.asString())
            .withHostConfig(hostConfig)
            .withName(containerName.asString())
            .withLabels(labels)
            .withEnv(environmentAssignments);

    hostName.ifPresent(containerCmd::withHostName);
    networkMode.ifPresent(hostConfig::withNetworkMode);
    ipv4Address.ifPresent(containerCmd::withIpv4Address);
    ipv6Address.ifPresent(containerCmd::withIpv6Address);
    entrypoint.ifPresent(containerCmd::withEntrypoint);

    return containerCmd;
}
 
Example #6
Source File: GenericContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * Set any custom settings for the create command such as shared memory size.
 */
private HostConfig buildHostConfig() {
    HostConfig config = new HostConfig();
    if (shmSize != null) {
        config.withShmSize(shmSize);
    }
    if (tmpFsMapping != null) {
        config.withTmpFs(tmpFsMapping);
    }
    return config;
}
 
Example #7
Source File: GenericContainerRuleTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void sharedMemorySetTest() {
    try (GenericContainer containerWithSharedMemory = new GenericContainer()
        .withSharedMemorySize(42L * FileUtils.ONE_MB)
        .withStartupCheckStrategy(new OneShotStartupCheckStrategy())) {

        containerWithSharedMemory.start();

        HostConfig hostConfig = containerWithSharedMemory.getContainerInfo().getHostConfig();
        assertEquals("Shared memory not set on container", hostConfig.getShmSize(), 42L * FileUtils.ONE_MB);
    }
}
 
Example #8
Source File: DockerImpl.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static ContainerResources containerResourcesFromHostConfig(HostConfig hostConfig) {
    // Docker keeps an internal state of what the period and quota are: in cgroups, the quota is always set
    // (default is 100000), but docker will report it as 0 unless explicitly set by the user.
    // This may lead to a state where the quota is set, but period is 0 (accord to docker), which will
    // mess up the calculation below. This can only happen if someone sets it manually, since this class
    // will always set both quota and period.
    final double cpus = hostConfig.getCpuQuota() > 0 ?
            (double) hostConfig.getCpuQuota() / hostConfig.getCpuPeriod() : 0;
    return new ContainerResources(cpus, hostConfig.getCpuShares(), hostConfig.getMemory());
}
 
Example #9
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("Duplicates")
@Test
public void createContainerWithShmSize() throws DockerException {
    HostConfig hostConfig = new HostConfig().withShmSize(96 * FileUtils.ONE_MB);
    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
            .withHostConfig(hostConfig).withCmd("true").exec();

    LOG.info("Created container {}", container.toString());

    assertThat(container.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();

    assertThat(inspectContainerResponse.getHostConfig().getShmSize(), is(hostConfig.getShmSize()));
}
 
Example #10
Source File: DockerTestUtils.java    From module-ballerina-docker with Apache License 2.0 5 votes vote down vote up
/**
 * Create port mapping from host to docker instance.
 *
 * @param dockerPortBindings Ports needed exposed. Key is docker instance port and value is host port.
 * @return The configuration.
 */
private static HostConfig getPortMappingForHost(List<Integer> dockerPortBindings) {
    List<PortBinding> portBindings = new ArrayList<>();

    for (Integer dockerPortBinding : dockerPortBindings) {
        PortBinding portBinding = new PortBinding(
                Ports.Binding.bindIpAndPort("0.0.0.0", Integer.parseInt(dockerPortBinding.toString())),
                ExposedPort.parse(dockerPortBinding.toString()));
        portBindings.add(portBinding);
    }

    return HostConfig.newHostConfig().withPortBindings(portBindings);
}
 
Example #11
Source File: CreateContainerCmdIT.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Test
public void createContainerWithTmpFs() throws DockerException {
    CreateContainerResponse container = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE).withCmd("sleep", "9999")
            .withHostConfig(new HostConfig().withTmpFs(Collections.singletonMap("/tmp", "rw,noexec,nosuid,size=50m"))).exec();

    assertThat(container.getId(), not(is(emptyString())));

    InspectContainerResponse inspectContainerResponse = dockerRule.getClient().inspectContainerCmd(container.getId()).exec();
    assertThat(inspectContainerResponse.getHostConfig().getTmpFs().get("/tmp"), equalTo("rw,noexec,nosuid,size=50m"));
}
 
Example #12
Source File: DockerTemplateBase.java    From docker-plugin with MIT License 5 votes vote down vote up
@Nonnull
private static HostConfig hostConfig(CreateContainerCmd containerConfig) {
    final HostConfig hc = containerConfig.getHostConfig();
    if (hc == null) {
        throw new IllegalStateException("Can't find " + HostConfig.class.getCanonicalName() + " within "
                + CreateContainerCmd.class.getCanonicalName() + " " + containerConfig);
    }
    return hc;
}
 
Example #13
Source File: KuduTestResource.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> start() {
    LOG.info(TestcontainersConfiguration.getInstance().toString());

    try {
        Network kuduNetwork = Network.newNetwork();

        // Setup the Kudu master server container
        masterContainer = new GenericContainer(KUDU_IMAGE).withCommand("master")
                .withExposedPorts(KUDU_MASTER_RPC_PORT, KUDU_MASTER_HTTP_PORT).withNetwork(kuduNetwork)
                .withNetworkAliases(KUDU_MASTER_NETWORK_ALIAS);
        masterContainer = masterContainer.withLogConsumer(new Slf4jLogConsumer(LOG)).waitingFor(Wait.forListeningPort());
        masterContainer.start();

        // Force host name and port, so that the tablet container is accessible from KuduResource, KuduTest and KuduIT.
        Consumer<CreateContainerCmd> consumer = cmd -> {
            Ports portBindings = new Ports();
            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_RPC_PORT), Ports.Binding.bindPort(KUDU_TABLET_RPC_PORT));
            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_HTTP_PORT), Ports.Binding.bindPort(KUDU_TABLET_HTTP_PORT));
            HostConfig hostConfig = HostConfig.newHostConfig().withPortBindings(portBindings)
                    .withNetworkMode(kuduNetwork.getId());
            cmd.withHostName(KUDU_TABLET_NETWORK_ALIAS).withHostConfig(hostConfig);
        };

        // Setup the Kudu tablet server container
        tabletContainer = new GenericContainer(KUDU_IMAGE).withCommand("tserver")
                .withEnv("KUDU_MASTERS", KUDU_MASTER_NETWORK_ALIAS)
                .withExposedPorts(KUDU_TABLET_RPC_PORT, KUDU_TABLET_HTTP_PORT).withNetwork(kuduNetwork)
                .withNetworkAliases(KUDU_TABLET_NETWORK_ALIAS).withCreateContainerCmdModifier(consumer);
        tabletContainer = tabletContainer.withLogConsumer(new Slf4jLogConsumer(LOG)).waitingFor(Wait.forListeningPort());
        tabletContainer.start();

        // Print interesting Kudu servers connectivity information
        final String masterRpcAuthority = masterContainer.getContainerIpAddress() + ":"
                + masterContainer.getMappedPort(KUDU_MASTER_RPC_PORT);
        LOG.info("Kudu master RPC accessible at " + masterRpcAuthority);
        final String masterHttpAuthority = masterContainer.getContainerIpAddress() + ":"
                + masterContainer.getMappedPort(KUDU_MASTER_HTTP_PORT);
        LOG.info("Kudu master HTTP accessible at " + masterHttpAuthority);
        final String tServerRpcAuthority = tabletContainer.getContainerIpAddress() + ":"
                + tabletContainer.getMappedPort(KUDU_TABLET_RPC_PORT);
        LOG.info("Kudu tablet server RPC accessible at " + tServerRpcAuthority);
        final String tServerHttpAuthority = tabletContainer.getContainerIpAddress() + ":"
                + tabletContainer.getMappedPort(KUDU_TABLET_HTTP_PORT);
        LOG.info("Kudu tablet server HTTP accessible at " + tServerHttpAuthority);

        return CollectionHelper.mapOf(KUDU_AUTHORITY_CONFIG_KEY, masterRpcAuthority);
    } catch (Exception ex) {
        LOG.error("Issue starting KuduTestResource, please have a look at KuduInfrastructureTestHelper", ex);
        return CollectionHelper.mapOf(KUDU_AUTHORITY_CONFIG_KEY,
                "Please_have_a_look_at_KuduInfrastructureTestHelper");
    }
}
 
Example #14
Source File: SerializableInspectContainerResponse.java    From jbpm-work-items with Apache License 2.0 4 votes vote down vote up
@Override
public HostConfig getHostConfig() {
    return hostConfig;
}
 
Example #15
Source File: CreateContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public CreateContainerCmd withHostConfig(HostConfig hostConfig) {
    this.hostConfig = hostConfig;
    return this;
}
 
Example #16
Source File: CreateContainerCmdImpl.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Override
public HostConfig getHostConfig() {
    return hostConfig;
}
 
Example #17
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@CheckForNull
HostConfig getHostConfig();
 
Example #18
Source File: InspectContainerResponse.java    From docker-java with Apache License 2.0 4 votes vote down vote up
public HostConfig getHostConfig() {
    return hostConfig;
}
 
Example #19
Source File: BesuNode.java    From ethsigner with Apache License 2.0 4 votes vote down vote up
private String createBesuContainer(final NodeConfiguration config) {
  final String genesisFilePath = genesisFilePath(config.getGenesisFilePath());
  LOG.info("Path to Genesis file: {}", genesisFilePath);
  final Volume genesisVolume = new Volume("/etc/besu/genesis.json");
  final Bind genesisBinding = new Bind(genesisFilePath, genesisVolume);
  final Bind privacyBinding = privacyVolumeBinding("enclave_key.pub");
  final List<Bind> bindings = Lists.newArrayList(genesisBinding, privacyBinding);

  try {
    final List<String> commandLineItems =
        Lists.newArrayList(
            "--genesis-file",
            genesisVolume.getPath(),
            "--logging",
            "DEBUG",
            "--miner-enabled",
            "--miner-coinbase",
            "1b23ba34ca45bb56aa67bc78be89ac00ca00da00",
            "--host-whitelist",
            "*",
            "--rpc-http-enabled",
            "--rpc-ws-enabled",
            "--rpc-http-apis",
            "ETH,NET,WEB3,EEA",
            "--privacy-enabled",
            "--privacy-public-key-file",
            privacyBinding.getVolume().getPath());

    config
        .getCors()
        .ifPresent(
            cors -> commandLineItems.addAll(Lists.newArrayList("--rpc-http-cors-origins", cors)));

    LOG.debug("besu command line {}", config);

    final HostConfig hostConfig =
        HostConfig.newHostConfig()
            .withPortBindings(httpRpcPortBinding(), wsRpcPortBinding())
            .withBinds(bindings);
    final CreateContainerCmd createBesu =
        docker
            .createContainerCmd(BESU_IMAGE)
            .withHostConfig(hostConfig)
            .withVolumes(genesisVolume)
            .withCmd(commandLineItems);

    LOG.info("Creating the Besu Docker container...");
    final CreateContainerResponse besu = createBesu.exec();
    LOG.info("Created Besu Docker container, id: " + besu.getId());
    return besu.getId();
  } catch (final NotFoundException e) {
    throw new RuntimeException(
        "Before you run the acceptance tests, execute 'docker pull hyperledger/besu:latest'", e);
  }
}
 
Example #20
Source File: UpdateContainerCmdIT.java    From docker-java with Apache License 2.0 4 votes vote down vote up
@Test
    public void updateContainer() throws DockerException, IOException {
        assumeThat("API version should be >= 1.22", dockerRule, isGreaterOrEqual(VERSION_1_22));

        CreateContainerResponse response = dockerRule.getClient().createContainerCmd(DEFAULT_IMAGE)
                .withCmd("sleep", "9999")
                .exec();

        String containerId = response.getId();
        dockerRule.getClient().startContainerCmd(containerId).exec();

        InspectContainerResponse inspectBefore = dockerRule.getClient().inspectContainerCmd(containerId).exec();
        LOG.debug("Inspect: {}", inspectBefore);
        final Long memory = inspectBefore.getHostConfig().getMemory();

        dockerRule.getClient().updateContainerCmd(containerId)
                .withBlkioWeight(300)
                .withCpuShares(512)
                .withCpuPeriod(100000)
                .withCpuQuota(50000)
//                .withCpusetCpus("0") // depends on env
                .withCpusetMems("0")
//                .withMemory(209715200L + 2L)
//                .withMemorySwap(514288000L) Your kernel does not support swap limit capabilities, memory limited without swap.
//                .withMemoryReservation(209715200L)
//                .withKernelMemory(52428800) Can not update kernel memory to a running container, please stop it first.
                .exec();

        // true only on docker toolbox (1.10.1)
//        assertThat(updateResponse.getWarnings(), hasSize(1));
//        assertThat(updateResponse.getWarnings().get(0),
//                is("Your kernel does not support Block I/O weight. Weight discarded."));

        InspectContainerResponse inspectAfter = dockerRule.getClient().inspectContainerCmd(containerId).exec();
        final HostConfig afterHostConfig = inspectAfter.getHostConfig();

//        assertThat(afterHostConfig.getMemory(), is(209715200L + 2L));

//        assertThat(afterHostConfig.getBlkioWeight(), is(300));
        assertThat(afterHostConfig.getCpuShares(), is(512));
        assertThat(afterHostConfig.getCpuPeriod(), is(100000L));
        assertThat(afterHostConfig.getCpuQuota(), is(50000L));
        assertThat(afterHostConfig.getCpusetMems(), is("0"));

//        assertThat(afterHostConfig.getMemoryReservation(), is(209715200L));
//       assertThat(afterHostConfig.getMemorySwap(), is(514288000L));

    }
 
Example #21
Source File: CreateContainerCmd.java    From docker-java with Apache License 2.0 votes vote down vote up
CreateContainerCmd withHostConfig(HostConfig hostConfig);