CloudFoundry application opening two ports

Currently an application on Cloud Foundry is not able to have two ports mapped into its container environment. As part of the new Diego runtime, multiple port mappings has been exposed, but is not currently available through the API.

Depending on what you need, you could take a look at Lattice, which uses the Diego runtime. Some documentation can be found here.


As stated in some other comments it is now possible in CF to use multiple ports for your application. There is a chapter in the CF documentation which describes how to do it. I followed the instructions and still had some trouble to fully understand it, that's why I provide a step by step guide here with some explanations (replace all variables in [] with the actual values):

  1. Configure your application to listen on multiple ports. In my case I configured a spring boot app to listen on port 8080 for HTTPS requests and on port 8081 for HTTP requests (used for calls to actuator endpoints like health/prometheus as described here). This means that I have configured one TCP route and one HTTP route in CF and mapped those routes to the CF app.
  2. Get the [APP_GUID] of the CF app which should be reachable on multiple ports: cf app [APP_NAME] --guid
  3. Add the ports (e.g. 8080, 8081) to the CF app: cf curl /v2/apps/[APP_GUID] -X PUT -d '{"ports": [8080, 8081]}'
  4. Now the route (e.g. in this case the HTTP route) which points to the CF app must also be adjusted so that it points to the correct CF app port. First you need to get the route information, you can do it with cf curl /v2/routes?q=host:[HOST_NAME] or with cf curl /v2/apps/[APP_GUID]/routes and save the guid of the route that points to your app ([ROUTE_GUID]).
  5. For this particular route you have to adjust the route mappings. Each CF route can have multiple route mappings. You can show current route mappings for a route with this command: cf curl /v2/routes/[ROUTE_GUID]/route_mappings. With cf curl /v2/route_mappings -X POST -d '{"app_guid": "[APP_GUID]", "route_guid": "[ROUTE_GUID]", "app_port": 8081}' you add a mapping to a route (e.g. here to 8081).
  6. The route has now two mappings, one pointing to 8080 and one pointing to 8081. If you want the route to only point to one of the ports (e.g. 8081) you have to delete the mapping with the port you do not want to have. Run cf curl /v2/routes/[ROUTE_GUID]/route_mappings to show all route mappings. Then extract the guid of the route mapping that should be deleted (e.g. the one to port 8080). Finally, run cf curl /v2/route_mappings/[GUID_ROUTE_MAPPING] -X DELETE to delete the route mapping you do not need.

Now your CF app should be reachable on another port than 8080 when the newly configured route is used.


Cloud Foundry will route TCP/WebSocket traffic coming from 80/443 to the one assigned port. Your application can not listen to any other port.

https://docs.cloudfoundry.org/devguide/deploy-apps/prepare-to-deploy.html#ports

You can either create multiple url mappings, or have two applications that communicate with each other using a messaging or database service.