Docker in DevOps

What is Docker
Docker is an OS-level virtualization software platform for developing, running, deploying and finally shipping software applications. it provides the ability to run a software application in a isolated environment called a container.
But Why ?
Think you are a team member of a software application development team or a individual developer. and you or your team is developing a enterprise software application in agile process background. when one developing cycle is finished, that developed code will be given to testing team for test. and testing phase is finished, it will be given to deploying phase to deploy in production development. But the problem is when you hand over the code that you developed, to another one they may say that the code is not working in their machine or their environment(Testing env, Production env). If you have ever done a university project, your team members could have blame to you that the code is not working in their machine. Or the client could have blame you as well.

The reason is to that is, your machine’s configuration, the programming language version that you working on or the operating system and etc. are different from your machine to another or from your environment to another. So the code that you developed may not work as same as your machine in other machines. This is the scenario that Docker will help to solve us.
How it works ?
Docker’s working mechanism is same as the virtual machine but there are some differences.

In a virtual machine there is a main operating system and on top of that there is a program called Hyper-visor. On top of that hyper-visor we install another operating system or systems. Hyper-visor manages environment for virtual machines with all the scenes behind the virtual machines.

But in docker there is no any hyper-visor involved. on top of the main operating system there is a system called Container(Docker) engine, and that docker engine manage and control all the docker container on top of that engine. each container works as a virtual machine but more efficiently and differently than a virtual machine.
Docker vs Virtual machine
Docker uses less memory than virtual machine. We cannot use remaining memory after we define some memory space to a virtual machine but in docker, it uses only the required memory for a process so remaining unused memory can be used by another process
The performance in docker is high than virtual machine. Running multiple operating systems in a one machine leads to unstable performances. But docker is running on one docker engine in same operating system
Also we can’t use a same app or same virtual machine in another system, because environment configurations isn’t the same on every environment. But in docker we can use same container in different systems because the app is encapsulated inside a container. Container is like a isolated environment, So that it is not dependable on a system(Like java virtual machine)
Boot-up time of docker container is very very less than boot-up time of a virtual machine
Docker Structure
There are four components belong to docker structure, they are
Docker client and server
Docker images
Docker registry
Docker container
Lets talk them one by one
Docker client and server

Docker client and server exists in your local machine. this client includes the the docker CLI and API and this host includes the docker daemon and containers and images. we use CLI to execute necessary commands and the that commands will go to docker host through a REST API. Docker daemon is listening to the REST requests and interact with the operating system to execute that command.
Docker image

Docker image is a template of instructions that use to build containers. it is build using a file called DockerFile. There are multiple layers in a docker file. The base layer is a base image(mostly it will be a operating system image) and every layer will depend the layer that below of current layer
to understand a docker image and layer lets examine a real docker file

In this docker file each command is belong to each layer.
FROM - Choose a base image.
PULL - Pull a docker image or file from docker registry.
RUN - Commands to run inside your docker image.
CMD - define a default command to run when your container starts.
Docker registry

Docker registry is the place where docker images are stored and shared among users. also docker has its cloud based registry aka docker hub. users can host their created images in docker hub and other users can use it.
We use pull command to extract a image from docker registry and push command to store a image to docker registry(either private or public)
Docker container
Docker container is am executable package of the application that you developed and it’s dependencies. We consider as it is a isolated separated light weight environment that can be executed on a docker engine.

we can build a container using a docker image and also we can build multiple containers from a one docker image.
run command is used to build a container and if the image doesn’t exist in locally it will be downloaded from registry and the build the container
Useful Docker Commands
You can get some useful commands and full explanation of that commands from following link. I am not going to explain one by one in here because a can get a better explanation from this website.
Demo
In this demo I am going to add docker to a nodeJs project as the demo. In order to use docker you need to install docker to your local machine. You can install docker easily by following instructions in this website.
For windows
For ubuntu
For mac
Writing DockerFile
First you have to create the DockerFile and add it into the root file in your nodejs project structure

FROM node:10 : We define from what image we want to build from in this the nodejs image from version 10
WORKDIR : Defining the app directory inside the container
COPY : copy the package.json file from root to inside the working directory of container(/usr/src/app)
RUN : Run the command npm install to install the dependencies
COPY . . : copy the project files to working directory of container(in this dot means the current directory, so current directory of project is root and current directory of the container is /usr/src/app)
EXPOSE 8080 : define the port to expose to the environment from container the communication of container with environment is happen thorough this port
CMD [“node”, “server.js”] : finally the command to start the nodejs server inside the container ( node server.js)
Building image
Go to the directory that has your dockerfile and run the following command
docker build -t app .
Building container
Run the following command to see all the docker images
docker images
and then run following command to build the container
docker run -p 49160:8080 app
-p defines the port that should use public port related to private port inside the container
Now you can use the app using localhost:49160