Search in sources :

Example 1 with HostConfig

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

the class DockerConnection method createContainer.

@Override
public String createContainer(final IDockerContainerConfig c, IDockerHostConfig hc, final String containerName) throws DockerException, InterruptedException {
    try {
        HostConfig.Builder hbuilder = HostConfig.builder().containerIdFile(hc.containerIDFile()).publishAllPorts(hc.publishAllPorts()).privileged(hc.privileged()).networkMode(hc.networkMode()).readonlyRootfs(((DockerHostConfig) hc).readonlyRootfs());
        if (((DockerHostConfig) hc).tmpfs() != null) {
            hbuilder.tmpfs(ImmutableMap.copyOf(((DockerHostConfig) hc).tmpfs()));
        }
        if (((DockerHostConfig) hc).capAdd() != null) {
            hbuilder.capAdd(((DockerHostConfig) hc).capAdd());
        }
        if (((DockerHostConfig) hc).capDrop() != null) {
            hbuilder.capDrop(((DockerHostConfig) hc).capDrop());
        }
        if (hc.binds() != null)
            hbuilder.binds(hc.binds());
        if (hc.dns() != null)
            hbuilder.dns(hc.dns());
        if (hc.dnsSearch() != null)
            hbuilder.dnsSearch(hc.dnsSearch());
        if (hc.links() != null)
            hbuilder.links(hc.links());
        if (hc.lxcConf() != null) {
            List<IDockerConfParameter> lxcconf = hc.lxcConf();
            ArrayList<LxcConfParameter> lxcreal = new ArrayList<>();
            for (IDockerConfParameter param : lxcconf) {
            // TODO: Fix This
            }
            hbuilder.lxcConf(lxcreal);
        }
        if (hc.portBindings() != null) {
            Map<String, List<IDockerPortBinding>> bindings = hc.portBindings();
            HashMap<String, List<PortBinding>> realBindings = new HashMap<>();
            for (Entry<String, List<IDockerPortBinding>> entry : bindings.entrySet()) {
                String key = entry.getKey();
                List<IDockerPortBinding> bindingList = entry.getValue();
                ArrayList<PortBinding> newList = new ArrayList<>();
                for (IDockerPortBinding binding : bindingList) {
                    newList.add(PortBinding.of(binding.hostIp(), binding.hostPort()));
                }
                realBindings.put(key, newList);
            }
            hbuilder.portBindings(realBindings);
        }
        if (hc.volumesFrom() != null) {
            hbuilder.volumesFrom(hc.volumesFrom());
        }
        if (hc.securityOpt() != null) {
            hbuilder.securityOpt(hc.securityOpt());
        }
        // interface
        if (((DockerHostConfig) hc).memory() != null) {
            hbuilder.memory(((DockerHostConfig) hc).memory());
        }
        // interface
        if (((DockerHostConfig) hc).cpuShares() != null && ((DockerHostConfig) hc).cpuShares().longValue() > 0) {
            hbuilder.cpuShares(((DockerHostConfig) hc).cpuShares());
        }
        ContainerConfig.Builder builder = ContainerConfig.builder().hostname(c.hostname()).domainname(c.domainname()).user(c.user()).attachStdin(c.attachStdin()).attachStdout(c.attachStdout()).attachStderr(c.attachStderr()).tty(c.tty()).openStdin(c.openStdin()).stdinOnce(c.stdinOnce()).cmd(c.cmd()).image(c.image()).hostConfig(hbuilder.build()).workingDir(c.workingDir()).labels(c.labels()).networkDisabled(c.networkDisabled());
        // don't set those fields in the builder.
        if (c.portSpecs() != null) {
            builder = builder.portSpecs(c.portSpecs());
        }
        if (c.exposedPorts() != null) {
            builder = builder.exposedPorts(c.exposedPorts());
        }
        if (c.env() != null) {
            builder = builder.env(c.env());
        }
        if (c.volumes() != null) {
            builder = builder.volumes(c.volumes());
        }
        if (c.entrypoint() != null) {
            builder = builder.entrypoint(c.entrypoint());
        }
        if (c.onBuild() != null) {
            builder = builder.onBuild(c.onBuild());
        }
        // create container with default random name if an empty/null
        // containerName argument was passed
        final ContainerCreation creation = client.createContainer(builder.build(), (containerName != null && !containerName.isEmpty()) ? containerName : null);
        final String id = creation.id();
        // force a refresh of the current containers to include the new one
        listContainers();
        return id;
    } catch (ContainerNotFoundException e) {
        throw new DockerContainerNotFoundException(e);
    } catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
        throw new DockerException(e.message());
    } catch (com.spotify.docker.client.exceptions.DockerException e) {
        throw new DockerException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) IDockerContainerConfig(org.eclipse.linuxtools.docker.core.IDockerContainerConfig) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) HostConfig(com.spotify.docker.client.messages.HostConfig) LxcConfParameter(com.spotify.docker.client.messages.HostConfig.LxcConfParameter) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) List(java.util.List) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) IDockerConfParameter(org.eclipse.linuxtools.docker.core.IDockerConfParameter) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) PortBinding(com.spotify.docker.client.messages.PortBinding)

Example 2 with HostConfig

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

