Exploring Docker

Exploring Docker

Problem Statement: Let say we have to setup multiple application (App 1, App 2 & App 3) on machine. All these application require different version of software/libraries dependencies. How we can setup applications so that there is no conflict of libraries version between applications.

Earlier times approach where we can setup different physical machines for all the applications. Following are some disadvantages:

  • Huge cost involved for setup different machines.
  • Wastage of resources in individual machines.
  • Difficult to scale & manage the applications.

Old Approach: Setup VMs (Virtual Machines) on top of one host machine & run applications separately. See below diagram

In this kind of hypervisor based virtualization model each VM has its own OS i.e. separate resources allocated for VM (RAM, HDD and CPU). Following are some disadvantages:

  • Entire OS loads first then the app start. So Boot time is slow.
  • VMs are generally huge is size (GB’s).
  • Wastage of unused resources in VM.
  • Deployment is not easy.

Latest Approach: Containerization – Process of packaging of an application with its required files, libraries/dependencies to run in efficient way in isolated user space called container.

Its a form of OS virtualization, where containers shares the host OS kernel. Containers are light weight as it holds only required files & libs & consume resources whenever required.

Source: pexels.com (Free Licence)

What is Docker?

‘Container based technology’ which enable developer to create, run & deploy application as efficient light weight container. You can create the containers using the docker images i.e. read only template. You can get the images for OS, Programming Language, Databases, DevOps etc.

Visit Docker Hub for more details.

Containers shares the host OS kernel

Docker Installation:

Docker is easy to install application, available for variety of Linux, macOS & Windows. There are several methods to install the Docker on your machine, you can refer all the methods at docs.docker.com/

In this blog we will install docker using the shell script on Ubuntu box.

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh het-docker.sh

Checking the docker version after installation using docker -v or docker –version

Testing Docker Installation:

Docker is now installed on the system, lets run the first container to test the docker installation using docker official image “Hello-World”. You can refer the complete documentation of this image here.

Pulling the image of ‘hello-word‘ from docker hub. You can check all downloaded images in your local repository using docker images.

Now we have hello-world image present in local repository. We can use docker run command to launch the container using this image.

Listing all the container using docker ps -a command.

We are able to test the doker installation by running first container successfully. Let’s move to next step by creating the custom images by passing our own set of code.


Creating Custom Docker Image:

There are many options to create the custom docker image. Here we are going to use the Dockerfile to create multiple custom docker custom images.

Here I’m going to create two custom images using the base images of Python version 2.7 & 3.8 and will provide common python code for both the apps. Later going to create the containers for these images.

I have create one Dockerfile, first I’m using Python 2.7 version image as base.

Sample python script:

Creating custom image for python 2.7 using docker build command.

python2.7app custom image is created

After this I have edited the Dockerfile & changed the Python version to 3.8

Creating second custom docker image for python 3.8:

python3.8app custom image is created

Checking all the docker images: So we have our own custom images created. See below image python2.7app & python3.8app.

Launching Docker Containers:

Containers created for separate images & code is also executed.

Checking all the container using docker ps -a command

Check the details of containers like IP, Ports etc using inspect command.

Pushing custom images to Docker Hub:

First you have to sign up for docker hub: hub.docker. You will get you docker id i.e. username.

docker tag <image-id> username/<image-name>

docker login

# provide your docker hub credentials

To push the image :

docker push username/<image-id>

You can check your image in hub.docker

Removing the Docker Images/Containers:

# Remove all stopped containers 
docker rm $(docker ps -a -q)
# Remove single container 
docker rm <container-id>
# Remove single image
docker rmi <image-id>

Thanks!

Happy Learning! Your feedback would be appreciated!