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);
}
}
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()));
}
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;
}
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())));
}
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));
}
Aggregations