DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Keep Your Application Secrets Secret

Trending

  • Scalable System Design: Core Concepts for Building Reliable Software
  • Google Cloud Document AI Basics
  • Emerging Data Architectures: The Future of Data Management
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Running Docker Containers on Cloud Foundry

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.

By 
Yves Debeer user avatar
Yves Debeer
·
May. 19, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.7K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Docker (software) Kubernetes Cloud Foundry Cloud shell app application IBM Cloud

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

Opinions expressed by DZone contributors are their own.

Related

  • Deploy an ASP.NET Core Application in the IBM Cloud Code Engine
  • Auto-Scaling a Spring Boot Native App With Nomad
  • Containerize Gradle Apps and Deploy to Kubernetes With JKube Kubernetes Gradle Plugin
  • Keep Your Application Secrets Secret

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!