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:
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:
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.
It will create a package.json file as below:
Create an index.js file to create simple nodejs server with expressjs as below.
const express = require(‘express’);
Install express package with npm command and dont forgot to use —save option.
Add start script to our packge.json file. In my case, I have already added into a package.json file.
Try running our Nodejs application without docker with npm start command.
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.
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.
Next thing we need to create project workspace directory inside docker image which will contain our applications workspace.
This will expose our application on port 8080 inside docker.
Finally, we will start our application by npm start.
At last, our whole dockerfile will look like this.
4. .dockerignore file
Create .dockerignore inside the root directory of the project.
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.
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.
In my case, it will show list like below
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.
￼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.