Search in sources :

Example 46 with HostConfig

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

the class DefaultDockerClientTest method testUpdateContainer.

@Test
public void testUpdateContainer() throws Exception {
    requireDockerApiVersionAtLeast("1.22", "update container");
    final String containerName = randomName();
    final HostConfig hostConfig = HostConfig.builder().cpuShares(256L).build();
    final ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image(BUSYBOX_LATEST).build();
    sut.pull(BUSYBOX_LATEST);
    final ContainerCreation container = sut.createContainer(config, containerName);
    final ContainerInfo containerInfo = sut.inspectContainer(container.id());
    assertThat(containerInfo.hostConfig().cpuShares(), is(256L));
    final HostConfig newHostConfig = HostConfig.builder().cpuShares(512L).build();
    final ContainerUpdate containerUpdate = sut.updateContainer(containerInfo.id(), newHostConfig);
    assertThat(containerUpdate.warnings(), is(nullValue()));
    final ContainerInfo newContainerInfo = sut.inspectContainer(container.id());
    assertThat(newContainerInfo.hostConfig().cpuShares(), is(512L));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ContainerUpdate(com.spotify.docker.client.messages.ContainerUpdate) Test(org.junit.Test)

Example 47 with HostConfig

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

the class DefaultDockerClientUnitTest method testNanoCpus.

@Test
public void testNanoCpus() throws Exception {
    final DefaultDockerClient dockerClient = new DefaultDockerClient(builder);
    final HostConfig hostConfig = HostConfig.builder().nanoCpus(2_000_000_000L).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).build();
    server.enqueue(new MockResponse());
    dockerClient.createContainer(containerConfig);
    final RecordedRequest recordedRequest = takeRequestImmediately();
    final JsonNode requestJson = toJson(recordedRequest.getBody());
    final JsonNode nanoCpus = requestJson.get("HostConfig").get("NanoCpus");
    assertThat(hostConfig.nanoCpus(), is(nanoCpus.longValue()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HostConfig(com.spotify.docker.client.messages.HostConfig) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 48 with HostConfig

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

the class PushPullIT method startUnauthedRegistry.

private static String startUnauthedRegistry(final DockerClient client) throws Exception {
    final Map<String, List<PortBinding>> ports = singletonMap("5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", 5000)));
    final HostConfig hostConfig = HostConfig.builder().portBindings(ports).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().image(REGISTRY_IMAGE).hostConfig(hostConfig).build();
    return startAndAwaitContainer(client, containerConfig, REGISTRY_NAME);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 49 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() {
    this.limitDateBeforeKilling = new Date(new Date().toInstant().plus(DELAY_BEFORE_KILLING_DOCKER_IMAGE, ChronoUnit.MINUTES).toEpochMilli());
    DockerClient docker = this.poolManager.getDockerClient();
    try {
        LOGGER.info("Start to build and run container for build id " + this.inputBuildId.getBuggyBuildId());
        LOGGER.info("At most this docker run will be killed at: " + this.limitDateBeforeKilling);
        Map<String, String> labels = new HashMap<>();
        labels.put("name", this.containerName);
        HostConfig hostConfig = HostConfig.builder().appendBinds(this.logDirectory + ":/var/log").build();
        ContainerConfig containerConfig = ContainerConfig.builder().image(imageId).env(envValues).hostname(Utils.getHostname()).hostConfig(hostConfig).labels(labels).build();
        LOGGER.info("Create the container: " + this.containerName);
        ContainerCreation container = docker.createContainer(containerConfig);
        this.volumes = containerConfig.volumeNames();
        this.containerId = container.id();
        treatedBuildTracking.setContainerId(this.containerId);
        LOGGER.info("Start the container: " + this.containerName);
        docker.startContainer(container.id());
        ContainerExit exitStatus = docker.waitContainer(this.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(this.containerId);
            this.removeVolumes(docker);
        }
        serialize("TREATED");
    } catch (InterruptedException e) {
        LOGGER.error("Error while running the container for build id " + this.inputBuildId.getBuggyBuildId(), e);
        killDockerContainer(docker, false);
    } catch (DockerException e) {
        LOGGER.error("Error while creating or running the container for build id " + this.inputBuildId.getBuggyBuildId(), e);
        serialize("ERROR");
    }
    this.poolManager.removeSubmittedRunnablePipelineContainer(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) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerExit(com.spotify.docker.client.messages.ContainerExit)

Example 50 with HostConfig

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

the class DockerContainerMock method getMockedDockerContainerClient.

@SuppressWarnings("ConstantConditions")
public static DockerContainerClient getMockedDockerContainerClient(String networkName) {
    DockerClient dockerClient = mock(DockerClient.class);
    ExecCreation execCreation = mock(ExecCreation.class);
    LogStream logStream = mock(LogStream.class);
    when(logStream.readFully()).thenReturn("ANY_STRING");
    when(execCreation.id()).thenReturn("ANY_ID");
    ContainerCreation containerCreation = mock(ContainerCreation.class);
    when(containerCreation.id()).thenReturn("ANY_CONTAINER_ID");
    AttachedNetwork attachedNetwork = mock(AttachedNetwork.class);
    NetworkSettings networkSettings = mock(NetworkSettings.class);
    HostConfig hostConfig = mock(HostConfig.class);
    ImageInfo imageInfo = mock(ImageInfo.class);
    ContainerConfig containerConfig = mock(ContainerConfig.class);
    ContainerInfo containerInfo = mock(ContainerInfo.class);
    ContainerMount tmpMountedMount = mock(ContainerMount.class);
    when(tmpMountedMount.destination()).thenReturn("/tmp/node/tmp/mounted");
    when(tmpMountedMount.source()).thenReturn("/tmp/mounted");
    ContainerMount homeFolderMount = mock(ContainerMount.class);
    when(homeFolderMount.destination()).thenReturn("/tmp/node/home/seluser/folder");
    when(homeFolderMount.source()).thenReturn("/tmp/folder");
    when(containerInfo.mounts()).thenReturn(ImmutableList.of(tmpMountedMount, homeFolderMount));
    when(attachedNetwork.ipAddress()).thenReturn("127.0.0.1");
    when(networkSettings.networks()).thenReturn(ImmutableMap.of(networkName, attachedNetwork));
    when(networkSettings.ipAddress()).thenReturn("");
    when(containerInfo.networkSettings()).thenReturn(networkSettings);
    when(hostConfig.extraHosts()).thenReturn(null);
    when(containerInfo.hostConfig()).thenReturn(hostConfig);
    String[] httpEnvVars = { "zalenium_http_proxy=http://34.211.100.239:8080", "zalenium_https_proxy=http://34.211.100.239:8080" };
    when(containerConfig.env()).thenReturn(ImmutableList.copyOf(Arrays.asList(httpEnvVars)));
    when(containerInfo.config()).thenReturn(containerConfig);
    String containerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container container_40000 = mock(Container.class);
    when(container_40000.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40000")));
    when(container_40000.id()).thenReturn(containerId);
    when(container_40000.status()).thenReturn("running");
    when(container_40000.image()).thenReturn("elgalu/selenium");
    Container container_40001 = mock(Container.class);
    when(container_40001.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium_40001")));
    when(container_40001.id()).thenReturn(containerId);
    when(container_40001.status()).thenReturn("running");
    when(container_40001.image()).thenReturn("elgalu/selenium");
    String zaleniumContainerId = RandomStringUtils.randomAlphabetic(30).toLowerCase();
    Container zalenium = mock(Container.class);
    when(zalenium.names()).thenReturn(ImmutableList.copyOf(Collections.singletonList("/zalenium")));
    when(zalenium.id()).thenReturn(zaleniumContainerId);
    when(zalenium.status()).thenReturn("running");
    when(zalenium.image()).thenReturn("dosel/zalenium");
    Info dockerInfo = mock(Info.class);
    when(dockerInfo.name()).thenReturn("ubuntu_vm");
    try {
        URL logsLocation = TestUtils.class.getClassLoader().getResource("logs.tar");
        URL videosLocation = TestUtils.class.getClassLoader().getResource("videos.tar");
        File logsFile = new File(logsLocation.getPath());
        File videosFile = new File(videosLocation.getPath());
        when(dockerClient.archiveContainer(containerId, "/var/log/cont/")).thenReturn(new FileInputStream(logsFile));
        when(dockerClient.archiveContainer(containerId, "/videos/")).thenReturn(new FileInputStream(videosFile));
        String[] startVideo = { "bash", "-c", START_RECORDING.getContainerAction() };
        String[] stopVideo = { "bash", "-c", STOP_RECORDING.getContainerAction() };
        String[] transferLogs = { "bash", "-c", TRANSFER_LOGS.getContainerAction() };
        String[] cleanupContainer = { "bash", "-c", CLEANUP_CONTAINER.getContainerAction() };
        String[] sendNotificationCompleted = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(COMPLETED.getTestNotificationMessage()) };
        String[] sendNotificationTimeout = { "bash", "-c", SEND_NOTIFICATION.getContainerAction().concat(TIMEOUT.getTestNotificationMessage()) };
        when(dockerClient.execCreate(containerId, startVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, stopVideo, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, transferLogs, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, cleanupContainer, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationCompleted, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execCreate(containerId, sendNotificationTimeout, DockerClient.ExecCreateParam.attachStdout(), DockerClient.ExecCreateParam.attachStderr(), DockerClient.ExecCreateParam.attachStdin())).thenReturn(execCreation);
        when(dockerClient.execStart(anyString())).thenReturn(logStream);
        doNothing().when(dockerClient).stopContainer(anyString(), anyInt());
        when(dockerClient.info()).thenReturn(dockerInfo);
        when(dockerClient.createContainer(any(ContainerConfig.class), anyString())).thenReturn(containerCreation);
        when(dockerClient.listContainers(DockerClient.ListContainersParam.allContainers())).thenReturn(Arrays.asList(container_40000, container_40001, zalenium));
        when(containerConfig.labels()).thenReturn(ImmutableMap.of("selenium_firefox_version", "52", "selenium_chrome_version", "58"));
        when(imageInfo.config()).thenReturn(containerConfig);
        when(dockerClient.inspectContainer(null)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(zaleniumContainerId)).thenReturn(containerInfo);
        when(dockerClient.inspectContainer(containerId)).thenReturn(containerInfo);
        when(dockerClient.inspectImage(anyString())).thenReturn(imageInfo);
        when(dockerClient.listImages(DockerClient.ListImagesParam.byName("elgalu/selenium"))).thenReturn(Collections.emptyList());
    } catch (DockerException | InterruptedException | IOException e) {
        e.printStackTrace();
    }
    DockerContainerClient dockerContainerClient = new DockerContainerClient();
    dockerContainerClient.setContainerClient(dockerClient);
    return dockerContainerClient;
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) DockerClient(com.spotify.docker.client.DockerClient) DockerContainerClient(de.zalando.ep.zalenium.container.DockerContainerClient) LogStream(com.spotify.docker.client.LogStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IOException(java.io.IOException) Info(com.spotify.docker.client.messages.Info) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) URL(java.net.URL) FileInputStream(java.io.FileInputStream) AttachedNetwork(com.spotify.docker.client.messages.AttachedNetwork) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ExecCreation(com.spotify.docker.client.messages.ExecCreation) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) NetworkSettings(com.spotify.docker.client.messages.NetworkSettings) Container(com.spotify.docker.client.messages.Container) HostConfig(com.spotify.docker.client.messages.HostConfig) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImageInfo(com.spotify.docker.client.messages.ImageInfo) File(java.io.File) ContainerMount(com.spotify.docker.client.messages.ContainerMount)

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