Previously we have seen the installations of hypervisors like Xen and cloud middleware like openstack. Recently a new technology called docker container has become the hottest trend in application development. In this article we shall see the what exactly is a docker, its differences with virtual machines, installation of docker and its usage.
What is Docker ?
Docker is a container which wraps up a piece of software in a complete file system that contains everything it needs to run: code, run-time, system tools, system libraries – anything you can install on a server. Containers virtualize at the operating system level making it more efficient than hypervisors in system resource usage. There are many Linux containers technologies like LXC, Docker etc.,
What is the difference between Docker and a Virtual Machine?
With different architectural approach, containers are much more portable and efficient, even though they have similar resource isolation. Below pic shows the architectural difference between a container and Virtual Machines.
|1.Needs an Hypervisor and a full OS inside||1. Talks to the host kernel|
|2. Bigger Footprint (RAM and Storage space)||2. Smaller footprint (No RAM and differential storage)|
|3. VMs consumes storage space for each instance ~1.2GB||3. Consumes very less space ~2.5MB|
|4. Heavier||4. Lightweight|
|5. Virtual Machines startup time is in the order of minutes||5. Startup time is in the order of seconds|
|6. Deployment is tough.||6. Easy Deployment with minimal requirements for running the application|
|7. Slower||7. Faster|
|8. Security issues of running OS||8. Security issues limited to Applications|
How to Install a Docker Container on CentOS 7?
The latest official release of Docker is v1.10. Below instructions is for installing Docker Container on CentOS 7 server. Login to the CentOS 7 server as root and follow the below steps.
Step 1: As a matter of best practice lets update the system using yum as shown below:
# yum -y update
Step 2: By default Docker is included in the CentOS-Extra repository. If you would like to use a more up-to date version of docker, run the below command to add to repo.
# tee /etc/yum.repos.d/docker.repo <<-'EOF' > [dockerrepo] > name=Docker Repository > baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/ > enabled=1 > gpgcheck=1 > gpgkey=https://yum.dockerproject.org/gpg > EOF [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg
Step 3: Install docker package and docker-registry. Docker registry is a stateless, highly scalable server side application that stores and lets you distribute Docker Images.
# yum -y install docker docker-registry
Step 3: Upon successful installation of docker, start the docker service.
systemctl start docker.service
Step 4: Make sure the docker service starts on boot with the below command:
systemctl enable docker.service
Step 5: Verify the docker service
systemctl status docker.service
docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2016-03-22 16:29:00 IST; 20h ago Docs: http://docs.docker.com Main PID: 30108 (docker) CGroup: /system.slice/docker.service ââ30108 /usr/bin/docker daemon --selinux-enabled
Mar 22 17:10:12 vm1 docker: time="2016-03-22T17:10:12.541577770+05:3...1" Mar 22 17:11:05 vm1 docker: time="2016-03-22T17:11:05.429417681+05:3...0" Mar 22 17:11:05 vm1 docker: time="2016-03-22T17:11:05.429730128+05:3...s" Mar 22 17:11:05 vm1 docker: time="2016-03-22T17:11:05.429781346+05:3...04 Mar 22 17:11:13 vm1 docker: time="2016-03-22T17:11:13.020019713+05:3...0" Mar 22 17:11:23 vm1 docker: time="2016-03-22T17:11:23.021293894+05:3...e" Mar 22 17:11:28 vm1 docker: time="2016-03-22T17:11:28.600657892+05:3...n" Mar 22 17:11:35 vm1 docker: time="2016-03-22T17:11:35.938950613+05:3...0" Mar 22 17:11:45 vm1 docker: time="2016-03-22T17:11:45.939810804+05:3...e" Mar 22 17:11:57 vm1 docker: time="2016-03-22T17:11:57.720737864+05:3...n" Hint: Some lines were ellipsized, use -l to show in full.
How to use Docker?
Step 1: Download a Docker container. Run the below command to get the latest stable official CentOS image:
# docker pull centos
The above command pulls the latest stable centos release. To pull any other version of CentOS image, for example CentOS 6:
# docker pull centos:centos6
Step 2: To verify the images have been downloaded locally:
# docker images centos REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/centos latest bb3d629a7cbc 2 weeks ago 196.6 MB
Step 3: To list all the available downloaded images:
# docker images
How to run a Docker Container
Step 1: Run a basic Centos container with a bash shell. Here, -i option attaches stdin and stdout, -t allocates a tty.
# docker run -i -t centos /bin/bash
That’s it! Now you’re landed onto a bash shell inside a centos docker container.
Commands for working with Docker containers
1. To disconnect or detach from the container without shutting down the container:
Ctrl-p + Ctrl-q
2. There are many containers already available by community. To list the containers from the community:
# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 2042 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 18 [OK] docker.io docker.io/jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / PHP M... 14 [OK] ... ...
# docker search ubuntu INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/ubuntu Ubuntu is a Debian-based Linux operating s... 3464 [OK] docker.io docker.io/ubuntu-upstart Upstart is an event-based replacement for ... 60 [OK] docker.io docker.io/torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 25 [OK] docker.io docker.io/rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 23 [OK] ... ...
3. List all the containers
# docker ps -a
4. List only running containers
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4293c8265d00 centos "/bin/bash" 22 hours ago Up 9 seconds agitated_poincare
5. Stop a Container
Get the container ID from the above listing and use it to stop the respective container:
# docker stop 4293c8265d00
6: Restart the docker service
# systemctl restart docker.service