Docker containers introduction and working

Docker is an open source platform to develop, ship and run an application in an isolated way using docker containers. Container is an unit of software that packages all dependencies such as binaries and libraries required to run an application.
Containers are not new technologies, since from 1979 used in UNIX system. The below article give a detailed info about container evolution from dzone.
The Evolution of Linux Containers and Their Future 
Docker containers isolate from each other but share the same Linux kernel system. This uses the kernel namespaces and cgroups to manage application. Docker containers can deployed in separate and managed as usual application.

Docker containers vs Virtual Machines

The Docker also use virtualization as Virtual Machines. But it uses OS level virtualization, whereas VM uses hardware level virtualization. Docker shares the Operating system kernel, VM shares the hardware. This makes docker more efficient than VM. Consider the following picture which illustrates the difference between Docker and VM.

docker vs VM

In VM architecture, it uses the Hypervisor to manage the Guest OS on it. Each Guest OS has different binaries and libraries for running software applications. In docker, it shares the Linux kernel to run all applications with its own dependencies.

These dependencies packaged as image called docker images. Docker images deployed as containers to run the application in an isolated environment. These  containers are run on docker engine.

docker architecture
Docker architecture

Docker Images

Images are simple templates that instructs to create the containers. Each image even built from another images itself. These images can be build using the dockerfile. dockerfile is a file which instructs to build an image using other images and dependencies. Even the syntax for writing the dockerfile is simple language. Each line in the dockerfile creates a layer to run a container. The registries such as docker registry, cloud registries include docker hub, Amazon ECR, Azure container registry.

Docker Containers

Container are the runnable instances using the docker images. It packages all the bins and libraries needed to run the application in an isolated environment. Docker CLI and Docker API can used to create, delete, start, stop containers using the Docker daemon. Each container has its own networks, storage, and configs. Container can connect to another container using its networks . When a container deleted, the storage in the container also removed. Here we can use the persistent storage for storing the data instead of containers.

Docker daemon

Docker daemon(dockerd) uses Docker API manages the all docker objects in the docker environment. These objects are containers, images, storage, networks. The docker daemon communicates with other daemons for container management and network communication.

Docker client

Docker client(docker) is the interactive way we can execute the API to manage docker system. To manage the docker daemon the docker API called using docker command.

Registry

Docker images are stored in the registries. Docker hub is a public registry, we can create and push the image in this docker hub. docker pull, docker push commands used to pull and push docker images from the registry. Cloud technologies also provide registries such as Amazon ECR by AWS, Azure container registry by Azure cloud.

Docker installation

Docker can be installed natively in Linux and Mac OS since it uses linux namespaces and cgroups for working. In windows, docker can be installed with some dependencies. Docker desktop, a GUI based docker tool is available for Windows and Mac OS
The following link give the documentation for installing docker in Mac OS, Windows and Linux distros.

Running our first docker container

First, check the docker is installed on your system using following command

$ docker --version
Docker version 19.03.8, build afacb8b7f0

Now, we run our first container hello-world image. If image is not present in our local system, it pulls those image from docker hub and runs it.

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    ca4f61b1923c: Pull complete
    Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
    Status: Downloaded newer image for hello-world:latest

    Hello from Docker!
    This message shows that your installation appears to be working correctly.

To list the container running in the system use ps command.

 $ docker ps --all

    CONTAINER ID     IMAGE           COMMAND      CREATED            STATUS
    54f4984ed6a8     hello-world     "/hello"     20 seconds ago     Exited (0) 19 seconds ago

To list the images pulled use

$ docker image ls

The next post will be about building a docker image.

For docker reference refer this docs -> go.

For my programming article refer this -> go.

If any suggestions or queries please comment it.

 1,205 total views

Merge conflicts and learn how to resolve them in git.

Merge conflicts

In this article, it gives knowledge about how merge conflicts happen and learn how to resolve merge conflicts using different methods and tools. To resolve merge conflicts we will be using tools and commands in git.

So, what is a Merge conflict? Merge Conflict is a warning when we try to merge branches if the same files are altered in both branches.
For instance, assume you’re working on an HTML file for web development. You use a master branch to have a mainline, and test branch to work without disturbing the master branch. Here, you have a file called index.html, you altered it in the test branch and commit it. You also altered the master branch’s index.html file. When you try to merge the test branch with master, it shows a conflicting message as git suggest you solve it to merge.
This is the same when two developers in a team try to alter files and merge it into the master branch. At, this time it shows merge conflict.

How to resolve a merge conflict

I have illustrated the images for reference please verify it.
Here, I have a directory called git-merge-conflict-demo with file content index.html.
Firstly initialize the git repository.

$ git init

Add the files to the git repository.

$ git add .

Commit the files to the git repository.

$ git commit -m "First commit"

git-merge-conflict-1

Then create the branch called test.

$ git branch test

Navigate to the test branch and change the contents using the nano text editor.

$ git checkout test
$ nano index.html

Then add the file and commit to the git repository.

$ git add . 
$ git commit -m "First test commit"

git-merge-conflict-2

Navigate to the master branch and now change the contents of the index.html file. Add it and commit it to the master branch.

$ git checkout master
$ nano index.html

Alter some contents in index.html, then add and commit it to the master branch.

$ git add . 
$ git commit -m "Altered commit in master"

Now try to merge the test branch with the master branch. It shows the conflict, auto-merge failed.

$ git merge test
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and the commit the result.

Try the status command to show unmerged files and changes. Now, try the diff command it shows the difference between the two files as the following image.
git-merge-conflict-3

How to resolve the merge conflict

1. The direct way to resolve is to clean-up the merge working tree by manual. Abort the merge option using the command

$ git merge --abort

2. Use the git mergetool such a vimdiff, araxis, vimdiff2. mergetool are the different graphical tools that can be used to point out the difference in two files and help to resolve it. The image shows the vimdiff2 tool

$ git mergetool --tool=vimdiff2

mergetool

3. Use the diff command to show the difference between the files in the MERGE_HEAD version.

$ git diff

diff-command

The contents above ======= are contents in our side or current branch. The contents below to it are on the other side. The text in red is the altered content. This is the same as in the mergetool option.

4. Use Status command to show the unmerged files and change contents.

$ git status

5. Use log -- merge -p the command to show log files and changed contents.

$ git log --merge -p

log-command

Use above all these methods use them as a guide to change the content of the files and then merge the files.

Learn how to branch in git -> Go.
Click here for Git archives.

To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.

 962 total views

Git Merging and learn how to merge in git

Git Merging

Though Git Branching is a powerful feature to branch the development work without changing the mainline, to add the features from one branch to another we need of this Git Merging, yet another powerful feature. Git Merging feature by merging the one development work of a branch to another branch it currently resides. This post help you to understand, how to git merge works and merge files in git from one branch to another.

Git Merge working

In Git Merging, we used the main command

  • $ git merge

The merge command will merge the given name branch to the current branch. When a merge command is given, a new snapshot is created in the current branch. Now, the pointer points to both previous snapshots of the current branch and another branch.

Here, we continue from the previous Git branching post(if not read it Git Branching). Assume, you’re currently in the ‘test’ branch. You made some improvements to the ‘test’ branch. Now, you want to add those features to the ‘master’ branches. First, navigate to the ‘master’ branch by the following command

$ git checkout master

Now, you moved to the ‘master’ branch. To merge the files to the ‘master’ branch from the ‘test’, type

$ git merge test

After this, it shows the following image

git-merge-work

The updating of checksums shows in the first line, the type of merge in the second line, the files merged and altered in next and changes in the last.

git-merging

The illustration describes that snapshot s6 from the ‘test’ branch is merged to the s5 ‘master’ branch as s7 snapshot. Now, the pointer heads to s5(master) and s6(test). This is how the basically merging works in git.

Problems with merging

When we try to merge a file, which is altered in both branches, it makes some conflicts with the merging and it shows git asks us to clear the conflict first. This looks like

merge-conflict

Here, I altered a Readme.md files both in master and test branches. When I try to merge it, it shows the ‘CONFLICT’ message.

Learn how to resolve this merge conflicts -> Go.

Learn how to branch in git -> Go.
Click here for Git archives.

To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.

 900 total views

Git Branching and learn how to branch

Git branching

Every version control system has a branching system. Branching is a powerful feature in which it does not disturb the main or actual pipeline. For instance, we can branch the development into main and test, in the main branch actual working content is developed, whereas in the test branch the features to be added can be developed and can be merged with the main branch. Git also supports this branching and merging feature.

Many version control systems make this feature in a heavy way by copying all content from one branch to another. This leads to a high load. But, git makes it in a lightweight process by snapshots(Already mentioned in the post – Git introduction please refer it). It snapshots the content using checksums. This makes git a lightweight and powerful version control system.

branch command

In git, a new branch is created using branch command. The default branch in all git repositories is main branch. The branch command simply creates a new branch only, do not navigate to the branch.

Here, we create a new branch called the ‘test’ branch by the following command.

$ git branch test

To view, the branches execute the log command that shows an arrow specified with the branch name. The log shows the name of the branch currently the pointer present and created.

$ git log

git-branch1

checkout command

To navigate to the branch, use the checkout command. This command takes control of another branch by taking a snapshot of the’master’ branch.

$ git checkout test

Now check out the log, the arrow changes to the test branch.

git-branch2

Now, you can able to work on the test branch freely, without disturbing the main content altering. If checkout is an extra process, use -b option with branch command. It will automatically navigate to target when a new branch is created.

The above picture illustrates how branching is done. Here, the ‘s’ stands for a snapshot. When the branch command is given with the name ‘test’, it creates a branch called ‘test’. Here s3 is the snapshot of the master and pointer points to master snapshot. When checkout command with ‘test’ given, it navigate to test branch. When some alteration made in the ‘test’ branch, commit it. This creates a new snapshot called s4 in the ‘test’ branch.

Edit and commit until the feature is ready. If your features are ready, to make the ‘test’ developments to the ‘master’ branch we must do merging of the branches, this will be available in the next tutorial.

Click here for Git archives.

To view official git documentation please visit git docs.

 639 total views

How to get remote repository and commit files in git

Git commands that are commonly used for managing project files such as ‘init’, ‘add’, ‘commit’, ‘status’, ‘log’.

 406 total views

Git commands

The last Git article illustrates how the git version control system works. This article will help you to learn how to get a remote git repository and commit files in git. You also get knowledge about git basic commands for remote repositories and commit files in git repository as local.

Getting a git repository

To get an existing git repository either from the local system or online repositories such as Github, bitbucket we can use the ‘clone’ command.
The format for getting a repository is as follows

$ git clone 

For example, consider the following syntax

$ git clone https://github.com/example.git

Initializing a repository

Initializing a repository refers to initialize the repository which is needed to be git i.e to be tracked. This is done using the ‘init’ command.To initialize a repository use the following command

$ git init

Go to the folder you need to be tracked and execute the ‘init’ command.
This creates a new file called .git the file that is where the git keeps of the files that are stored in it in the form of encryption.

Next, we need to stage the files that need to be tracked. This is done using ‘add’ command. The staging files refer to set the which files in the repositories are needed to be tracked in the git.
To stage a file

$ git add README.md

Instead of README.md replace with the file name needed to be staged.
To stage all files specify ‘.’ to add all files in the repository.

$ git add .

Next, we need to commit the files that was staged using the ‘commit’ command.

$ git commit

To commit the files we need to specify commit messages for all commits using ‘-m or –message’ specification in ‘commit’ command.

$ git commit -m 'Added new files'

Tracking a repository

To track the file status we can use ‘status’ in the git command.

$ git status

The status will show you the changes that are changed if any as follows.

On branch master
Changes not staged for commit:
    (use "git add ..." to update what will be committed)
    (use "git checkout -- ..." to discard changes in working directory)
        modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

If no changes are made in the files it displays the message as follows

On branch master
nothing to commit, working tree clean

To view the log of the files committed to the git we can use the ‘log’ command.
This log command will give you all the logs about the commit done to the files in git.

$ git log

This will output the log as follows

commit 9222b1d10ed1741a2e7ddde50a264a729dc9c85d (HEAD -> master)
Author: AjeethT 
Date:   Thu Jul 16 15:48:54 2020 +0530

The first line gives the commit hash value next it specifies the branch as a master. Next are the author’s name and the timestamp of the commit.

The full flow of the git operation are as follows:

$ git init
$ git add README.md
$ git commit -m 'Initial commit'
$ git status

In the next git post, I will be sharing on different operations on the git repository.
Click here for Git archives.
To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.
If the article is helpful or any thought please comment on me.

 406 total views

Hacker earth solutions

Count divisors – Hacker earth solution. My first hacker earth solution. Count divisors, Factorial, Toggle String, Roy, and Profile Picture-C++.

 305 total views

Count divisors

I try to start practicing for data structures and algorithms. So, I chose this Hacker earth platform to practice my logical skills. This is my first problem with Hacker earth. I first start with the basics of input and output. Here are the hacker earth solutions for count divisors, factorial, toggle string, roy and profile picture using c++.
This problem is called count divisors. This is simple we need to find the count of numbers divisible by a number k.
You can find the problem set from here. The solution for the code is

Factorial

Next is the simple factorial program which gives the factorial of the given number. The problem set is here. The code is below

Toggle string

This probelm is to covert captial letters to small and small letters to capital. This is solved using the ASCII values of the alphabets. The problem set is find here.

Roy and Profile Picture

In this problem, we need to make the profile picture resolution accepted if it is equal or crop it, if it is bigger and upload another if it is small. The problem set is found here.

For programming articles -> Go.

If any suggestions and solutions comment about it, please.

 305 total views

Git and Version control system introduction

Introduction to the git version control tool and basic operations in the git tool.

 382 total views

Git

Git is an open-source version control system used by millions of users over the world. A version control system is used to track every change we do to a project. This system keeps track of every file changes done accordingly to the time. These changes can be rollbacked whenever needed from the repositories. Linus Torvalds, the person who developed the Linux kernel is also the reason to build this Git version control system.

There are three types of version control system

1. Local VCS – In the local version control system, version control is done within the local computer we use. It only saves the changes in the files over time as a version within the local computer. It saves the changes in the localhost.

2. Centralized version control system – Has the name specifies, there is a centralized hub that is used to store the files including the file changes. The central hub stores the file details and the snapshots over time. We can revert the files when needed from the central repository.

3. Distributed version control system – As in the form of distributed computers, we can have a distributed version control system in which the version control system is set on every computer connected over the network. The file changes are distributed over the servers and they can be retrieved at any time.

These repositories can be a local machine or server machine. An opensource distributed Git platform is called GitHub. By this, we can store and control our opensource projects in a distributed manner. This is also helpful in contributing millions of developers over the world for opensource projects successful. The other platforms are called Bitbucket, SourceForge, etc. Among them, Git is popular due to the following features:

  1. Open-source project.
  2. It tracks files like the snapshots in virtual machines.
  3. It gives security by means of checksum all the files using the SHA1 algorithm.
  4. It uses easy strategies to store the files in three stages as stages modified, staged, committed.
Git-working
Working of git

Git installation

Install git on Linux OS

All linux system comes with all the Linux kernel
To install git on Linux distros

Debian system – using apt

sudo apt-get update
sudo apt-get install git

Fedora system – using yum

sudo yum install git

Install git on Windows OS

Like Linux, windows don’t come with git
To install git on windows system

  1. Download the git installer from here.
  2. Select the installer and then give next and setup git first.
  3. Next, we need to configure it by using Command prompt.
  4. Run the following commands in the cmd and give your user_name and email_id

git config --global user.name "user_name"
git config --global user.email "email_id"

Install git on Mac OS

To install git on Mac OS we use brew package manager

Using the brew package manager

brew install git
After successful installation of git check it by checking version using below command
git --version
It will return the following output according to the version number
git version 2.9.2
To get help type
git --help

 382 total views