Run a Node.js web app in Docker
Post By kanra
Blogs Run a Node.js web app in Docker

The goal of this post is to show how to run a Node.js application in a Docker container environment. By using Docker container, one can run any applications without needing to install the target application run-time environment (in this case, you don’t need to install node.js to get the app runs, including the modules need to run the app).

Docker provides containers as Virtual operating systems for running multiple systems concurrently over a single operating system.

 

Overview

 

Download/Install Docker

sudo apt-get update
sudo apt-get install docker.io  # old version
sudo apt-get install docker-ce  # new one
# another way for new version
# update local apt repo
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

Install docker-compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

 

Detailed Instructions:

https://docs.docker.com/install/linux/docker-ce/ubuntu/ 

https://docs.docker.com/docker-for-windows/

https://docs.docker.com/compose/install/#install-compose

 

 

Create a simple Node.js app

1. Create a folder called hello-world.

2. Create a JS file main.js:

var http = require( 'http' );

var port = Number( 8000 );
var hostname = '0.0.0.0';

var server = http.createServer( function ( request, response ) {
    console.log( "Coming a request" );
    response.statusCode = 200;
    response.setHeader( 'content-type', 'text/html' );
    response.write( 'Hello World!!' );
    response.end( '' );
} );

server.listen( port, hostname, () => {
    console.log( `Server running at http://${hostname}:${port}/` );
} );

3. Create a package.json file:

{
    "name": "hello-world",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "cc",
    "private": true,
    "scripts": {
        "start": "node main"
    }
}

4. Create a Dockerfile file:

# This image is based on a nodejs image
# as a parent image
FROM node:7-onbuild

# set maintainer/Author of image
LABEL mantainer "cc"

# set what port to listen
# EXPOSE /
EXPOSE 8000

Dockerfile Document: https://docs.docker.com/engine/reference/builder/

 

Build a Docker image

Download a built nodejs image from DockerHub since this sample app will be built base on this image.

The official nodejs image can be found at https://hub.docker.com/_/node

In terminal, using docker command to download the node image:

sudo docker pull node

Build a docker image of this sample web app using the project folder hello-world with repo name as nodejs-app, tag as v1.0.0 (version 1.0.0).

sudo docker build -t nodejs-app:v1.0.0 /path/to/hello-world

 

Listing all docker images:

sudo docker image ls

 

Run docker image

Run an image in Interactive mode, with pseudo terminal (like putty), and attach the host port 8000 to the container port 8000, so we can access it outside the container.

sudo docker run -itp 8000:8000 3d56b7750cfa

 

Test the running app by opening the browser, go to http://localhost:8000, the running server log should show “Coming a request”.

To terminate, enter Control+c

 

To run the image in background, using -d tag, run container in background and print container ID.

sudo docker run -itp 8000:8000 -d 3d56b7750cfa

To see the running contain:

sudo docker ps

 

To terminate the running contain, using contain ID instead of image ID:

sudo docker stop bfd4c3b5706b

 

Remove docker image

Remove a docker image by image ID.

sudo docker image rm -f 3d56b7750cfa

 

Pushing docker image to DockerHub

 

 

 

Reference

Linux installation: https://docs.docker.com/install/linux/docker-ce/ubuntu/ 

Window installation: https://docs.docker.com/docker-for-windows/

Docker commands: https://docs.docker.com/engine/reference/commandline/docker/

Dockerfile Document: https://docs.docker.com/engine/reference/builder/

DockerHub: https://hub.docker.com

Nodejs sample: https://nodejs.org/de/docs/guides/nodejs-docker-webapp/

 

 



AUTHOR : kanra
EMAIL : karawakara@gmail.com
Working on Networking, Web Development, Software Development, Server-side deployment. Having fun on doing experiments on new technologies.

Comment is Closed
/(; +_+ ?)p

7 thoughts on "Run a Node.js web app in Docker"
  1. Lani Barthel says:

    Hi there. I discovered your blog by way of Google even as searching for a related topic, your web site came up. It appears to be great. I’ve bookmarked it in my google bookmarks to come back then.

  2. Eboni Crumb says:

    Oh my goodness! Impressive article dude! Thank you so much,

  3. Lucina Devitt says:

    Hiya, I am really glad I’ve found this info. Today bloggers publish just about gossip and net stuff and this is really irritating. A good web site with interesting content, this is what I need. Thanks for making this web site, and I will be visiting again. Do you do newsletters by email?

  4. Tess Trevillian says:

    Many thanks

  5. Robbin Offord says:

    Awesome post. I am a regular visitor of your website and appreciate you taking the time to maintain the nice site. I’ll be a frequent visitor for a long time.

  6. Ozell Kemerly says:

    wonderful points altogether, you simply received a emblem new reader. What might you suggest in regards to your put up that you simply made some days ago? Any sure?

  7. Great content! Super high-quality! Keep it up! 🙂