docker with nodejs- thenextfact.com

1. What is Docker and Why?

Docker is a tool which enables us to run an application inside a lightweight virtual environment. It is also called as containerization. Docker helps to build, ship, run Nodejs application anywhere.

For eg: To run any application(Nodejs, python etc) we need an operating system. Operating system contains all the necessary programs which are required to run the application(programs like: node, npm, python, java, other related packages etc). The operating system also contains unwanted packages which are not required to run the application, which will eventually increase the size of the environment. On the other hand, if we compare the docker container with OS, then docker only contains those things which are required to run the application and it will eventually reduce the size of the container.

Till now we understood what is docker and the major advantage of it. Apart from this there is another major advantage of it is that it will help to build, ship and run application across the different environments like development, staging, production etc. So this will not lead to any environment specific issues due to the consistent environment of the application.

Let’s get started with docker and Nodejs:

Requirements:
1. Docker
2. Node 8.x and NPM

I assume that we are good with above-required applications. If not then follow below links for Nodejs, npm and docker installation and setup on ubuntu:

Install The Latest Node.Js And NPM Packages On Ubuntu 16.04 or 18.04

How To Install and Use Docker on Ubuntu 16.04

How To Install and Use Docker on Ubuntu 18.04

2. Creating simple Nodejs app.

Create a project directory, in my case it is nodejs-docker. Goto to command line and initialize nodejs project with default package.json file with the following command.

npm init -y

It will create a package.json file as below:

{
    "name": "nodejs-docker",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node index.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {}
}

Create an index.js file to create simple nodejs server with expressjs as below.

const express = require(‘express’);

const express = require('express');

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Install express package with npm command and dont forgot to use —save option.

npm i —save express

Add start script to our packge.json file. In my case, I have already added into a package.json file.

"start": "node index.js"

Try running our Nodejs application without docker with npm start command.

$ npm start
Running on http://0.0.0.0:8080

It looks like our Nodejs application is running.

3. Create dockerfile and wrap nodejs application inside docker.

Now its time to create dockerfile for our nodejs application. Create a new file named dockerfile in the root of the project. Our folder structure will look like this.

Dockerfile content:

The first line we need to define the image from what image we want to build our application. Our application will use Nodejs version 8 that is why we will use node image with version 8.  Explore Official Repositories of docker images.

#Nodejs image
FROM node:8

Next thing we need to create project workspace directory inside docker image which will contain our applications workspace.

# Application workspace
WORKDIR /usr/src/app
# It will copy package.json and project source code from our workspace to docker image workspace
COPY package*.json ./
RUN npm install
# Install application dependencies

# Bundle app source
COPY . .

This will expose our application on port 8080 inside docker.

EXPOSE 8080

Finally, we will start our application by npm start.

CMD [ "npm", "start" ]

At last, our whole dockerfile will look like this.

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

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

4. .dockerignore file

Create .dockerignore inside the root directory of the project.

node_modules
npm-debug.log

It will prevent node_modules and npm-debug.log being copied to docker workspace directory.

Now we are ready to play with docker commands.

5. Build docker image.

To build docker image locally with below command.

$ docker build -t /node-web-app .

In my case, it will look like this:

After building image it should be listed in the list of docker images. Hit below command to see the list of docker images.

$ docker images

In my case, it will show list like below

Ignore repositories.

6. Run Docker application locally.

Now its time to run the docker on your system with below command. Option -d will be used to run Docker in background mode. Option -p with map docker port with another port which will be used to access the docker outside.

$ docker run -p 49160:8080 -d /node-web-app

The output will look like this. It will return an id for running docker instance. This id will help in further inspection of the running Docker.

7. Docker commands cheat sheet.

There are some other docker commands which will be helpful.

This command will list the images which are currently running
$ docker ps

This command will inspect the logs inside the docker image.
$ docker logs

This command will connect to docker image, just like ssh.
$ docker exec -it /bin/bash

 

You have already voted.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.