Equally up to the task, or perhaps, even more evident, is the use of cloud! All these applications are largely built for the cloud, where one needs to minimize the dependency on OS and the environment to truly leverage the power of the cloud. And that’s where containerization technology comes to the fore, helping developers like you and me to break these apps down into binary and deploy them on cloud clusters!
One of the most commonly used tools to achieve containerization is, Docker. In a nutshell, Docker is a software (or a containerization platform), that provides an abstraction of OS-level virtualization. These containers contain the real operating system, the software that you build along with all dependencies to run the software in a variable environment. In this blog post, we’ll explore how to containerize a node.js application using Docker. But before that, we need to break the app into binary – and for that very purpose, we’ll use StrongLoop Process Manager.
If you’re a developer, get yourself a host-machine with Docker-Engine and Docker-Compose installed. And it goes without saying, that you need a node.js application to containerize a node.js app!
While we’re at it, let’s take a look at some of the Docker components, since we’ll be using some of them in the subsequent processes.
Next up, are some of the best practices. Trust me, although this looks simple enough, one step amiss and you’ll have to run-around quite a bit!
Having checked for the best practices, here’s a quick look at some of the Dockerfile instructions we’ll use:
And finally, we’ve reached a point where we can install StrongLoop Process Manager and get done with the rest of the process in a breeze! So, here’s the step-by-step process you’ve been waiting for:
Download and run the StrongLoop Process Manager container curl -sSL https://strong-pm.io/docker.sh | sudo /bin/sh
Verify the Docker image docker images
Verify the Docker container and ports docker ps
Note: Port 8701 is the deployment port while 3001 – 3003 is the manager port.
Once the Strongloop process manager is up and running, it’s time to build the Dockerfile – this goes a long way in ensuring we don’t run multiple complex executables.</p>
Paste the below content, save and exit
FROM node:4-onbuild RUN mkdir /demo WORKDIR /demo COPY package.json /demo COPY server.js /demo RUN apt-get update && apt-get install -y nodejs npm vim RUN npm install -g strongloop VOLUME /home/demo/:/demo/ EXPOSE 3000CMD ["npm", "start"]
Note:We have downloaded the code from git at /home/demo/ directory and by using VOLUME parameter we are mounting it to /demo/folder of the container.
The dockerfile is ready. Now we’ll create docker-compose.yml to build and run the container vi docker-compose.yml Paste the below content, save and exit version: ‘2’ services: web: build: volumes: - “./home/demo/:/demo” ports: - “32769:3000”
We have the docker-compose and docker file ready, now we will build the container: docker-compose up -d
Verify the image ocker images
Remember, since you’re using StrongLoop Process Manager container, type the container IP of the same in place of remote host. That’s all folks! We’ve successfully:
The next time you come across an application built on node.js and you want to deploy it without worrying about OS environments, you know what to do! And now that we already have a container up and running, you may choose to deploy any other app, irrespective of the OS - on the same host!
Doesn’t that save you a good bit of cost and all the troubles involved in building a new environment from the scratch?
Trust us, it does! If you have any questions, we’re here to answer. Just drop us a comment and we’ll be glad to reply in double-quick time. Until then, happy containerization!