本文整理汇总了Java中com.github.dockerjava.api.model.Ports.Binding方法的典型用法代码示例。如果您正苦于以下问题:Java Ports.Binding方法的具体用法?Java Ports.Binding怎么用?Java Ports.Binding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.github.dockerjava.api.model.Ports
的用法示例。
在下文中一共展示了Ports.Binding方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBindingForPort
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
private InetSocketAddress getBindingForPort(DockerAPI api, InspectContainerResponse ir, int internalPort) {
// get exposed port
ExposedPort sshPort = new ExposedPort(internalPort);
Integer port = 22;
final NetworkSettings networkSettings = ir.getNetworkSettings();
final Ports ports = networkSettings.getPorts();
final Map<ExposedPort, Ports.Binding[]> bindings = ports.getBindings();
// Get the binding that goes to the port that we're interested in (e.g: 22)
final Ports.Binding[] sshBindings = bindings.get(sshPort);
// Find where it's mapped to
for (Ports.Binding b : sshBindings) {
String hps = b.getHostPortSpec();
port = Integer.valueOf(hps);
}
String host = getExternalIP(api, ir, networkSettings, sshBindings);
return new InetSocketAddress(host, port);
}
示例2: testPortBinding
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
@Test
public void testPortBinding() throws IOException, InterruptedException {
DockerComputerSSHConnector connector = new DockerComputerSSHConnector(Mockito.mock(DockerComputerSSHConnector.SSHKeyStrategy.class));
CreateContainerCmdImpl cmd = new CreateContainerCmdImpl(Mockito.mock(CreateContainerCmd.Exec.class), "");
cmd.withPortBindings(PortBinding.parse("42:42"));
connector.setPort(22);
connector.beforeContainerCreated(Mockito.mock(DockerAPI.class), "/workdir", cmd);
final Ports portBindings = cmd.getPortBindings();
Assert.assertNotNull(portBindings);
final Map<ExposedPort, Ports.Binding[]> bindingMap = portBindings.getBindings();
Assert.assertNotNull(bindingMap);
Assert.assertEquals(2, bindingMap.size());
final Ports.Binding[] configuredBindings = bindingMap.get(new ExposedPort(42));
Assert.assertNotNull(configuredBindings);
Assert.assertEquals(1, configuredBindings.length);
Assert.assertEquals("42", configuredBindings[0].getHostPortSpec());
final Ports.Binding[] sshBindings = bindingMap.get(new ExposedPort(22));
Assert.assertNotNull(sshBindings);
Assert.assertEquals(1, sshBindings.length);
Assert.assertNull(sshBindings[0].getHostPortSpec());
System.out.println();
}
示例3: shouldStartContainerWithOnePort
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
@Test
@DisplayName("start a container with one port")
public void shouldStartContainerWithOnePort() {
String containerId = defaultDockerClient.startContainer(WANTED_IMAGE, emptyMap(),
new PortBinding(8081, 8080));
InspectContainerResponse startedContainer = dockerClient.inspectContainerCmd(containerId).exec();
Ports ports = startedContainer.getHostConfig().getPortBindings();
assertThat(ports).isNotNull();
Map<ExposedPort, Ports.Binding[]> portBindings = ports.getBindings();
assertThat(portBindings).hasSize(1)
.containsKeys(new ExposedPort(8080));
assertThat(portBindings.get(new ExposedPort(8080))).hasSize(1)
.extracting(Ports.Binding::getHostPortSpec)
.contains("8081");
}
示例4: fillPortMapping
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
public void fillPortMapping(InspectContainerResponse containerInspectResponse) {
Map<Integer, Integer> portMapping = new HashMap<Integer, Integer>();
if (containerInspectResponse.getNetworkSettings() != null && containerInspectResponse.getNetworkSettings().getPorts() != null) {
Ports ports = containerInspectResponse.getNetworkSettings().getPorts();
for (ExposedPort port : ports.getBindings().keySet()) {
Ports.Binding binding = ports.getBindings().get(port);
portMapping.put(port.getPort(), binding.getHostPort());
}
}
this.setPortMapping(portMapping);
}
示例5: getExternalIP
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
private String getExternalIP(DockerAPI api, InspectContainerResponse ir, NetworkSettings networkSettings,
Ports.Binding[] sshBindings) {
// If an explicit IP/hostname has been defined, always prefer this one
String dockerHostname = api.getHostname();
if (dockerHostname != null && !dockerHostname.trim().isEmpty()) {
return dockerHostname;
}
// for (standalone) swarm, need to get the address of the actual host
if (api.isSwarm()) {
for (Ports.Binding b : sshBindings) {
String ipAddress = b.getHostIp();
if (ipAddress != null && !"0.0.0.0".equals(ipAddress)) {
return ipAddress;
}
}
}
// see https://github.com/joyent/sdc-docker/issues/132
final String driver = ir.getExecDriver();
if (driver != null && driver.startsWith("sdc")) {
// We run on Joyent's Triton
// see https://docs.joyent.com/public-cloud/instances/docker/how/inspect-containers
return networkSettings.getIpAddress();
}
final URI uri = URI.create(api.getDockerHost().getUri());
if(uri.getScheme().equals("unix")) {
// Communicating with unix domain socket. so we assume localhost
return "0.0.0.0";
} else {
return uri.getHost();
}
}
示例6: createPortBinding
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
private PortBinding createPortBinding(final int hostPort, final int containerPort) {
final Ports.Binding binding = new Ports.Binding("0.0.0.0", String.valueOf(hostPort));
final ExposedPort exposedPort = new ExposedPort(containerPort);
return new PortBinding(binding, exposedPort);
}
示例7: startContainer
import com.github.dockerjava.api.model.Ports; //导入方法依赖的package包/类
@Override
public Container startContainer(Environment env, Container container) throws ContainerizationException {
DockerContainer dockerContainer = (DockerContainer) container;
try {
ImageConfiguration imageConf = container.getImage().getConf();
Ports portBindings = new Ports();
if (imageConf.getPorts() != null) {
for (PortConfiguration port : imageConf.getPorts()) {
ExposedPort exposedPort = new ExposedPort("tcp", port.getContainerPort());
Ports.Binding binding = new Ports.Binding(port.getHostPort());
portBindings.bind(exposedPort, binding);
}
}
/*if (dockerContainer.getVolumes() != null) {
for (ServiceConfiguration.VolumeInfo volumeInfo : dockerContainer.getVolumeInfos()) {
if (volumeInfo.getHostDir() != null) {
binds.add(volumeInfo.getHostDir() + ":" + volumeInfo.getId() + ":rw");
} else {
binds.add(volumeInfo.getId());
}
}
}*/
List<Link> links = new ArrayList<Link>();
for (LinkConfiguration link : container.getImage().getConf().getLinks()) {
String serviceNameInCurrentEnv = getNamingStrategy().generateContainerName(env, link.getService());
String alias = link.getAlias();
if (alias == null) {
alias = link.getService();
}
links.add(new Link(serviceNameInCurrentEnv, alias));
}
getDockerClient().startContainerCmd(dockerContainer.getId())
.withLinks(links.toArray(new Link[0]))
//.withBinds()
.withPortBindings(portBindings)
.withPrivileged(imageConf.isPrivileged())
.exec();
InspectContainerResponse containerInspectResponse = getDockerClient().inspectContainerCmd(dockerContainer.getId()).exec();
dockerContainer.setRunning(containerInspectResponse.getState().isRunning());
dockerContainer.fillPortMapping(containerInspectResponse);
fillVariables(env, imageConf, dockerContainer);
return dockerContainer;
} catch(Exception ex) {
throw new ContainerizationException(ex);
}
}