Monday, January 11, 2016

Docker – basics

We have been working on Virtual machines until now but containers are rapidly pushing the virtualization technology behind. In this article we will see a container based technology called Docker.

Docker is an Open-source Project that helps in creating and managing linux based containers. These containers are light weight VM’s thus allow code to run in isolation from other Containers. One other important thing is this container shares the Host features like kernel, network, Disk and memory etc making it more light weight, less CPU intensive and low memory.

Since these containers use the Host machine features booting up the containers are done very fast. Docker does provide a CLI that allow you to do almost everything you could want to do on containers.

In other words Containers allow creating multiple isolated, secure Linux containers in order to run them on the same physical server without any conflict between applications. The first type of containers was created by OpenVZ. FreeBSD then came up with another container technology which lets us to put apps and servers into one Jail ( which we call as a Container ) by using Chroot.

What exactly can docker can do for us?
Docker solves many problems that we see from the Virtualization.
1) Containers are less resource users
2) They don’t need a separate hardware abstraction layer for running.
3) They share the same Host kernel and thus no need to install separate Kernel for every container
4) They isolate the application dependencies
5) Containers are shared as images
6) Creating ready to start applications that are easily distributable
7) Allowing easy and fast scaling of instances
8) Testing out applications and disposing them afterwards

Docker Terms
Before moving into the docker usage, there are certain terms that need to learn.

Images – Images on Docker are some thing like a snapshot of the Virtual machine but are light weight. These images allow one to replicate containers if we have an existing image of the same container. Most of the images are available publically and if we don’t have the image available docker allows us to create our own. Images can be called, for example, ubuntu:latest, ubuntu:precise, django:1.6, django:1.7, etc. This means that we can download a light weight container image version for ubuntu and create a container based on that.

Containers - From images you can create containers; this is the equivalent of creating a VM from a snapshot, but way more lightweight. Containers are the ones that run stuff. They also have an unique ID and a unique human-readable name. It’s necessary for containers to expose services, so Docker allows you to expose specific ports of a container

Volumes – Volumes are how you persist your data beyond the lifespan of the container. These are the spaces inside the container that store data outside of it thus allowing us to destroy container with out touching the data. Docker allows you to define what parts is your application and what parts are your data, and demands that you gives you the tools to keep them separated.

Links – Whenever a container is started a random Private IP is assigned to that so that other containers can talk to that using the IP address. This is important for 2 reasons: first it provides a way for containers to talk to each other, second containers share a local network. The links allows one container running web application connect to another container running Database.

Installing Docker
Installing docker is pretty straight ward.  Docker packages are being added to the Main repository for linux and other flavors. In Centos we can install the package docker.<ENV> directly to install docker.

Once the docker with all necessary packages are installed, we can start that using “service docker start”. As I said earlier Docker has a CLI that allows you to do almost everything you could want to a container. Check the installation by running the version command as

[root@vx111a work]# docker version
Version: 1.8.2-el7.centos
API version: 1.20
Package Version: docker-1.8.2-10.el7.centos.x86_64
Go version: go1.4.2
Git commit: a01dc02/1.8.2
OS/Arch: linux/amd64
Version: 1.8.2-el7.centos
API version: 1.20
Package Version:
Go version: go1.4.2
Git commit: a01dc02/1.8.2
OS/Arch: linux/amd64

How does this Work?
A Container is something related to Operating System level virtualization that allows us to create multiple isolated user spaces instead of just one. This isolation is made by using the chroot. According to Wiki “chroot on Unix operating systems is an operation that changes the apparent root directory for the current running process and its children. A program that is run in such a modified environment cannot name (and therefore normally cannot access) files outside the designated directory tree

When we create multiple virtual machines by using VM, the operating system and virtualized hardware are duplicated for each guest but when we create multiple containers, only the operating system distribution related folders are created from scratch, while the parts related to the Linux kernel are shared between containers.

Generally in a virtual machine,
In this case even when we configure the different distributions using Vm , their Guest OS will be duplicated even though they have same kernel.

But when you configure things using Docker or any other container tool we have,
In this case of containers, we have the operating system level architecture is shared across containers and only bin and lib’s are created from scratch for different containers. Docker engine takes care of these orchestrating containers.

More to Come, Happy learning J

No comments :

Post a Comment