Search in sources :

Example 1 with PortBinding

use of com.spotify.docker.client.messages.PortBinding 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 PortBinding

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

the class DefaultDockerClientTest method testInspectContainerWithExposedPorts.

@Test
public void testInspectContainerWithExposedPorts() throws Exception {
    sut.pull(MEMCACHED_LATEST);
    final ContainerConfig config = ContainerConfig.builder().image(MEMCACHED_LATEST).build();
    final ContainerCreation container = sut.createContainer(config, randomName());
    sut.startContainer(container.id());
    final ContainerInfo containerInfo = sut.inspectContainer(container.id());
    assertThat(containerInfo, notNullValue());
    assertThat(containerInfo.networkSettings().ports(), hasEntry("11211/tcp", Collections.<PortBinding>emptyList()));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Test(org.junit.Test)

Example 3 with PortBinding

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

the class TaskConfig method portBindings.

/**
 * Create a port binding configuration for the job.
 *
 * @return The port bindings.
 */
private Map<String, List<PortBinding>> portBindings() {
    final Map<String, List<PortBinding>> bindings = Maps.newHashMap();
    for (final Map.Entry<String, PortMapping> e : job.getPorts().entrySet()) {
        final PortMapping mapping = e.getValue();
        final Integer jobDefinedExtPort = mapping.getExternalPort();
        // If the job didn't specify an external port, use dynamically allocated ports
        final String externalPort = jobDefinedExtPort == null ? ports.get(e.getKey()).toString() : jobDefinedExtPort.toString();
        final PortBinding binding = PortBinding.of(mapping.getIp(), externalPort);
        final String entry = containerPort(mapping.getInternalPort(), mapping.getProtocol());
        bindings.put(entry, Collections.singletonList(binding));
    }
    return bindings;
}
Also used : PortBinding(com.spotify.docker.client.messages.PortBinding) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PortMapping(com.spotify.helios.common.descriptors.PortMapping) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with PortBinding

use of com.spotify.docker.client.messages.PortBinding in project kie-wb-common by kiegroup.

the class DockerRuntimeExecExecutor method create.

private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
    if (runtimeConfig.isPull()) {
        try {
            LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
            docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
        } catch (DockerException | InterruptedException ex) {
            LOG.error(ex.getMessage(), ex);
            throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
        }
    }
    final String[] ports = { runtimeConfig.getPort() };
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
    if (!_dockerProvider.isPresent()) {
        return Optional.empty();
    }
    final DockerProvider dockerProvider = _dockerProvider.get();
    final List<PortBinding> randomPort = new ArrayList<>();
    final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
    randomPort.add(randomPortBinding);
    portBindings.put(runtimeConfig.getPort(), randomPort);
    final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
    final ContainerCreation creation;
    try {
        creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
        docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
    } catch (DockerException | InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
    }
    final String id = creation.id();
    String shortId = id.substring(0, 12);
    String host = "";
    try {
        docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
        host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
    } catch (DockerException | InterruptedException ex) {
        throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
    }
    DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
    dockerRuntimeEndpoint.setHost(host);
    dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
    dockerRuntimeEndpoint.setContext("");
    return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
Also used : DockerException(com.spotify.docker.client.DockerException) HashMap(java.util.HashMap) DockerProvider(org.guvnor.ala.docker.model.DockerProvider) DockerRuntimeEndpoint(org.guvnor.ala.docker.model.DockerRuntimeEndpoint) ArrayList(java.util.ArrayList) Date(java.util.Date) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) HostConfig(com.spotify.docker.client.messages.HostConfig) DockerRuntimeState(org.guvnor.ala.docker.model.DockerRuntimeState) ArrayList(java.util.ArrayList) List(java.util.List) DockerRuntimeInfo(org.guvnor.ala.docker.model.DockerRuntimeInfo)

Example 5 with PortBinding

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

the class HeliosSoloDeploymentTest method setup.

@Before
public void setup() throws Exception {
    this.dockerClient = mock(DockerClient.class);
    this.heliosClient = mock(HeliosClient.class);
    // the anonymous classes to override a method are to workaround the docker-client "messages"
    // having no mutators, fun
    final Info info = mock(Info.class);
    when(info.operatingSystem()).thenReturn("foo");
    when(this.dockerClient.info()).thenReturn(info);
    // mock the call to dockerClient.createContainer so we can test the arguments passed to it
    this.containerConfig = ArgumentCaptor.forClass(ContainerConfig.class);
    final ContainerCreation creation = mock(ContainerCreation.class);
    when(creation.id()).thenReturn(CONTAINER_ID);
    when(this.dockerClient.createContainer(this.containerConfig.capture(), anyString())).thenReturn(creation);
    // we have to mock out several other calls to get the HeliosSoloDeployment ctor
    // to return non-exceptionally. the anonymous classes to override a method are to workaround
    // the docker-client "messages" having no mutators, fun
    when(this.dockerClient.info()).thenReturn(info);
    final PortBinding binding = PortBinding.of("192.168.1.1", 5801);
    final ImmutableMap<String, List<PortBinding>> ports = ImmutableMap.<String, List<PortBinding>>of("5801/tcp", ImmutableList.of(binding));
    final ContainerInfo containerInfo = mock(ContainerInfo.class);
    final NetworkSettings networkSettings = mock(NetworkSettings.class);
    when(networkSettings.gateway()).thenReturn("a-gate-way");
    when(networkSettings.ports()).thenReturn(ports);
    when(containerInfo.networkSettings()).thenReturn(networkSettings);
    when(this.dockerClient.inspectContainer(CONTAINER_ID)).thenReturn(containerInfo);
    when(this.dockerClient.waitContainer(CONTAINER_ID)).thenReturn(ContainerExit.create(0L));
}
Also used : ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) NetworkSettings(com.spotify.docker.client.messages.NetworkSettings) DockerClient(com.spotify.docker.client.DockerClient) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) ImmutableList(com.spotify.docker.client.shaded.com.google.common.collect.ImmutableList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) HeliosClient(com.spotify.helios.client.HeliosClient) Info(com.spotify.docker.client.messages.Info) ImageInfo(com.spotify.docker.client.messages.ImageInfo) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) Before(org.junit.Before)

Aggregations

PortBinding (com.spotify.docker.client.messages.PortBinding)9 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)8 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)8 List (java.util.List)7 HashMap (java.util.HashMap)6 HostConfig (com.spotify.docker.client.messages.HostConfig)5 ArrayList (java.util.ArrayList)4 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)3 DockerClient (com.spotify.docker.client.DockerClient)2 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)2 DockerException (com.spotify.docker.client.exceptions.DockerException)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Map (java.util.Map)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 NotNull (com.microsoft.azuretools.azurecommons.helpers.NotNull)1 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 DockerException (com.spotify.docker.client.DockerException)1 LogStream (com.spotify.docker.client.LogStream)1