Docker Container, the lightweight, portable, and self-sufficient OS Virtualization.


It is very clear now that Virtualization Technologies are changing the way Developpers, Sys-Admins, and Dev-Ops, operate in their day to day activities. Lately Docker has emerged as a key cloud technology for deploying and porting applications into the cloud using a container-based framework. Compared to traditional Hardware virtualization (VMs, VMWare, Xen, KVM, VirtualBox, HyperV, …), Docker, an Operating-system-level virtualization (CTs, OpenVZ, VServer, Jail, …), lets you quickly assemble, manage, and deploy container based applications on any x86 Operating System, Virtual Machine, or Bare Metal host. In this tutorial, Docker Essentials, we will learn more about Docker, how to install and deploy a Docker Container in CentOS 6.5/CentOS 7.0.

Hardware Virtualization Versus Container Based Virtualization:

alt text

  • Hardware Virtualization produces Virtual Machines that run on top of an Hypervisor (KVM, Xen, VMWare, HyperV).

    • Pros:

      • Fully Virtualized System
      • Totally Isolated Environment
    • Cons:

      • Less Agile
      • Use lot of space and memory
      • Slow to move around
  • Operating System Level Virtualization produces Containers that run on top of the Host Operating System (CentOS, RHEL, Ubuntu, …).

    • Pros:

      • Lightweight
      • Totally Isolated Environment
      • Agile and easily portable
      • Use less space and memory
    • Cons:

      • Host OS Dependent

About Docker:

The Docker platform consist of 03 main components:

  • The Docker Client:The front end interface that allows communication between the user and the Docker Daemon.
  • The Docker Daemon:The back end interface that handles requests from the Docker Client. It sits usually on top of the host Operating System (The Docker Host).
  • The Docker Index:A repository where we can search, push or pull Docker Containers or Images. It works much like a SaaS service and has a public and private access permission.

The following is an architectural view of a Docker environment.

alt text

Docker Use Cases:

Virtualization is the single most effective way to reduce IT expenses while boosting efficiency, agility, and re-usability, not just for large enterprises, but for any organization, non-profits or for-profits. Docker’s success and wide adoption relies on its lightweight, Portable, and Cross Cloud architecture. Docker container based virtualization technology lets you:

  1. Deploy lightweight and isolated container space instances.
  2. Containerize your Work, Application, or Dev-Environment.
  3. Run anywhere your Docker containers, rather in a host VM, Bare Metal, or any Cloud Infrastructure.
  4. Create an extra Layer of Security by isolating containers from one another and from the host machine.
  5. Allocate memory dynamically (Soft Memory).
  6. Be agile in deploying, managing, and porting your container-based applications.
  7. Deploy and scale more easily.
  8. Automate the creation of Docker containers using a DockerFile.
  9. Have a Fast Boot and Shutdown environment.
  10. Include git-like versioning capabilities.

Many Developers, Sys-Admins, Dev-Ops, use Docker to run flexible, resilient, and self-contained distributed systems that can be pushed from Development Stage, to Staging, then to Production. Docker simplifies the deployment and iteration process during a system development cycle.

User cases for Docker:

  • Docker can be used for packaging and deploying applications into an isolated self-contained system.
  • Docker can also be used for automating the deployment process of self-contained system using DockerFiles, Snapshots, and Docker images.
  • Docker can be used to build PaaS (Platform as a Service) environments.
  • Docker can be used to create lightweight and scallable virtual environments.
  • Docker is also used for testing, continuous integration, and deployment.
  • Docker is also used for application or system isolation.

How To Install Docker on CentOS 6.5/CentOS 7.0

Now, after you have been convinced that Docker is the solution to decrease your IT expenses while boosting efficiency, agility, and re-usability, in the next step we will show you how to deploy Docker on CentOS 6.5/CentOS 7.0.

Installing Docker in CentOS 7.0:

Since CentOS 7.0, Docker is included by default in the CentOS-Extras repository. To install just run the following command:

sudo yum install docker docker-registry

Once Docker is installed, you would want to start the docker daemon and setup Docker to start at boot:

sudo systemctl enable docker.service
sudo systemctl start docker.service

Note: CentOS 7.0 introduced firewalld, a wrapper around iptables, which conflicts sometimes with Docker. When using systemd, firewalld is started before Docker, but if you start or restart firewalld after Docker, do not forget to restart the Docker daemon. If you do not do so the Docker chain will be remove from iptables, preventing Docker from working properly.

Installing Docker in CentOS 6.5:

To effortlessly install Docker in CentOS 6.5, you will need to install the EPEL repository. If you do not have it already installed, please follow the EPEL installation instructions.
Next, let’s install the docker-io package which will install Docker on our host:

sudo yum install docker-io

Once Docker is installed, you would want to start the docker daemon and setup Docker to start at boot:

sudo service docker start
sudo chkconfig docker on

Using Docker

Once the core components of Docker has been installed in the Docker host, next we will need to create a Docker Container and run our desired application.

  • First, we’ll build our Docker Container based on the latest CentOS image.
sudo docker pull centos

To ensure that we have downloaded a CentOS based Docker Container run the following:

sudo docker images centos

The output should be similar to this:

$ sudo docker images centos
centos latest 0b443ba03958 1 hour ago 297.6 MB
  • Next, we will use our newly created Docker Container to run a simple bash shell in an interactive mode:
sudo docker run -i -t centos /bin/bash

If everything works well, you should get a simple bash prompt.

If you would like to run the bash shell in a background mode, run the following:

sudo docker run -d -t centos /bin/bash

Note: the -i option enabled us to run the Docker Container in interactive mode, the -d option enabled us to run the Docker Container in background mode, the -t option allocates a tty environment (terminal) to our Docker Container, and the -p option is used to create a network port mapping between the Docker Container and the Docker Host.

To disconnect, or detach the bash shell without killing our Docker Container, use the shortcut sequence Ctrl-p + Ctrl-q. To exit out from the bash shell and kill the Docker Container, just type exit.

Important: Note that the Docker Container is used to only run a main process. In our above example the main process was a bash shell. The lifetime of a Docker Container is equal to the lifetime of its main process. In other words if we kill the bash shell (in our example above), we will destroy in the same time the Docker Container.

Basic Docker Commands:

  • docker run --> Lets you run a Docker container in either Background, Foreground, or Interactive mode.
  • docker ps --> Lets you list all docker containers running in a docker host.
  • docker logs --> Lets you view the logs of a running job inside a docker container.
  • docker images --> Lets you obtain a list of all docker images publicly available.
  • docker commit --> Lets you commit a docker container in a local repository.
  • docker push --> Lets you push a commit docker container into a publicly accessible repository.
  • docker pull --> Lets you pull a pre-built docker image from a public repository.
  • docker diff --> Lets you list the different changes in files and directories in a docker container.
  • docker build --> Lets you build a Docker image using a DockerFile.
  • docker inspect --> Lets you inspect and gather low level detailed information about a Docker container or image.
  • docker attach --> Lets you attach new features to a Docker container.
  • docker top --> Lets you display running processes in a Docker container.
  • docker kill --> Lets you kill the main process of a Docker container.
  • docker rm --> Lets you delete a Docker container.
  • docker rmi --> Lets you delete a Docker image.

Docker Ecosystem:

alt text