What is Docker
Docker is one of the most exciting and recent open source projects that makes application portability more efficient. Docker is very similar to other popular and widely used virtualization tools like VMware with more ease of use and added capabilities.
It provides the facility of running applications on different platforms by packing them or encapsulating them in a virtual container. Like other virtualization tools already available in the market, docker adds an extra layer between the OS and the software but unlike traditional OS virtualization tools it does not have its own OS but it shares the underlying system OS.
Basically Docker is used when we want to use multiple flavors of different Operating Systems on same machine/hardware. Instead of installing multiple OS (or dual booting), which itself is an onerous task and overloading the hardware with it, we can use Docker which is very light weight and makes our work very easy.
The elegant idea behind Docker is: Make an application portable by packaging it in a container and using it wherever needed. Even when we want to test some application or software, it might cause problems in our Host Operating System. Therefore to counter this problem, we might use Docker, so that if we make any mistake or some problem occurs, it is limited to the virtual image that we are using in Docker and our system remains completely safe.
The most significant aspect of Docker is that it uses the resources very efficiently and makes portability of applications and software very easy and fast. The major advantage of using docker is that the installed applications are portable and can be run on any system with installed docker engine whether it’s a dedicated or cloud server.
How to start
Because of its popularity in open source community, great documentation is available for docker. This makes the learning and using docker very easy. Here I have given a step by step tutorial for installing the docker and implementing its various features through terminal. This tutorial is designed for Ubuntu 14.04 but works for other versions of Ubuntu also.
Installing Docker on Ubuntu 14.04
Before we begin the installation, you must make sure that all the packages of your Linux system are up to date, otherwise you might face problems in later steps:
$ apt-get update
Docker can be installed using following command:
$ apt-get -y install docker.io
Configuring the installed docker is the most important step for proper functioning of this tool.
Initially we need to fix the paths for this tool:
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker $ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
Now to verify that the installation was successful and all the components are working properly:
$ sudo docker info
This provides all the information related to docker.
Once Docker is installed on the system, the docker daemon runs in the background.
To make sure that the docker is running:
$ service docker.io status
In case it is not running, use following syntax-
$ service docker.io start
To list all the commands that Docker supports:
$ sudo docker
Searching for Docker Images
Since the container image is one of the most important components of the docker, therefore finding the right image is really significant. Various users have shared lot of images across docker image index that can be downloaded based on your requirements. Using terminal you can easily search and download the image:
$ sudo docker search ubuntu
Downloading and Installing Docker Images
Before we can start using docker, we must have container in which all the applications run. Once we have searched the image using above command, we can easily download the image from the network:
$ docker pull Ubuntu
At any time, we can have many images installed on our system. To get a list of images available on your system:
$ sudo docker images
Running the Docker
As the users keep making changes to the installed docker image, all the changes will be lost if they are not saved properly. Below command saves all the changes and all our activity in a particular image:
$ sudo docker commit 8ddd9e222a96 image
So next time when we initialize the same image, it starts from the point where we left if off.
Once we have downloaded and installed the Ubuntu image, we can now start the container and run the applications. The container can be run from terminal with following syntax:
$ docker run -i -t ubuntu /bin/bash
Above step launches a new docker container and the new shell prompt operates runs under this Docker container.
After you have completed your work, you can exit the docker by simply typing exit.
If you want to run some other flavor of container like Fedora, CentOS etc, then type following command:
$ docker.io run -i -t fedora /bin/bash
If the fedora image is already downloaded and present in the system, then this command will start new fedora container otherwise it will automatically pull fedora image for Docker from the network and then start Fedora container.
If you want swap and memory accounting feature to work, then you must execute following command in the terminal:
Creating New Containers
To create a new container we must start with some basic image. It is impossible to create a new container from nothing. So just download any image from the network and specify the commands that you want to make the changes:
$ sudo docker run image echo "hi" $ sudo docker run -name cont_name new_cont echo "hi"
As we work on some basic image and make changes in it, we can actually publish or share our container on internet so that other users can also use it. For that just push your image in the index:
$ sudo docker push user/image
Since different number of containers might be present on a system, so networking them proves to be really significant and useful. By default they all are connected docker0 interface through Linux bridge. But if needed then the containers can be networked among themselves and external network using custom Linux Bridge:
$ sudo apt-get install bridge-utils $ sudo brctl addbr br0 $ sudo ifconfig br0 10.0.0.1 netmask 255.255.255.0
So finally the docker has been installed successfully on your Linux system. You can use this virtualization tool for various testing and other purposes. You can work with different flavors of Linux using this tool and without having to deal with all the fuss of dual booting. You can easily have many images of different operating systems on single Linux system and work on them without any problem.
This tool will surely make your life very easy if you need to work on different flavors regularly.