{{announcement.body}}
{{announcement.title}}

Running Docker Containers on Cloud Foundry

DZone 's Guide to

Running Docker Containers on Cloud Foundry

This article describes how to run Docker containers in Cloud Foundry without having to worry about setting up a Kubernetes cluster.

· Cloud Zone ·
Free Resource

If you are an experienced developer already familiar with Docker, here's a quick way to just deploy your containers into the cloud without having to worry about setting up and managing a Kubernetes cluster. And also important...it comes for free using Cloud Foundry.

Let's start by creating a simple NodeJS application locally using 'npm init', give your app a name e.g. 'tinyapp' and use 'server.js' as the entry point. 

This will generate a 'package.json' file:

JSON
 




x


 
1
    {
2
      "name": "tinyapp",
3
      "version": "1.0.0",
4
      "description": "",
5
      "main": "server.js",
6
      "scripts": {
7
        "test": "echo \"Error: no test specified\" && exit 1"
8
      },
9
      "author": "",
10
      "license": "ISC"
11
    }



Next install the NodeJS 'Express-framework' using the command: 

Shell
 




xxxxxxxxxx
1


 
1
# npm install express --save 



Create a NodeJS application file 'server.js'

JavaScript
 




xxxxxxxxxx
1
11
9


1
const express = require('express');
2
const app = express();
3
const port = process.env.PORT || 3000;
4
   
5
 app.get('/', (req,res) => res.send('Hello there !!!'));
6
 app.listen(port, () => console.log(`Example app listening on port ${port}!`));



Test the NodeJS application locally using the command:

Shell
 




xxxxxxxxxx
1


 
1
 # node server.js



This will launch the app locally to listen on port 3000. You can verify the working of the app with a  browser http://localhost:3000.

Now we can already deploy this app onto Cloud Foundry with the standard NodeJS runtime buildpack using the command: 

Shell
 




xxxxxxxxxx
1


 
1
# cf push tinynodejs 



Once the deployment is done we can test the app using the given URL with Curl or with a Web Browser.

Of course you will need an IBM CLoud account for this. 

If you don't have an account yet, you can register for a free Lite account here: https://ibm.biz/freeaccount.

More details on using the IBM Cloud CLI can be found here: https://cloud.ibm.com/docs?tab=develop

You might get an error during deployment as Cloud Foundry will try to deploy your app using 1Gb of memory by default and with a lite account you're limited to 256M. In order to resolve this you can create a manifest.yaml file from your failed deployed application using the command :

Shell
 




xxxxxxxxxx
1


 
1
# cf create-app-manifest tinynodejs -p manifest.yaml 



Edit the local manifest.yaml file and change the memory to e.g. 128M and do a 'cf push' again.

This is the most common way to deploy an applcation using a Cloud Foundry runtime.

Let's now 'Dockerize' our application:

First we need to create a 'Dockerfile' with following content:

Dockerfile
 




xxxxxxxxxx
1


 
1
FROM node
2
WORKDIR /app
3
COPY package.json /app
4
RUN npm install
5
COPY . /app
6
CMD node server.js
7
EXPOSE 8080



Perform a Docker build:

Shell
 




xxxxxxxxxx
1


1
# docker build . -t tinyapp



Verify the Docker image was stored successfully:

Shell
 




xxxxxxxxxx
1


1
# docker images
2
REPOSITORY     TAG           IMAGE ID            CREATED             SIZE
3
tinyapp        latest        e30aeb035b14        59 seconds ago      948MB



Run the container locally:

Shell
 




xxxxxxxxxx
1


 
1
# docker run -p 8080:3000 -d tinyapp



Verify the container is running:

Shell
 




x


1
# docker ps
2
CONTAINER ID IMAGE   COMMAND                CREATED       STATUS        PORTS       
3
1e8571529040 tinyapp "docker-entrypoint.s…" 7 minutes ago Up 7 minutes  8080/tcp, 0.0.0.0:8080->3000/tcp   hungry_montalcini



Verify access to the application using http://localhost:8080 as the initial port 3000 is now mapped to port 8080 on Docker.

You can also execute a shell command into the container using the 'CONTAINER ID': 

Shell
 




xxxxxxxxxx
1


 
1
# docker exec -it 1e8571529040 /bin/bash



Next we can deploy this docker image to the cloud using Cloud Foundry 'cf push' command but before we do that we will first clean up the existing app:

Shell
 




xxxxxxxxxx
1


 
1
    # cf delete tinynodejs



Caution here because deleting the app does not delete the application route ! 

You can check the Cloud Foundry routes with:

Shell
 




xxxxxxxxxx
1


 
1
    # cf routes



So let's also delete the existing route: 

Shell
 




xxxxxxxxxx
1


 
1
# cf delete-route eu-de.mybluemix.net --hostname tinynodejs



And now let's deploy the Docker container:

First we need to upload our Docker image to a central container repository e.g. https://hub.docker.com

  • Login to Dockerhub:
Shell
 




x


1
# docker login --username=<your username>



  • Tag local image using the 'IMAGE ID'
Shell
 




xxxxxxxxxx
1


 
1
# docker tag e30aeb035b14 ydebeer/tinynodejs:latest



  • Push the image to your Dockerhub repository:
Shell
 




xxxxxxxxxx
1


 
1
# docker push ydebeer/tinynodejs:latest



Now we can push the image to run on the IBM Cloud using CLoud Foundry:

Shell
 




xxxxxxxxxx
1


 
1
# cf push tinynodejs -o ydebeer/tinynodejs:latest



And there you have it: a NodeJS app running as a Docker container in the IBM Cloud.

Topics:
cloud ,cloud foundry ,cloud foundry container runtime ,docker ,ibm cloud ,nodejs ,tutorial

Published at DZone with permission of Yves Debeer . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}