public List<ExposedPort> getExposedPorts() { List<Map<String, Object>> rawExposedPorts = (List<Map<String, Object>>) getProperty("exposed_ports"); List<ExposedPort> exposedPorts = Lists.newArrayList(); if (rawExposedPorts != null) { for (Map<String, Object> rawExposedPortsEntry : rawExposedPorts) { int port = ((Number) rawExposedPortsEntry.get("port")).intValue(); if ("udp".equals(rawExposedPortsEntry.get("protocol"))) { exposedPorts.add(ExposedPort.udp(port)); } else { exposedPorts.add(ExposedPort.tcp(port)); } } } return exposedPorts; }
@Override public CreateContainerCmd build(ServiceDescriptor sd, CreateContainerCmd cmd, PortBinding pb) { List<ExposedPort> ports = new ArrayList<>(Arrays.asList(cmd.getExposedPorts())); ExposedPort containerPort = pb.protocol() .equals(Protocol.TCP) ? ExposedPort.tcp(pb.exposedPort()) : ExposedPort.udp(pb.exposedPort()); ports.add(containerPort); Ports bindings = cmd.getPortBindings(); if (bindings == null) { bindings = new Ports(); } bindings.bind(containerPort, pb.hostIp() .isEmpty() ? Binding.bindPort(pb.hostPort()) : Binding.bindIpAndPort(pb.hostIp(), pb.hostPort())); return cmd.withExposedPorts(ports) .withPortBindings(bindings); }
@Override protected CreateContainerCmd dockerCommand() { int port = getServicePort(); ExposedPort exposedPort = ExposedPort.tcp(port); Ports portBindings = new Ports(); if (getCluster().isMapPortsToHost()) { portBindings.bind(exposedPort, Ports.Binding.bindPort(port)); } ExposedPort consulHTTPPort = ExposedPort.tcp(ConsulConfig.CONSUL_HTTP_PORT); ExposedPort consulDNSPort = ExposedPort.udp(ConsulConfig.CONSUL_DNS_PORT); return DockerClientFactory.build().createContainerCmd(config.getImageName() + ":" + config.getImageTag()) .withName(getName()) .withCmd("agent", "-server", "-bootstrap", "-client", "0.0.0.0") .withExposedPorts(consulHTTPPort, consulDNSPort) .withPortBindings(portBindings); }