What is Docker?
Hi, I’m Kishen Deemud. In this article I will give you an introduction about Docker.
Docker is a platform for building, running and shipping applications in a consistent manner. So if your application works on your development machine, it can run and function the same way on other machines. If you have been developing software for a while you've probably come across this situation where your application works on your development machine but doesn't somewhere else. Can you think of the reasons why this happens.
This can happen if one or more files are not included as part of your deployment, so your application is not completely deployed. It's missing something. This can also happen if the target machine is running a different version of some software that your application needs. Let's say your application needs node version 14, but the target machine is running node version 9. This can also happen if the configuration settings, like environment variables are different across these machines. And this is where docker comes to the rescue.
With docker we can easily package up our application with everything it needs, and run it anywhere on any machine with docker. So if your application needs a given version of node and mongodb, all of these will be included in your applications package. Now you can take this package and run it on any machine that runs docker. So if it works on your development machine, it's definitely going to work on your test and production machines. If someone joins your team, they don't have to spend half a day or so setting up a new machine to run your application. They don't have to install and configure all these dependencies. They simply tell docker to bring up your application and docker itself will automatically download and run these dependencies inside an isolated environment called a container.
And this is the beauty of docker, this isolated environment allows multiple applications use different versions of some software side by side. So one application may use node version 14, another application may use node version 9. Both these applications can run side by side on the same machine without messing with each other. So this is how docker allows us to consistently run an application on different machines.
Now there is one more benefit here. When we're done with this application and don't want to work on it anymore, we can remove the application and all its dependencies in one go. Without docker as we work on different projects, our development machine gets cluttered with so many libraries and tools that are used by different applications. And then after a while we don't know if we can remove one or more of these tools, because we're always afraid that we would mess up with some application. With docker we don't have to worry about this. Because each application runs with it's dependencies inside an isolated environment. We can safely remove an application with all its dependencies to clean up our machine. So in a nutshell, docker helps us consistently build, run and ship our applications.
What is Container?
A container is an isolated environment for running an application. Now one of the questions that often comes up is, how are containers different from virtual machines.
A virtual machine as the name implies is an abstraction of a machine or physical hardware. So we can run several virtual machines on a real physical machine. For example, we can have a mac, and on this mac we can run two virtual machines. One running windows the other running linux. How do we do that, using a tool called hypervisor. In simple terms a hypervisor is software we use to create and manage virtual machines. There are many hypervisors available out there like,
- VirtualBox
- VMware
Which are cross-platform, so they can run on windows, mac os and linux. And hyper-v which is only for windows. So with a hypervisor, we can manage virtual machines.
What is the benefit of building virtual machines?
For software developers, we can run an application in isolation inside a virtual machine. So on the same physical machine we can have two different virtual machines, each running a completely different application and each application has the exact dependencies it needs. So application 1 may use node version 14 and mongodb version 4, while application 2 may use node version 9 and mongodb version 3. All these are running on the same machine but in different isolated environments. That's one of the benefits of virtual machines.
But there are a number of problems with this model. Each virtual machine needs a full copy of an operating system that needs to be licensed, patched and monitored. And that's why these virtual machines are slow to start, because the entire operating system has to be loaded just like starting your computer. Another problem is that, these virtual machines are resource intensive. Because each virtual machine takes a slice of the actual physical hardware resources like cpu, memory and disk space. So if you have 8 gigabytes of memory, that memory has to be divided between different virtual machines. Of course we can decide how much memory to allocate to each virtual machine, but at the end of the day we have a limit in terms of the number of VMs we can run on a machine. Usually a handful, otherwise we're going to run out of hardware resources.
Differences between Containers and Virtual Machines
Let's talk about containers containers give us the same kind of isolation. So we can run multiple applications in isolation, but they're more lightweight. They don't need a full operating system. In fact all containers on a single machine share the operating system of the host. So that means we need to license, patch and monitor a single operating system. Also because the operating system has already started on the host, a container can start up pretty quickly. usually in a second, sometimes less. And also these containers don't need a slice of the hardware resources on the host. So we don't need to give them a specific number of cpu cores or a slice of memory or disk space. So on a single host, we can run tens or even hundreds of containers side by side. So these are the differences between containers and virtual machines.
Architecture of Docker
Let's talk about the architecture of docker. So you understand how it works. Docker uses a client server architecture. So it has a client component that talks to a server component using a restful API.
The server also called the docker engine, sits on the background and takes care of building and running docker containers. But technically a container is just a process like other processes running on your computer. But it's a special kind of process. Unlike virtual machines containers don't contain a full-blown operating system. Instead all containers on a host, share the operating system of the host. Now more accurately all these containers share the kernel of the host.
What's a kernel, a kernel is the core of an operating system. It's like the engine of a car. It's the part that manages all applications as well as hardware resources like memory and cpu. Every operating system has its own kernel or engine. And these kernels have different APIs. That's why we cannot run a windows application on linux. Because under the hood, this application needs to talk to the kernel of the underlying operating system. So that means, on a linux machine we can only run linux containers, because these containers need linux. On a windows machine however we can run both windows and linux containers. Because windows 10 is now shipped with a custom built linux kernel. This is in addition to the windows kernel, that's always been in windows. It's not a replacement. So with this linux kernel now we can run linux applications natively on windows. So on windows we can run both linux and windows containers. Windows containers share the windows kernel and linux containers share the linux kernel. Now what about mac os, mac os has its own kernel which is different from linux and windows kernels and this kernel does not have native support for continuous applications. So docker on mac uses a lightweight linux virtual machine to run linux containers.
So I hope you understand my explanation about Docker. Thank you!
Useful š
ReplyDelete