Deploy a Node.js application using OpenShift's Command Line Interface (CLI)

Truong, articlesopenshift
Back

On this tutorial you

We'll be using the "Example Health" application.

From the editor's menu bar, choose the Terminal menu and click on New Terminal.

Example Health details

Clone the application's source code

First, clone the Example Health source code and change to that directory.

git clone https://github.com/IBM/node-build-config-openshift
cd node-build-config-openshift

Outputs

theia@theiaopenshift-truong1:/home/project$ git clone https://github.com/IBM/node-build-config-openshift
Cloning into 'node-build-config-openshift'...
remote: Enumerating objects: 120, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 120 (delta 2), reused 2 (delta 2), pack-reused 115
Receiving objects: 100% (120/120), 5.44 MiB | 19.06 MiB/s, done.
Resolving deltas: 100% (26/26), done.
theia@theiaopenshift-truong1:/home/project$ cd node-build-config-openshift
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ 

Let's start by taking a look at the Dockerfile in the application's root directory.

A Dockerfile tells us how our application is being containerized, this is important in the next step as we'll be building an image to save and deploy in an OpenShift container.

To view the Dockerfile you can use the cat command below or use the embedded IDE as shown in the image below.

cat Dockerfile

Open Dockerfile

Let's go through each line and read the corresponding comments.

# Use the official Node 10 image
FROM node:10

# Change directory to /usr/src/app
WORKDIR /usr/src/app

# Copy the application source code
COPY . .

# Change directory to site/
WORKDIR site/

# Install dependencies
RUN npm install

# Allow traffic on port 8080
EXPOSE 8080

# Start the application
CMD [ "npm", "start" ]

Build a new image

Build your application's image by running the oc new-build command from your source code root directory.

This will create a Build and an ImageStream of the app.

oc new-build --strategy docker --binary --docker-image node:10 --name example-health

The output should look like below:

theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc new-build --strategy docker --binary --docker-image node:10 --name example-health
--> Found container image 28dca66 (3 months old) from Docker Hub for "node:10"

    * An image stream tag will be created as "node:10" that will track the source image
    * A Docker build using binary input will be created
      * The resulting image will be pushed to image stream tag "example-health:latest"
      * A binary build was created, use 'oc start-build --from-dir' to trigger a new build

--> Creating resources with label build=example-health ...
    imagestream.image.openshift.io "node" created
    imagestream.image.openshift.io "example-health" created
    buildconfig.build.openshift.io "example-health" created
--> Success

Start a new build using the oc start-build command.

NOTE: This command can take up to a minute to complete, please be patient!

oc start-build example-health --from-dir . --follow

The output should look like below:

theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc start-build example-health --from-dir . --follow
Uploading directory "." as binary input for the build ...

Uploading finished
build.build.openshift.io/example-health-1 started
Receiving source from STDIN as archive ...
Replaced Dockerfile FROM image node:10
Caching blobs under "/var/cache/blobs".

Pulling image node@sha256:686e0e859358f28bfe1641e1627549b9cd0ad74f222b953d74209213488c6858 ...
Getting image source signatures
Copying blob sha256:b53ce1fd2746e8d2037f1b0b91ddea0cc7411eb3e5949fe10c0320aca8f7392b
Copying blob sha256:7a803dc0b40fcd10faee3fb3ebb2d7aaa88500520e6295295f5163c4bb48548b
Copying blob sha256:b800e94e7303e276b8fb4911a40bfe28f46180d997022c69bf1ee02fb7b86721
Copying blob sha256:2e2bafe8a0f40509cc10249087268e66a662e437f10e9598a09abb5687038a57
Copying blob sha256:84a8c1bd5887cc4a89e1f286fed9ee31ce12dba9f6813cf14082ada3e9ab6f63
Copying blob sha256:76b8ef87096fa726adbe8f073ef69bb5664bac19474c5cce4dd69e08a234903b
Copying blob sha256:0da9fbf60d485c74d153bfc7562f34533550e3dd8aa78f5e8c2476ed500e0e73
Copying blob sha256:04dccde934cf0d6ab9d303fe73b36bc4ed84329d9d562049a69a5d38afb83f14
Copying blob sha256:73269890f6fdf96184d0fdb0afe3cf697b0c8dbe1e7a077f0fab445d74372c78
Copying config sha256:28dca6642db82aaecbed18101d9966f001e8de6691e1a1718c2927c124d81262
Writing manifest to image destination
Storing signatures
STEP 1: FROM node@sha256:686e0e859358f28bfe1641e1627549b9cd0ad74f222b953d74209213488c6858
STEP 2: WORKDIR /usr/src/app
--> 95cbf29232d
STEP 3: COPY . .
--> 1920e70d903
STEP 4: WORKDIR site/
--> 961c0fd43be
STEP 5: RUN npm install
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated circular-json@0.5.9: CircularJSON is in maintenance only, flatted is its successor.
npm notice created a lockfile as package-lock.json. You should commit this file.
added 114 packages from 97 contributors and audited 114 packages in 12.487s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

