/** * Convert the port mapping to the format expected by the API * * <pre> * "8282/tcp": [ * { * "HostIp": "0.0.0.0", * "HostPort": "8282" * } * ] * </pre> * * @return */ public Map<String, List<Map<String, String>>> toMap() { Map<String, String> portDetails = new HashMap<>(); if (hostIp != null) { portDetails.put(HOST_IP, hostIp); } if (hostPort != null) { portDetails.put(HOST_PORT, hostPort); } List<Map<String, String>> list = new ArrayList<>(1); list.add(portDetails); Map<String, List<Map<String, String>>> portMap = new HashMap<>(); portMap.put(getContainerPortAndProtocol(), list); return portMap; }
/** * Map port binding to ExposedPorts and PortBinding * * ExposedPorts are only used by the API adapter, as the docker CLI will add that itself * * @param input * @param portBindings */ private void addPortBindings(CommandInput input, List<PortBinding> portBindings) { Map<String, Map<String, String>> exposedPortsMap = new HashMap<>(); input.withProperty(DOCKER_CONTAINER_EXPOSED_PORTS_PROP_NAME, exposedPortsMap); Map<String, Object> hostConfig = getOrAddMap(input, DOCKER_CONTAINER_HOST_CONFIG_PROP_NAME); Map<String, List<Map<String, String>>> portBindingsMap = new HashMap<>(); hostConfig.put(DOCKER_CONTAINER_PORT_BINDINGS_PROP_NAME, portBindingsMap); for (PortBinding portBinding : portBindings) { DockerPortMapping mapping = DockerPortMapping.fromString(portBinding.toString()); Map<String, List<Map<String, String>>> portDetails = mapping.toMap(); portBindingsMap.putAll(portDetails); exposedPortsMap.put(mapping.getContainerPortAndProtocol(), Collections.emptyMap()); } }