Search in sources :

Example 21 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testExtraHosts.

@Test
public void testExtraHosts() throws Exception {
    requireDockerApiVersionAtLeast("1.15", "Container Creation with HostConfig.ExtraHosts");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig expected = HostConfig.builder().extraHosts("extrahost:1.2.3.4").build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).cmd("sh", "-c", "cat /etc/hosts | grep extrahost").build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    sut.startContainer(id);
    final HostConfig actual = sut.inspectContainer(id).hostConfig();
    assertThat(actual.extraHosts(), equalTo(expected.extraHosts()));
    final String logs;
    try (LogStream stream = sut.logs(id, stdout(), stderr())) {
        logs = stream.readFully();
    }
    assertThat(logs, containsString("1.2.3.4"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 22 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testLogDriver.

@Test
public void testLogDriver() throws Exception {
    requireDockerApiVersionAtLeast("1.21", "Container Creation with HostConfig.LogConfig");
    sut.pull(BUSYBOX_LATEST);
    final String name = randomName();
    final Map<String, String> logOptions = new HashMap<>();
    logOptions.put("max-size", "10k");
    logOptions.put("max-file", "2");
    logOptions.put("labels", name);
    final LogConfig logConfig = LogConfig.create("json-file", logOptions);
    assertThat(logConfig.logType(), equalTo("json-file"));
    assertThat(logConfig.logOptions(), equalTo(logOptions));
    final HostConfig expected = HostConfig.builder().logConfig(logConfig).build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).build();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    sut.startContainer(id);
    final HostConfig actual = sut.inspectContainer(id).hostConfig();
    assertThat(actual.logConfig(), equalTo(expected.logConfig()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HashMap(java.util.HashMap) HostConfig(com.spotify.docker.client.messages.HostConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) LogConfig(com.spotify.docker.client.messages.LogConfig) Test(org.junit.Test)

Example 23 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project docker-client by spotify.

the class DefaultDockerClientTest method testContainerWithBlkioOptions.

@Test
public void testContainerWithBlkioOptions() throws Exception {
    requireDockerApiVersionAtLeast("1.19", "Container creation with blkio options");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig.Builder hostConfigBuilder = HostConfig.builder();
    if (dockerApiVersionAtLeast("1.19")) {
        hostConfigBuilder.blkioWeight(300);
    }
    if (dockerApiVersionAtLeast("1.22")) {
        // TODO (dxia) Some kernels don't support blkio weight. How detect to skip this check?
        // hostConfigBuilder.blkioWeightDevice(ImmutableList.of(
        // HostConfig.BlkioWeightDevice.builder().path("/dev/random").weight(500).build(),
        // HostConfig.BlkioWeightDevice.builder().path("/dev/urandom").weight(200).build()
        // ));
        final List<HostConfig.BlkioDeviceRate> deviceRates = ImmutableList.of(HostConfig.BlkioDeviceRate.builder().path("/dev/loop0").rate(1024).build());
        hostConfigBuilder.blkioDeviceReadBps(deviceRates);
        hostConfigBuilder.blkioDeviceWriteBps(deviceRates);
        hostConfigBuilder.blkioDeviceReadIOps(deviceRates);
        hostConfigBuilder.blkioDeviceWriteIOps(deviceRates);
    }
    final HostConfig expected = hostConfigBuilder.build();
    final ContainerConfig config = ContainerConfig.builder().image(BUSYBOX_LATEST).hostConfig(expected).build();
    final String name = randomName();
    final ContainerCreation creation = sut.createContainer(config, name);
    final String id = creation.id();
    sut.startContainer(id);
    final HostConfig actual = sut.inspectContainer(id).hostConfig();
    if (dockerApiVersionAtLeast("1.19")) {
    // TODO (dxia) Some kernels don't support blkio weight. How detect to skip this check?
    // assertThat(actual.blkioWeight(), equalTo(expected.blkioWeight()));
    }
    if (dockerApiVersionAtLeast("1.22")) {
        // TODO (dxia) Some kernels don't support blkio weight device. How detect to skip this check?
        // assertThat(actual.blkioWeightDevice(), equalTo(expected.blkioWeightDevice()));
        assertThat(actual.blkioDeviceReadBps(), equalTo(expected.blkioDeviceReadBps()));
        assertThat(actual.blkioDeviceWriteBps(), equalTo(expected.blkioDeviceWriteBps()));
        assertThat(actual.blkioDeviceReadIOps(), equalTo(expected.blkioDeviceReadIOps()));
        assertThat(actual.blkioDeviceWriteBps(), equalTo(expected.blkioDeviceWriteBps()));
    }
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 24 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project repairnator by Spirals-Team.

the class RunnablePipelineContainer method run.

@Override
public void run() {
    String containerId = null;
    DockerClient docker = Launcher.docker;
    try {
        LOGGER.info("Start to run check container for branch " + branchName);
        String containerName = "checkbranch_" + branchName;
        String humanPatchStr = "";
        if (this.repairnatorConfig.isHumanPatch()) {
            humanPatchStr = "--human-patch";
        }
        String[] envValues = new String[] { "BRANCH_NAME=" + this.branchName, "REPOSITORY=" + this.repairnatorConfig.getRepository(), "HUMAN_PATCH=" + humanPatchStr };
        Map<String, String> labels = new HashMap<>();
        labels.put("name", containerName);
        HostConfig hostConfig = HostConfig.builder().appendBinds(this.repairnatorConfig.getOutputPath() + ":/tmp/result.txt").build();
        ContainerConfig containerConfig = ContainerConfig.builder().image(imageId).env(envValues).hostname(Utils.getHostname()).hostConfig(hostConfig).labels(labels).build();
        LOGGER.info("Create the container: " + containerName);
        ContainerCreation container = docker.createContainer(containerConfig);
        containerId = container.id();
        LOGGER.info("Start the container: " + containerName + " (id: " + containerId + ")");
        docker.startContainer(container.id());
        ContainerExit exitStatus = docker.waitContainer(containerId);
        LOGGER.info("The container has finished with status code: " + exitStatus.statusCode());
        if (!this.repairnatorConfig.isSkipDelete() && exitStatus.statusCode() == 0) {
            LOGGER.info("Container will be removed.");
            docker.removeContainer(containerId);
        }
        LOGGER.info("TREATED Docker container : " + containerId);
    } catch (InterruptedException e) {
        LOGGER.error("Error while running the container for branch name " + branchName, e);
        killDockerContainer(docker, containerId);
    } catch (DockerException e) {
        LOGGER.error("Error while creating or running the container for branch name " + branchName, e);
    }
    Launcher.submittedRunnablePipelineContainers.remove(this);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerException(com.spotify.docker.client.exceptions.DockerException) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) DockerClient(com.spotify.docker.client.DockerClient) HashMap(java.util.HashMap) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit)

Example 25 with HostConfig

use of com.spotify.docker.client.messages.HostConfig in project helios by spotify.

the class BindVolumeContainerDecorator method decorateHostConfig.

@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);
}
Also used : HostConfig(com.spotify.docker.client.messages.HostConfig)

Aggregations

HostConfig (com.spotify.docker.client.messages.HostConfig)53 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)45 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)30 Test (org.junit.Test)29 Matchers.containsString (org.hamcrest.Matchers.containsString)22 Long.toHexString (java.lang.Long.toHexString)19 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)19 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)16 DockerClient (com.spotify.docker.client.DockerClient)12 List (java.util.List)11 DockerException (com.spotify.docker.client.exceptions.DockerException)8 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 LogStream (com.spotify.docker.client.LogStream)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)4 PortBinding (com.spotify.docker.client.messages.PortBinding)4 IOException (java.io.IOException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ImmutableList (com.google.common.collect.ImmutableList)3