JsonObject toDockerPortBindingsJson ( ) { Map < String , Integer > portMap = getContainerPortToHostPortMap ( ) ; if ( ! portMap . isEmpty ( ) ) { JsonObject portBindings = new JsonObject ( ) ; Map < String , String > bindToMap = getBindToHostMap ( ) ; for ( Map . Entry < String , Integer > entry : portMap . entrySet ( ) ) { String containerPortSpec = entry . getKey ( ) ; Integer hostPort = entry . getValue ( ) ; JsonObject o = new JsonObject ( ) ; o . addProperty ( "HostPort" , hostPort != null ? hostPort . toString ( ) : "" ) ; if ( bindToMap . containsKey ( containerPortSpec ) ) { o . addProperty ( "HostIp" , bindToMap . get ( containerPortSpec ) ) ; } JsonArray array = new JsonArray ( ) ; array . add ( o ) ; portBindings . add ( containerPortSpec , array ) ; } return portBindings ; } else { return null ; } }
Create a JSON specification which can be used to in a Docker API request as the PortBindings part for creating container .