the class KubernetesPushingGopherIT method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    DockerClient client = DefaultDockerClient.fromEnv().build();
    logger.info("Downloading registry image");
    client.pull("registry:2");
    this.registryPort = 5000;
    final Map<String, List<PortBinding>> ports = singletonMap("5000/tcp", Collections.singletonList(PortBinding.of("0.0.0.0", this.registryPort)));
    final HostConfig hostConfig = HostConfig.builder().portBindings(ports).build();
    logger.info("Creating Local Registry Container");
    ContainerConfig config = ContainerConfig.builder().hostConfig(hostConfig).image("registry:2").build();
    String id = client.createContainer(config).id();
    logger.info("Registry container id: {}", id);
    logger.info("Starting registry container");
    client.startContainer(id);
    this.runningContainers.add(id);
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerClient(com.spotify.docker.client.DockerClient) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) HostConfig(com.spotify.docker.client.messages.HostConfig) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with HostConfig

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

the class DefaultDockerClientTest method testContainerWithMoreCpuOptions.

@Test
public void testContainerWithMoreCpuOptions() throws Exception {
    requireDockerApiVersionAtLeast("1.19", "Container creation with more cpu options");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig expected = HostConfig.builder().cpuShares(4096L).cpuPeriod(100000L).cpuQuota(50000L).cpusetCpus("0,1").cpusetMems("0").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();
    assertThat(actual.cpuShares(), equalTo(expected.cpuShares()));
    assertThat(actual.cpuPeriod(), equalTo(expected.cpuPeriod()));
    assertThat(actual.cpuQuota(), equalTo(expected.cpuQuota()));
    assertThat(actual.cpusetCpus(), equalTo(expected.cpusetCpus()));
}
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 4 with HostConfig

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

the class DefaultDockerClientTest method testContainerWithHostConfig.

@Test
public void testContainerWithHostConfig() throws Exception {
    requireDockerApiVersionAtLeast("1.18", "Container creation with HostConfig");
    sut.pull(BUSYBOX_LATEST);
    final boolean privileged = true;
    final boolean publishAllPorts = true;
    final String dns = "1.2.3.4";
    final List<Ulimit> ulimits = newArrayList(Ulimit.builder().name("nofile").soft(1024L).hard(2048L).build());
    final Device expectedDevice = Device.builder().pathOnHost(".").pathInContainer("/foo").cgroupPermissions("mrw").build();
    final HostConfig.Builder hostConfigBuilder = HostConfig.builder().privileged(privileged).publishAllPorts(publishAllPorts).dns(dns).dnsSearch("domain1", "domain2").devices(expectedDevice).ulimits(ulimits);
    if (dockerApiVersionAtLeast("1.21")) {
        hostConfigBuilder.dnsOptions("some", "options");
    }
    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();
    assertThat(actual.privileged(), equalTo(expected.privileged()));
    assertThat(actual.publishAllPorts(), equalTo(expected.publishAllPorts()));
    assertThat(actual.dns(), equalTo(expected.dns()));
    if (dockerApiVersionAtLeast("1.21")) {
        assertThat(actual.dnsOptions(), equalTo(expected.dnsOptions()));
    }
    assertThat(actual.dnsSearch(), equalTo(expected.dnsSearch()));
    assertEquals(ulimits, actual.ulimits());
    assertThat(actual.devices(), contains(expectedDevice));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) Device(com.spotify.docker.client.messages.Device) HostConfig(com.spotify.docker.client.messages.HostConfig) Ulimit(com.spotify.docker.client.messages.HostConfig.Ulimit) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with HostConfig

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

the class DefaultDockerClientTest method testContainerVolumesOldStyle.

@Test
@SuppressWarnings("deprecation")
public void testContainerVolumesOldStyle() throws Exception {
    requireDockerApiVersionLessThan("1.20", "Creating a container with volumes and inspecting volumes in old style");
    sut.pull(BUSYBOX_LATEST);
    final HostConfig hostConfig = HostConfig.builder().binds(Bind.from("/local/path").to("/remote/path").build()).build();
    final ContainerConfig volumeConfig = ContainerConfig.builder().image(BUSYBOX_LATEST).addVolume("/foo").hostConfig(hostConfig).build();
    final String id = sut.createContainer(volumeConfig, randomName()).id();
    final ContainerInfo volumeContainer = sut.inspectContainer(id);
    final List<String> expectedDestinations = newArrayList("/foo", "/remote/path");
    final Set<String> actualDestinations = volumeContainer.volumes().keySet();
    // To make sure two sets are equal, when they may be in different orders,
    // we check that each one contains all the elements of the other.
    // Equivalent to, in math, proving two sets are one-to-one by proving
    // they are injective ("into") and surjective ("onto").
    assertThat(actualDestinations, everyItem(isIn(expectedDestinations)));
    assertThat(expectedDestinations, everyItem(isIn(actualDestinations)));
    // The local paths returned from ContainerInfo.volumes() are paths in the docker
    // file system. So they are not predictable (at least by me, the test writer,
    // John Flavin.) However, the local path we asked for will always be included as part of
    // the path that is returned. So we can just check that one in the list of items
    // we got back contains our expected path.
    final String expectedLocalPath = "/local/path";
    assertThat(volumeContainer.volumes().values(), hasItem(containsString(expectedLocalPath)));
    assertThat(volumeContainer.config().volumes(), hasItem("/foo"));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) 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) Test(org.junit.Test)

Aggregations

HostConfig (com.spotify.docker.client.messages.HostConfig)55 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)46 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)31 Test (org.junit.Test)30 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)12 DockerException (com.spotify.docker.client.exceptions.DockerException)9 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)8 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)6 PortBinding (com.spotify.docker.client.messages.PortBinding)5 IOException (java.io.IOException)5 LogStream (com.spotify.docker.client.LogStream)4 ContainerMount (com.spotify.docker.client.messages.ContainerMount)4 URL (java.net.URL)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3