Monday, April 30, 2018

Docker Installation & Configuration

Introduction

Docker is nowadays a buzz word, I heard this everywhere in software development sector. I went through it to learn what it actually it and why do we need it. First of all, before we go into need of docker, we have to know about virtual machines. Yes, docker is kind of virtual machine, but virtual machines are bloated, need more resources. That means we can run many instances of docker compared to virtual machines in the same system.

The lightweight nature of docker instances has several advantages such as more customized configuration, and also application portability. The application can be deployed into docker and can be packed and shipped anywhere.  Because of this, the developers prefer docker to deploy their applications in a cloud.

Installation

The installation in Linux system is quite easy. I have just installed it into my Ubuntu 16.04 system using the following commands:

  • Add public key into your system
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • Add repository
     sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"

  • Update packages
        sudo apt update

  • Install docker community edition ( I am afraid to install enterprise edition, I hope it is also free, but I like the word community)
    sudo apt install docker-ce

  • Test installation
    sudo docker info
    sudo docker run hello-world

Do Interesting Stuffs

After successful installation of docker, it is good idea to do something interesting.  Docker comes with default image hello-world which does nothing but print a message.  The image are blueprints of application which form the basis of containers. After we run images, creates a container which runs the actual application. 

So, we need to download (or pull) the image first before we create instance of it. We  do that by using "docker pull" command.

sudo docker pull busybox 

Then, after pull completes, we can see the image using the following command:

sudo docker images

Now, we run the container 

sudo docker run busybox 

This creates container instance, since no command is given, it does nothing, and terminates the instance.  If we do something like this:

sudo docker run busybox echo "hello world"

This will print out the hello world in the console. 

Now, if we run the image using -it parameter (-it stands for interactive), then the instance does not terminate.  

sudo docker run -it busybox

We can verify the running of this using the following command:

sudo docker container ls

This shows the running instances.

 Remove instance

sudo docker rm container_id1, container_id2

or  to remove all exited instances:

sudo docker rm $(sudo docker ps -a -q -f status=exited)


Remove image 

sudo docker rmi image_id1, image_id2

or to remove all images

sudo docker rmi $(docker images -a -q)


Ubuntu 18.04

Now my interest is to pull ubuntu 18.04 image and create a container of it.

sudo docker pull ubuntu:18.04 (pull)
sudo docker images (check)
sudo docker run -it ubuntu:18.04 (run)
sudo docker container ls (verify container)

So, after running container you are in the bash terminal, where you have possibilities to install commands and tools from scratch.

There basic image does not come with all necessary command or tools. So, we have to install or configure ourselves. 

No comments:

Post a Comment