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.
Join the DZone community and get the full member experience.
Join For FreeIf 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:
x
{
"name": "tinyapp",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Next install the NodeJS 'Express-framework' using the command:
xxxxxxxxxx
# npm install express --save
Create a NodeJS application file 'server.js'
xxxxxxxxxx
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req,res) => res.send('Hello there !!!'));
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
Test the NodeJS application locally using the command:
xxxxxxxxxx
# 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:
xxxxxxxxxx
# 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 :
xxxxxxxxxx
# 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:
xxxxxxxxxx
FROM node
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node server.js
EXPOSE 8080
Perform a Docker build:
xxxxxxxxxx
# docker build . -t tinyapp
Verify the Docker image was stored successfully:
xxxxxxxxxx
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tinyapp latest e30aeb035b14 59 seconds ago 948MB
Run the container locally:
xxxxxxxxxx
# docker run -p 8080:3000 -d tinyapp
Verify the container is running:
x
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
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':
xxxxxxxxxx
# 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:
xxxxxxxxxx
# cf delete tinynodejs
Caution here because deleting the app does not delete the application route !
You can check the Cloud Foundry routes with:
xxxxxxxxxx
# cf routes
So let's also delete the existing route:
xxxxxxxxxx
# 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:
x
# docker login --username=<your username>
- Tag local image using the 'IMAGE ID'
xxxxxxxxxx
# docker tag e30aeb035b14 ydebeer/tinynodejs:latest
- Push the image to your Dockerhub repository:
xxxxxxxxxx
# docker push ydebeer/tinynodejs:latest
Now we can push the image to run on the IBM Cloud using CLoud Foundry:
xxxxxxxxxx
# cf push tinynodejs -o ydebeer/tinynodejs:latest
And there you have it: a NodeJS app running as a Docker container in the IBM Cloud.
Published at DZone with permission of Yves Debeer. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments