My initial goal was to figure out how to “containerize” a Node.js application and then put it on my local openshift VM, but when I started to look at it little deeper, I found a few different ways of doing it. Hopefully, this post will go into the different ways.

Using Source-to-image

S2I will take your code and a compatible S2I image and smash them together to give you a nice container to run.

So how does this work, it sort of sounds like magic? Well, it sort of is.

Let’s look at the command we are going to use to build our app’s container image. For an example app, we will keep using the basic express example from earlier. This repo https://github.com/lholmquist/s2i-web-app will have the code. So clone the code (download) from GitHub and then on your local machine after the code has been “downloaded”, make sure you are in that new code directory since the next few commands will assume you are in that directory.

s2i build . bucharestgold/centos7-s2i-nodejs:7.5.0 s2i-web-app

Let’s dissect this.

s2i build

– We are using the build command of the s2i cli tool.

The

.

is telling the tool that we are using our current directories code.

This next part is interesting:

bucharestgold/centos7-s2i-nodejs:7.5.0

Let’s look at this from back to front.

7.5.0

is the node version this image uses.

centos7-s2i-nodejs

is the image we are going to use. I’ll explain in a minute why we used this one.

bucharestgold

is just the namespace from docker hub.

So if run this command, you will see the base image (the centos7-s2i-nodejs) download like a normal container build would.

Then you will see a message:

---> Installing application source

What this is doing is copying your code in your current directory to a directory in the image.

Then

---> Building your Node application from source

This basically is just running a

npm install -d

Once done, you can then run the similar ‘docker run’ command we used earlier to create a running container.

docker run -p 8080:8080 s2i-web-app

And if you go to http://localhost:8080 in a browser, you should see something just like the other example.

So the reason we used this image, is because it is already “enabled” to use s2i’s features.

Now, this can be used by itself for getting a Node.js app (or really any other language app) into a container quickly, but where this tool shines is when developing Node.js apps for Openshift.

If we take a look at the base images Dockerfile, we can see that actually we drop the root user and change the permissions. This is because, on Openshift, you cannot run the process as root. So creating the docker container this way saves some steps when developing with openshift.

We also provide some “ONBUILD” docker images in case you are in a scenario where you want to use this “bucharestgold/centos7-s2i-nodejs” image and need to use a Dockerfile.

You would just put this in your “FROM” part of your Dockerfile.

FROM bucharestgold/centos7-s2i-nodejs:7.5.0

Lets actually take a look at what that original Dockerfile would look like if we used this image instead.
Here is the original: