CONTAINERIZE NODE.JS APP

For demo purpose we will use the simple node app. Create a dir and create files as

$ tree
.
├── Dockerfile
├── package.json
└── server.js
0 directories, 3 files

Create server.js as

$ cat server.js
//'use strict';

const express = require('express');

// Constants
const PORT = 8080;

// App
const app = express();
app.get('/', function (req, res) {
res.send('Hello world from nodejs webapp\n');
});

app.listen(PORT);
console.log('Running on http://localhost:' + PORT);

Create package.json as

$ cat package.json
{
 "name": "hello_world_nodejs_app",
 "version": "1.0.0",
 "description": "Node.js on Docker",
 "author": "Suraj Deshmukh <surajssd009005@gmail.com>",
 "main": "server.js",
 "scripts": {
 "start": "node server.js"
 },
 "dependencies": {
 "express": "^4.13.3"
 }
}

Create Dockerfile as


$ cat Dockerfile
FROM docker.io/centos

RUN yum -y install epel-release && yum -y install nodejs npm

RUN mkdir -p /app
WORKDIR /app

COPY package.json /app/
RUN npm install
COPY . /app

EXPOSE 8080
CMD ["npm", "start"]

Build docker container

$ docker build -t node-centos .

Sending build context to Docker daemon 4.096 kB
Step 0 : FROM docker.io/centos
---> 0e0217391d41
Step 1 : RUN yum -y install epel-release && yum -y install nodejs npm
---> Running in ee3232f51717
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: centos.mirror.net.in
* extras: centos.mirror.net.in

[SNIP]

---> cdbaffd158a7
Removing intermediate container 89946985d590
Step 6 : COPY . /app
---> a8a26264cb2c
Removing intermediate container 3d58c25da195
Step 7 : EXPOSE 8080
---> Running in d1c14008387f
---> ba0fb83a0f22
Removing intermediate container d1c14008387f
Step 8 : CMD npm start
---> Running in 0eb3d4da7e80
---> 0ba43445adf9
Removing intermediate container 0eb3d4da7e80
Successfully built 0ba43445adf9

Check if the image is built

$ docker images
REPOSITORY       TAG    IMAGE ID     CREATED        VIRTUAL SIZE
node-centos      latest 0ba43445adf9 30 seconds ago 425 MB
docker.io/centos latest 0e0217391d41 6 days ago     196.6 MB

start the container as daemon

$ docker run -d -p 40040:8080 node-centos
ede9aad900f55da1112ad607c7d34275aab67fc3cfc6be7b61ae4a8b01fdb133

check if the container started without errors

$ docker logs ede

> docker_web_app@1.0.0 start /app
> node server.js

Running on http://localhost:8080

see if you can access

$ curl http://localhost:40040
Hello world from nodejs webapp

Ref:

https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-a-centos-7-server

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s