Docker is an open source lightweight virtualization technology that helps to build, run and deploy application as containers. Docker enables you to configure the application once and run anywhere. Containers are portable. Hence, applications running on containers can be seamlessly ported across different infrastructure and platform clouds. Docker is lightweight because it runs directly on top of the kernel unlike Virtual Machines (VMs).
Virtual Machine Vs. Docker containers
A VM is a heavyweight computing resource that contains entire OS inside the host OS and runs in the application space on the server on top of the hypervisor. Hence, 10-15% of the host capacity is occupied, which is quite expensive in terms of resource utilization. What is more, an application running on a VM will run on top of all these additional layers.
However, Docker does not need a hypervisor and can run directly on the OS kernel. This enables Docker to create the machine set-ups faster when compared to VMs.
Why to use Docker?
Docker container is a lightweight process because it runs directly on top of the OS kernel. Developers can build the application stack in their laptops. Further, the same application can be moved to testing,to staging and to production environment across different infrastructures. A Docker image packages the application along with its environment specific configuration dependencies.
To create a VM, the physical hardware needs to be prepared, the Hypervisor need to be installed, and the application need to be deployed on the guest OS. Even with Continuous Integration (CI) processes in place, it would take around 10 minutes to deploy an application on a VM. Docker saves this time as the deployment of a Docker image will happen in few seconds. Further, it typically takes 30-45 seconds to start a Virtual Machine it takes 5-10 seconds to stop the VM. On the contrary, it just takes less than 50 milliseconds to stop and start a Docker container.
- Portability across cloud layers:
Containers are portable. Hence, applications can be seamlessly ported across infrastructure and platform clouds.
- Pack more containers on a single server:
Containers share the same host resources though each container runs in its own space. Hence, you can run multiple containers on a single host. By packing more containers within a single server (or even a VM), you can improve the cloud efficiency and dramatically reduce the cost.
Workflow for building and shipping containers:
Step 1:Create the Docker image of an application using the Docker file. A Docker file is a list of instructions i.e., base OS definition, application binaries, libraries and dependencies. Executing the Docker file will build a Docker image. Every Docker image starts with a base image that contains the bare minimum OS that interacts with the kernel.
Sample Docker file which pulls base Ubuntu OS image from bluemeric docker repository and installs redis-server
Docker file (Save the below commands in a file named as “Dockerfile” :
#Pull base image Ubuntu FROM bluemeric/ubuntu #Install redis-server RUN apt-get update RUN apt-get install redis-server -y CMD ["redis-server"]
To create the Docker image using the “Dockerfile” that we have created above, execute this command in the same location where the Dockerfile exists
docker build -t <username>/<repository name>:<tag> .
Step 2: Push this Docker image to Docker hub. Docker hub is a SaaS (Software as a Service) portal for storing and retrieving Docker images. Docker hub is similar to Github and it has public and private repositories. Private repository images are restricted through authentication and authorization mechanisms and are usually shared within a team. Public repository images can be accessed by anyone.
Command to push the Docker image that we have created above is :
docker push <username>/<repository name>:<tag>
Step 3: To deploy the application, the Docker image needs to be pulled from the Docker hub and deployed onto a target machine.
Command to pull the Docker image that we have created above is :
docker pull <username>/<repository name>:<tag>
To deploy the Docker image as container, execute this command :
docker run -d --name <container-name> -p <ports> <image name>