Run a Docker image as a Cloud Foundry App on IBM Cloud
Tags: ibmcloud, cloudfoundry, ibmdeveloper, docker
Published: 2020-05-13 07:45:21

---
title: Run a Docker image as a Cloud Foundry App on IBM Cloud
published: true
description: In that blog post I want to point out an awesome topic: “Run a Docker container image as a Cloud Foundry App on IBM Cloud”
tags: #IBMCloud, #CloudFoundry, #IBMDeveloper, #Docker
---

In that blog post I want to point out an awesome topic: “Run a Docker container image as a Cloud Foundry App on IBM Cloud”

Rainer Hochecker, Simon Moser and I had an interesting exchange about running a Docker image as a Cloud Foundry App on IBM Cloud.

> The advantage with that approach is: you don’t need to instantiate a Kubernetes or OpenShift cluster. You can just run a single Docker image with your single application on IBM Cloud. That can be useful in different situations where you need to control the contents of your application, and the cloud foundry build-pack mechanism maybe restricts you.

IBM offers to run [Cloud Foundry Apps on IBM Cloud](https://www.ibm.com/cloud/cloud-foundry) and supports a set of [build packs](https://cloud.ibm.com/docs/cloud-foundry?topic=cloud-foundry-available_buildpacks).  But, by the fact IBM uses [Cloud Foundry](https://www.cloudfoundry.org), you can also upload a Docker image as a Cloud Foundry application, it’s an officially supported feature. Yes there is no documentation related to that topic in the IBM Cloud documentation, but you can apply the Cloud Foundry documentation.


By the fact IBM uses , you can also try to upload a Docker image as a Cloud Foundry application, currenlty there is no restriction, but it's not offically supported. You not will find any documentation for that on IBM Cloud, but as IBM Cloud uses Cloud Foundry you can apply topics from the [Cloud Foundry documentation](https://docs.cloudfoundry.org/devguide/deploy-apps/push-docker.html). 

One impact of that situation is, you don’t see the [VCAP variables](https://suedbroecker.net/2018/09/06/can-i-use-vcap-when-i-have-a-cloud-foundry-application-and-iam-enabled-service-in-ibm-cloud/) and you can’t use the out of the box binding for IBM Cloud services. You have to manage the bindings to your IBM Cloud services by yourself.


## LET’S START WITH A SHORT GUIDE: HOW TO SETUP A CLOUD FOUNDRY APPLICATION USING A DOCKER IMAGE

When you follow the steps, you need to replace the name for the domain, port or hostname with your own settings. We will use in that example the [IBM Cloud Shell(https://cloud.ibm.com/docs/cloud-shell?topic=cloud-shell-getting-started)] on IBM Cloud and [Node-RED](https://nodered.org/docs/getting-started/docker) as our Docker image.

### The following images shows a simplified overview.

1. We push the Docker image and run it as a container in a Cloud Foundry app
2. We define a route with a port mapping to access the application from the internet.

![](https://suedbroecker.files.wordpress.com/2020/05/simplified-overview.png)

---

### STEP 1: LOGON TO IBM CLOUD AND OPEN THE IBM CLOUD SHELL.

The gif shows how to access the IBM Cloud Shell from the IBM Cloud UI.

![](https://suedbroecker.files.wordpress.com/2020/05/open-ibmcloud-shell.gif)


### STEP 2: CHANGE TO THE IBM CLOUD UI TO CREATE A CLOUD FOUNDRY SPACE IN YOUR REGION, IF YOU DON’T HAVE ONE.

The gif shows how to create a new space **dev**, in the existing organization **thomas.suedbroecker** , in the region **Germany**.

![](https://suedbroecker.files.wordpress.com/2020/05/create-cloud-foundry-space.gif)

---

### STEP 3: GO BACK TO THE IBM CLOUD SHELL AND SET THE [CLOUD FOUNDRY ENDPOINT](https://cloud.ibm.com/docs/cloud-foundry-public?topic=cloud-foundry-public-endpoints), ORGANIZATION AND SPACE. I MY CASE I USED FOLLOWING VALUES.

* Endpoint: `api.eu-de.cf.cloud.ibm.com`
* Organization: `thomas.suedbroecker`
* Space: `dev`
* Resource group: `Default`

```sh
ibmcloud target --cf-api api.eu-de.cf.cloud.ibm.com -o thomas.suedbroecker -s dev -g Default
```

---

### STEP 4: VERIFY YOUR CLOUD FOUNDRY SETTINGS ON IBM CLOUD (IBM CLOUD DOCUMENTATION)

```sh
ibmcloud target
```

---

### STEP 5: PUSH A DOCKER IMAGE FROM A CONTAINER REGISTRY.

* Name: `node-red`
* Image: `node-red-docker:v10`

In the IBM Cloud Shell you need to install the Cloud Foundry API with `ibmcloud cf install` and verify the API version `ibmcloud cf -v`.

```sh
ibmcloud cf push node-red --docker-image=docker.io/nodered/node-red-docker:v10  --no-start --no-route
```

Note: Of course you can also use a private container image registry like the [IBM Cloud Image Container Registry](https://cloud.ibm.com/docs/Registry?topic=registry-registry_overview) , if you want.

In this case, you’d need understand the [Cloud Foundry documentation](https://docs.cloudfoundry.org/devguide/deploy-apps/push-docker.html):

```sh
CF_DOCKER_PASSWORD=YOUR-PASSWORD cf push APP-NAME --docker-image REPO/IMAGE:TAG --docker-username USER
```

The [IBM Cloud Image Container Registry](https://cloud.ibm.com/docs/Registry?topic=registry-registry_overview) also contains the [documentation how to do this for Cloud Foundry](https://cloud.ibm.com/docs/Registry?topic=registry-registry_access#registry_access_apikey_auth_other_example_cf), here is the command.

```sh
export CF_DOCKER_PASSWORD=<apikey>
ibmcloud cf push appname -o <region>.icr.io/<namespace>/<image_repo> --docker-username iamapikey
```

---

### STEP 6: GET THE GUID FROM THE CLOUD FOUNDRY APP INSTANCE. `A_GUID=APPLICATION GUID`

```sh
A_GUID=$(ibmcloud cf app node-red --guid|awk '/[0-9]/{print $1}')
echo $A_GUID
```

---

### STEP 7:  SET THE PORT TO ACCESS THE APPLICATION INSIDE THE DOCKER CONTAINER OF THE CLOUD FOUNDRY APPLICATION. 

The port information you get from the Docker image description.

* PORT: `1880`

```sh
ibmcloud cf curl /v2/apps/$A_GUID -X PUT -d '{"ports": [1880]}'
```

---

### STEP 8: IN THIS STEP WE CREATE A ROUTE TO THE CLOUD FOUNDRY APP, TO ACCESS LATER THE RUNNING APPLICATION FROM THE INTERNET.

The Domain name eu-de.mybluemix.net depends on the region you create the Cloud Foundry App. In that sample we using eu-de. ([see IBM Cloud documentation](https://cloud.ibm.com/docs/overview?topic=overview-locations&locale=en) )

* Domain: `eu-de.mybluemix.net`
* Hostname: `node-red-tsuedbroecker`

```sh
ibmcloud cf create-route dev eu-de.mybluemix.net --hostname node-red-tsuedbroecker
```
---

### STEP 9: WE NEED TO MAP THE ROUTE TO THE CLOUD FOUNDRY APP , TO ACCESS THE RUNNING APPLICATION FROM THE INTERNET.

* Application name: `node-red`
* Domain: `eu-de.mybluemix.net`
* Hostname: `node-red-tsuedbroecker`

```sh
ibmcloud cf map-route node-red eu-de.mybluemix.net --hostname node-red-tsuedbroecker
```

10. Now we extract the GUID for the created route. (`R_GUID=route GUID`)

```sh
R_GUID=$(ibmcloud cf curl "/v2/routes?q=host:node-red-tsuedbroecker" | sed -n 's|.*"guid": "\([^"]*\)".*|\1|p')
echo $R_GUID
```

---

### STEP 10: NOW WE EXTRACT THE GUID FOR THE NEWLY CREATED ROUTE. (`R_GUID=ROUTE GUID`)

```sh
ibmcloud cf curl /v2/route_mappings -X POST -d '{"app_guid": "'"$A_GUID"'", "route_guid": "'"$R_GUID"'", "app_port": 1880}'
```

---

### STEP 11: WE NEED TO UPDATE THE ROUTE MAPPINGS WITH OUR GUID’S. (`R_GUID` AND `A_GUID`)

```sh
ibmcloud cf curl /v2/route_mappings -X POST -d '{"app_guid": "'"$A_GUID"'", "route_guid": "'"$R_GUID"'", "app_port": 1880}'
```

---

### STEP 12: START THE CLOUD FOUNDRY APPLICATION.

```sh
ibmcloud cf start node-red
```

---

### STEP 13: VISIT THE CLOUD FOUNDRY APP IN THE IBM CLOUD UI AND INSPECT THE POSSIBILITIES.

In the gif you see, there is no build pack information and there are no environment variables.

![](https://suedbroecker.files.wordpress.com/2020/05/open-cloud-foundry-app.gif)

---

### STEP 14: NOW OPEN THE APPLICATION URL AND USE THE RUNNING NODE-RED INSTANCE.

The gif shows, how to access the application URL inside the Cloud Foundry App.

![](https://suedbroecker.files.wordpress.com/2020/05/visit-cloud-foundry-application.gif)

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas