What is a Container?

Container is a good way to create a package of applications along with all its dependencies so the application can be transferred between environments and also, it should run without any changes. Container is basically the process of isolating the application from other applications or from the outer world. Docker provides us a set of tools to use the Container. In this article, we will try to understand how to install and run a Docker container on a local machine.

What is Docker?

Docker is a platform for developers to develop, deploy, and run applications with containers. We can easily set up the environment, install the required software, and then run our application. There is a lot of time required to set up the environment before starting the application.

Containers and virtual machines

Containers are very lightweight virtual machines but some differences are there. Please find them explained below.
Container
Virtual Machine
Container is running on Linux and also sharing the kernel of the host machine
VM or Virtual Machine is running on a different OS that hosts machine
To run a container needs fewer resources
To run a VM needs fewer resources
Very lightweight
Large resources get utilized while running VM
Docker Integration With Node.js Rest API

Environment Setup

Now, we can get to the actual implementation, so for this purpose we need to install docker on our local machine. First, we need to check if docker is available on your local machine and if not, then please follow the steps to download and install docker on your local machine. You will get docker installer as per your operating system. Actually, docker is a Linux based system so it is very easy to install on Linux based systems.
So, please visit the link and follow the steps to install docker here.

Docker Commands

After the installation gets completed now we can start exploring the docker commands. So, the docker commands can be run from the command prompt. So first, open a command prompt or terminal.
docker version
First, open the terminal and run command docker version.
Docker Integration With Node.js Rest API
This command gives the docker installed version
docker info
Now, run the command docker info
  1. Containers: 2
  2. Running: 0
  3. Paused: 0
  4. Stopped: 2
  5. Images: 8
  6. Server Version: 18.06.1-ce
  7. Storage Driver: overlay2
  8. Backing Filesystem: extfs
  9. Supports d_type: true
  10. Native Overlay Diff: true
  11. Logging Driver: json-file
  12. Cgroup Driver: cgroupfs
  13. Plugins:
  14. Volume: local
  15. Network: bridge host macvlan null overlay
  16. Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
  17. Swarm: inactive
  18. Runtimes: runc
  19. Default Runtime: runc
  20. Init Binary: docker-init
  21. containerd version: (expected: 468a545b9edcd5932818eb9de8e72413e616e86e)
  22. runc version: N/A (expected: 69663f0bd4b60df09991c08812a60108003fa340)
  23. init version: v0.18.0 (expected: fec3683b971d9c3ef73f284f176672c44b448662)
  24. Security Options:
  25. apparmor
  26. seccomp
  27. Profile: default
  28. Kernel Version: 4.15.0-43-generic
  29. Operating System: Ubuntu 18.04.1 LTS
  30. OSType: linux
  31. Architecture: x86_64
  32. CPUs: 4
  33. Total Memory: 3.709GiB
  34. Name: amitcp
  35. ID: NLDX:GMLU:3E64:KSBK:6ZXP:YGBF:37Y2:SYK6:QNXO:YHQS:HDOC:M3TD
  36. Docker Root Dir: /var/lib/docker
  37. Debug Mode (client): false
  38. Debug Mode (server): false
  39. Registry: https://index.docker.io/v1/
  40. Labels:
  41. Experimental: false
  42. Insecure Registries:
  43. 127.0.0.0/8
  44. Live Restore Enabled: false
After running basic commands of docker (docker version and docker info) we can start to explore more on docker.
docker image ls
This command will list down images available on your local machine. Docker image includes system libraries, other files and dependencies for the executable code. Docker image can reuse static image layers for different projects. We can download the image as per our requirement.
Docker Integration With Node.js Rest API
docker pull
Pull an image or a repository from a registry. Most of your images will be created on top of a base image from the registry here. Docker Hub contains many pre-built images that you can pull and use to build our application on top of that.
docker pull nodejs
docker run hello-world
This will run the hello-world image. This is the most lightweight image. This is available after installation of docker on local machine. So using docker run command we can run docker image locally.
Docker Integration With Node.js Rest API
docker container ls --all
Docker Integration With Node.js Rest API
This command will give you some more details like commands available in that docker, last executed. An instance of an image is called a container. Running form of image means container. You can have many running containers of the same image. You can see all your images with docker images whereas you can see your running containers with docker ps (and you can see all containers with docker ps -a). So a running instance of an image is a container.
Recap and cheat sheet
  1. ## List Docker CLI commands
  2. docker
  3. docker container --help
  4. ## Display Docker version and info
  5. docker --version
  6. docker version
  7. docker info
  8. ## Execute Docker image
  9. docker run hello-world
  10. ## List Docker images
  11. docker image ls
  12. ## List Docker containers (running, all, all in quiet mode)
  13. docker container ls
  14. docker container ls --all
  15. docker container ls -aq
After understanding the commands docker uses and the way to execute these commands we will move ahead.
Develop app in docker environment
Now we will develop one javascript based application using docker.
Create one sample folder first
Create file : package.json
And paste the below code,
  1. {
  2. "name": "docker_web_app",
  3. "version": "1.0.0",
  4. "description": "Node.js on Docker",
  5. "author": "First Last <[email protected]>",
  6. "main": "server.js",
  7. "scripts": {
  8. "start": "node server.js"
  9. },
  10. "dependencies": {
  11. "express": "^4.16.1"
  12. }
  13. }
Create file : server.js
Paste the below code,
  1. 'use strict';
  2. const express = require('express');
  3. // Constants
  4. const PORT = 8080;
  5. const HOST = '0.0.0.0';
  6. // App
  7. const app = express();
  8. app.get('/', (req, res) => {
  9. res.send('testing node app');
  10. });
  11. app.listen(PORT, HOST);
  12. console.log(`Running on http://${HOST}:${PORT}`);
Create a file called Dockerfile
What is Dockerfile ?
Here we can declare or specify the software requirements or prerequisite as per my applications.
Paste the below code,
  1. FROM node:8
  2. # Create app directory
  3. WORKDIR /usr/src/app
  4. COPY package*.json ./
  5. # Install app dependencies
  6. RUN npm install
  7. COPY . .
  8. EXPOSE 8080
  9. CMD [ "npm", "start" ]
Create a file called .dockerignore
Paste the below code
node_modules
npm-debug.log
Building application’s docker image
Open the terminal
And run the below command
docker build -t node-web-app .
Docker Integration With Node.js Rest API
Your image will now be listed by Docker:
docker images
Run the image
docker run -p 49160:8080 -d node-web-app
Here 49160 is port number exposed by docker for the outside access.
Test the application
Finally we need to see the result so open the browser and http://localhost:49160/
Docker Integration With Node.js Rest API
Code modification and rebuild the image

Advantages of Docker

Conclusion

This all about docker and integrating node api. This way we can create any application and integrate with docker. For understanding more about docker please visit here.