--> 279f34194eb
STEP 6: EXPOSE 8080
--> 21ea7f1db88
STEP 7: CMD ["npm","start"]
--> 2af8b0e09a5
STEP 8: ENV "OPENSHIFT_BUILD_NAME"="example-health-1" "OPENSHIFT_BUILD_NAMESPACE"="sn-labs-truong1"
--> f26125453b2
STEP 9: LABEL "io.openshift.build.name"="example-health-1" "io.openshift.build.namespace"="sn-labs-truong1"
STEP 10: COMMIT temp.builder.openshift.io/sn-labs-truong1/example-health-1:07bc83bd
--> bf728f71771
bf728f717717b9aaa00a0cc2bf7a7a7a2018817ee6cf8b287ff2831da15b52ee

Pushing image image-registry.openshift-image-registry.svc:5000/sn-labs-truong1/example-health:latest ...
Getting image source signatures
Copying blob sha256:7a803dc0b40fcd10faee3fb3ebb2d7aaa88500520e6295295f5163c4bb48548b
Copying blob sha256:b53ce1fd2746e8d2037f1b0b91ddea0cc7411eb3e5949fe10c0320aca8f7392b
Copying blob sha256:b800e94e7303e276b8fb4911a40bfe28f46180d997022c69bf1ee02fb7b86721
Copying blob sha256:76b8ef87096fa726adbe8f073ef69bb5664bac19474c5cce4dd69e08a234903b
Copying blob sha256:2e2bafe8a0f40509cc10249087268e66a662e437f10e9598a09abb5687038a57
Copying blob sha256:84a8c1bd5887cc4a89e1f286fed9ee31ce12dba9f6813cf14082ada3e9ab6f63
Copying blob sha256:0da9fbf60d485c74d153bfc7562f34533550e3dd8aa78f5e8c2476ed500e0e73
Copying blob sha256:04dccde934cf0d6ab9d303fe73b36bc4ed84329d9d562049a69a5d38afb83f14
Copying blob sha256:73269890f6fdf96184d0fdb0afe3cf697b0c8dbe1e7a077f0fab445d74372c78
Copying blob sha256:06fd6a35a04541fcd2bd3e7305fe89cff791d190b48607117c6b4837e19c1054
Copying blob sha256:27640fcbb23b49fc33f1d39864d1cbd686bbf883a751033382adacd9a4ee6136
Copying config sha256:bf728f717717b9aaa00a0cc2bf7a7a7a2018817ee6cf8b287ff2831da15b52ee
Writing manifest to image destination
Storing signatures
Successfully pushed image-registry.openshift-image-registry.svc:5000/sn-labs-truong1/example-health@sha256:8a43fd8dd4a9436981a71433544908083318c9e0e9707de54fa1b1edfeb64b1b
Push successful

Deploy the application

Now that we have our build, we can choose to deploy the application by running oc new-app. In this case we use the -i tag to indicate the image name.

oc new-app --image-stream example-health

The output should look like below:

theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc new-app --image-stream example-health
--> Found image bf728f7 (About a minute old) in image stream "sn-labs-truong1/example-health" under tag "latest" for "example-health"

    * This image will be deployed in deployment config "example-health"
    * Port 8080/tcp will be load balanced by service "example-health"
      * Other containers can access this service through the hostname "example-health"
    * WARNING: Image "sn-labs-truong1/example-health:latest" runs as the 'root' user which may not be permitted by your cluster administrator

--> Creating resources ...
    deploymentconfig.apps.openshift.io "example-health" created
    service "example-health" created
--> Success
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose svc/example-health' 
    Run 'oc status' to view your app.

Expose the service using oc expose, a route will be created. This allows our application to be accessed from the outside world, it will give us a fully qualified URL to access our running application.

oc expose svc/example-health
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc expose svc/example-health
route.route.openshift.io/example-health exposed

Find the application's route by running oc get routes. This should return a fully qualified URL that we can access from any device.

oc get routes

The output should look like below:

theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc get routes
NAME             HOST/PORT                                                                                                                         PATH   SERVICES         PORT       TERMINATION   WILDCARD
example-health   example-health-sn-labs-truong1.labs-prod-openshift-san-a45631dc5778dc6371c67d206ba9ae5c-0000.us-east.containers.appdomain.cloud          example-health   8080-tcp                 None

Copy the application's URL into a browser and login with the username and password admin:test.

example-health-sn-labs-truong1.labs-prod-openshift-san-a45631dc5778dc6371c67d206ba9ae5c-0000.us-east.containers.appdomain.cloud

Example Health details

Let's clean up our workspace by running these commands:

oc delete dc example-health
oc delete svc example-health
oc delete bc example-health
oc delete route example-health
oc delete imagestream example-health

Outputs

theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc delete dc example-health
deploymentconfig.apps.openshift.io "example-health" deleted
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc delete svc example-health
service "example-health" deleted
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc delete bc example-health
buildconfig.build.openshift.io "example-health" deleted
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc delete route example-health
route.route.openshift.io "example-health" deleted
theia@theiaopenshift-truong1:/home/project/node-build-config-openshift$ oc delete imagestream example-healthimagestream.image.openshift.io "example-health" deleted

Or better yet, just run cleanup.sh in a terminal to make sure you don't run out of resources in future.

If you are interested in continuing on this journey, you should get

Free Kubernetes cluster - https://www.ibm.com/cloud/container-service

Free IBM Container Registry. https://www.ibm.com/cloud/container-registry

© TruongIdeas & Feedback