本文整理汇总了Java中com.github.dockerjava.api.model.PortBinding类的典型用法代码示例。如果您正苦于以下问题:Java PortBinding类的具体用法?Java PortBinding怎么用?Java PortBinding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PortBinding类属于com.github.dockerjava.api.model包,在下文中一共展示了PortBinding类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startDockerNoVnc
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
private int startDockerNoVnc() {
String novncImage = getString("sel.jup.novnc.image");
dockerService.pullImageIfNecessary(novncImage);
int novncPort = dockerService.findRandomOpenPort();
Binding novncBindPort = bindPort(novncPort);
ExposedPort novncExposedPort = tcp(getInt("sel.jup.novnc.port"));
List<PortBinding> portBindings = asList(
new PortBinding(novncBindPort, novncExposedPort));
String novncContainerName = dockerService
.generateContainerName("novnc");
DockerContainer novncContainer = DockerContainer
.dockerBuilder(novncImage, novncContainerName)
.portBindings(portBindings).build();
dockerService.startAndWaitContainer(novncContainer);
containers.put(novncContainerName, novncImage);
return novncPort;
}
示例2: testWithAContainerWithAPort
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
@Test
public void testWithAContainerWithAPort() throws InterruptedException {
CreateContainerResponse container = client.createContainerCmd("nginx")
.withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443))
.withPortBindings(PortBinding.parse("80"))
.exec();
AtomicBoolean done = new AtomicBoolean();
Future<Void> future = Future.future();
future.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(0);
done.set(false);
client.startContainerCmd(container.getId()).exec();
Future<Void> future2 = Future.future();
future2.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future2);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(1);
DockerService service = bridge.getServices().get(0);
assertThat(service.records()).hasSize(1);
}
示例3: getPortMappings
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
public Iterable<PortBinding> getPortMappings() {
if (Strings.isNullOrEmpty(bindPorts)) {
return Collections.emptyList();
}
return Iterables.transform(Splitter.on(' ')
.trimResults()
.omitEmptyStrings()
.split(bindPorts),
new Function<String, PortBinding>() {
@Nullable
@Override
public PortBinding apply(String s) {
return PortBinding.parse(s);
}
});
}
示例4: testPortBinding
import com.github.dockerjava.api.model.PortBinding; //导入依赖的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();
}
示例5: portDecorator
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
private CreateContainerCmd portDecorator(CreateContainerCmd createContainerCmd) {
Value value = instance.getDictionary().findValuesByID("port");
if (value != null) {
String ports = value.getValue();
if (ports != null && !ports.isEmpty()) {
PortBinding[] portBindings = Arrays
.stream(ports.trim().split("\\s"))
.map(PortBinding::parse)
.toArray(PortBinding[]::new);
for (PortBinding binding : portBindings) {
System.out.println("binding>>> " + binding.getBinding().toString() + ":" + binding.getExposedPort().toString());
}
return createContainerCmd.withPortBindings(portBindings);
}
}
return createContainerCmd;
}
示例6: buildPortBindings
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
private java.util.List<PortBinding> buildPortBindings(final InfrastructureComponent component, final EnvironmentContext ctx, final DockerComponent dockerC) {
return dockerC.ports().foldRight(List.<PortBinding>empty(), (namePort, list) -> {
final Integer highPort = Utils.getAvailablePort().get();
ctx.addPort(component.name(), namePort._1, highPort, namePort._2);
return list.append(createPortBinding(highPort, namePort._2));
}).toJavaList();
}
示例7: testWithAContainerWithTwoPorts
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
@Test
public void testWithAContainerWithTwoPorts() throws InterruptedException {
CreateContainerResponse container = client.createContainerCmd("nginx")
.withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443))
.withPortBindings(PortBinding.parse("80"), PortBinding.parse("443"))
.exec();
AtomicBoolean done = new AtomicBoolean();
Future<Void> future = Future.future();
future.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(0);
done.set(false);
client.startContainerCmd(container.getId()).exec();
Future<Void> future2 = Future.future();
future2.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future2);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(1);
DockerService service = bridge.getServices().get(0);
assertThat(service.records()).hasSize(2);
client.stopContainerCmd(container.getId()).exec();
done.set(false);
Future<Void> future3 = Future.future();
future3.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future3);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(0);
}
示例8: testWithAContainerWithLabels
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
@Test
public void testWithAContainerWithLabels() throws InterruptedException {
Map<String, String> labels = new LinkedHashMap<>();
labels.put("service.type", "http-endpoint");
labels.put("ssl", "true");
CreateContainerResponse container = client.createContainerCmd("nginx")
.withExposedPorts(ExposedPort.tcp(80), ExposedPort.tcp(443))
.withPortBindings(PortBinding.parse("80"))
.withLabels(labels)
.exec();
AtomicBoolean done = new AtomicBoolean();
Future<Void> future = Future.future();
future.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(0);
done.set(false);
client.startContainerCmd(container.getId()).exec();
Future<Void> future2 = Future.future();
future2.setHandler(ar -> done.set(ar.succeeded()));
bridge.scan(future2);
await().untilAtomic(done, is(true));
assertThat(bridge.getServices()).hasSize(1);
DockerService service = bridge.getServices().get(0);
assertThat(service.records()).hasSize(1);
assertThat(service.records().get(0).getLocation().getString("endpoint")).startsWith("https");
}
示例9: parseOnePortBinding
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
private static PortBinding parseOnePortBinding(String definition) throws IllegalArgumentException {
try {
return PortBinding.parse(definition.replace(' ', ':'));
} catch (Exception e) {
throw new IllegalArgumentException("Port binding needs to be in format '[hostIP:]hostPort containerPort[/protocol]'");
}
}
示例10: assertCreatesBindings
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
private static void assertCreatesBindings(String input, PortBinding... expected) {
PortBinding[] parsed = PortBindingParser.parse(input);
assertEquals("wrong number of PortBindings created", expected.length, parsed.length);
for (int i = 0; i < parsed.length; i++) {
assertEquals(expected[i], parsed[i]);
}
}
示例11: beforeContainerCreated
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
@Override
public void beforeContainerCreated(DockerAPI api, String workdir, CreateContainerCmd cmd) throws IOException, InterruptedException {
// TODO define a strategy for SSHD process configuration so we support more than openssh's sshd
if (cmd.getCmd() == null || cmd.getCmd().length == 0) {
if (sshKeyStrategy.getInjectedKey() != null) {
cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port),
// override sshd_config to force retrieval of InstanceIdentity public for as authentication
"-o", "AuthorizedKeysCommand=/root/authorized_key",
"-o", "AuthorizedKeysCommandUser=root"
);
} else {
cmd.withCmd("/usr/sbin/sshd", "-D", "-p", String.valueOf(port));
}
}
cmd.withPortSpecs(port+"/tcp");
final PortBinding sshPortBinding = PortBinding.parse(":" + port);
final Ports portBindings = cmd.getPortBindings();
if(portBindings != null) {
portBindings.add(sshPortBinding);
cmd.withPortBindings(portBindings);
} else {
cmd.withPortBindings(sshPortBinding);
}
cmd.withExposedPorts(ExposedPort.parse(port+"/tcp"));
}
示例12: startAndWaitContainer
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
public void startAndWaitContainer(DockerContainer dockerContainer) {
String containerName = dockerContainer.getContainerName();
String imageId = dockerContainer.getImageId();
log.debug("Starting Docker container {}", imageId);
if (!isRunningContainer(containerName)) {
pullImageIfNecessary(imageId);
try (CreateContainerCmd createContainer = dockerClient
.createContainerCmd(imageId).withName(containerName)) {
Optional<String> network = dockerContainer.getNetwork();
if (network.isPresent()) {
log.trace("Using network: {}", network.get());
createContainer.withNetworkMode(network.get());
}
Optional<List<PortBinding>> portBindings = dockerContainer
.getPortBindings();
if (portBindings.isPresent()) {
log.trace("Using port binding: {}", portBindings.get());
createContainer.withPortBindings(portBindings.get());
}
Optional<List<Volume>> volumes = dockerContainer.getVolumes();
if (volumes.isPresent()) {
log.trace("Using volumes: {}", volumes.get());
createContainer.withVolumes(volumes.get());
}
Optional<List<Bind>> binds = dockerContainer.getBinds();
if (binds.isPresent()) {
log.trace("Using binds: {}", binds.get());
createContainer.withBinds(binds.get());
}
Optional<List<String>> envs = dockerContainer.getEnvs();
if (envs.isPresent()) {
log.trace("Using envs: {}", envs.get());
createContainer.withEnv(envs.get());
}
Optional<List<String>> cmd = dockerContainer.getCmd();
if (cmd.isPresent()) {
log.trace("Using cmd: {}", cmd.get());
createContainer.withCmd(cmd.get());
}
createContainer.exec();
dockerClient.startContainerCmd(containerName).exec();
waitForContainer(containerName);
}
} else {
log.warn("Container {} already running", containerName);
}
}
示例13: getPortBindings
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
public Optional<List<PortBinding>> getPortBindings() {
return portBindings;
}
示例14: createPortBinding
import com.github.dockerjava.api.model.PortBinding; //导入依赖的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);
}
示例15: fullDefinition_blank
import com.github.dockerjava.api.model.PortBinding; //导入依赖的package包/类
@Test
public void fullDefinition_blank() {
assertCreatesBindings("127.0.0.1:80 8080/tcp",
new PortBinding(Ports.Binding.bindIpAndPort("127.0.0.1", 80), TCP_8080));
}