Docker for Beginners Part 1 - Containers and Virtual Machines

Part 1 - Containers and Virtual Machines

Hello!

This is the Part 1 from the Docker series - Docker for Beginners in 8 Parts. In this post we're going to explore the differences between Containers and Virtual Machines!

  • Part 1 - Differences between Containers and Virtual Machines
  • Part 2 - Overview of Docker Installation for Mac and Ubuntu
  • Part 3 - Docker Images and Containers
  • Part 4 - Exploring Docker Images in Details
  • Part 5 - Exploring Docker Containers in Details
  • Part 6 - Building Custom Docker Images with Dockerfile
  • Part 7 - Pushing our Great Docker Image to Docker Hub
  • Part 8 - Keeping MongoDB Data with Docker Volumes

Before jump into hacking Docker, let's explore a few differences between Containers and Virtual Machines. Actually, we should understand what is a Container and what is a Virtual Machine even before compare them.

It's common to compare them and in theory, as Internet always sometimes says, Containers are better than Virtual Machines.

Although you can run your Apps in "almost" the same way in both technologies, they're different and sometimes you can't just compare them, since one can be better than other based on your context. Even more: They can be used together! They're are not even enemies!

Let's jump into the fundamental concepts of both technologies.

Applications Running in a Real Computer

Before starting the comparison, what about get a step back and remember how does a classical application runs in a real computer?

To get a more real example, imagine an application that has 3 main components that should run together:

  • MySQL Database
  • Nodejs Application
  • MongoDB Database

As you can see, we should execute 3 different applications. We can run this applications directly in a real computer, using its Operating System (let's say a Linux Ubuntu) as below:

Notice that:

Server

Is the real physical computer that runs an operating system

Host OS

Is the operating system running upon the server, in this case a Linux Ubuntu

Applications

Are the 3 applications running together in the operating system

But you can fall in the challenge to get these 3 applications running isolated from each other, each with its own operating system. Imagine that:

  • MySQL should run on Linux Fedora
  • Nodejs should run on Linux Ubuntu
  • MongoDB should run on Windows

If we follow the approach above, we can create the next architecture with 3 real computers:

Hmm, that doesn't seems good because it is too heavy. Now we're working with 3 physical machines, each one with its own operating system and besides that they must communicate with each other.

Virtual Machines come to the game to create a better isolated environment without using hundreds of real computers!

Virtual Machines

Long story short:

Virtual Machines emulate a real computer by virtualizing it to execute applications, running on top of a real computer

Virtual Machines can emulate a real computer and can execute applications separately. To emulate a real computer, virtual machines use a Hypervisor to create a virtual computer.

Hypervisor is responsible to create a virtual hardware and software environment to run and manages Virtual Machines

On top of the Hypervisor, we have a Guest OS that is a Virtualized Operating System where we can run isolated applications, called Guest Operating System.

Applications that run in Virtual Machines have access to Binaries and Libraries on top of the operating system.

Let's see a terrible picture designed by me beautiful picture with this architecture:

As you can see from this picture, now we have a Hypervisor on top of the Host OS that provides and manages the Guest OS. In this Guest OS we would run applications as below:

Great! Now the 3 applications can run on the same Real Computer but in 3 Virtualized Machines, completely isolated from each other.

Virtual Machines Good Things

Full Isolation

Virtual Machines are environments that are completely isolated from each other

Full Virtualization

With full virtualization we can have a fully isolated environment, with each Virtual Machine with its own CPU virtualization

Virtual Machines Drawbacks

Heavy

Virtual Machines usually execute in a heavy isolated process, because it needs am entire Guest OS

More Layers

Depending on your configuration, you would have one more layer when your virtual machine doesn't have direct access to the hardware (hosted hypervisor) and it brings to us less performance

Containers

Long story short again:

Containers are isolated processes that share resources with its host and, unlike VMs, doesn't virtualize the hardware and doesn't need a Guest OS

One of the biggest differences between Containers and VMs is that Containers share resources with other Containers in the same host. This automatically brings to us more performance than VMs, since we don't have a Guest OS for each container.

Instead of having a Hypervisor, now we have a Container Engine, like below:

The Container Engine doesn't need to expose or manage Guest OS, therefore our 3 applications would run directly in the Container as below:

Applications in Containers can also access Binaries and Libraries:

Containers Good Things

Isolated process

Containers are environments that will be executed by isolated processes but can share resources with other containers on the same host

Mounted files

Containers allow us to mount files and resources from inside the container to the outside

Lightweight Process

Containers don't run in a Guest OS, so its process is lightweight with a better performance and can start up the container in seconds

Containers Drawbacks

Same Host OS

You can fall in a situation when each application requires a specific OS and it easier to achieve with VMs, since we can have different Guest OS

Security Issues

Containers are isolated process that have direct access to a few important namespaces such as Hostname, Networks and Shared Memory. Your container can be used to do bad things more easily! Of course you can control your root user, create a few barriers but you should worry about it.

Result from the comparison

From this simple comparison you can have thoughts like this:

Hey, Virtual Machines is not for me! It's really heavy, run an entire operating system and I can't pack one hundred apps in seconds!

We can list more problems with Containers and Virtual Machines. Actually the point is:

There is no winner here! There is no better approach if you're just analyzing them in isolation.

You can have a better approach based on a context, a scope, a problem

Actually, Virtual Machines are great and you can even work with Virtual Machines and Containers together! Imagine a situation that:

  • Your production environment uses Windows, but you should have an application that just runs on Linux
  • So, you can have a Virtual Machine to run a Linux distribution
  • Then, you can have Containers running inside this Virtual Machine

So, what is Docker?

What is Docker and why we were talking about Virtual Machines and Containers so far?

Docker is a great technology to build Containers easily.

Where you read Container Engine in the picture, now you can read Docker Engine.

But containers technology is more than a decade old. Google has been working with a thousand of Containers so far.

So, why we're talking about Containers? And why Docker?

Docker is really simple and easy to use and has a great adoption of the Community

Containers are really old, but the way to create containers was really complicated. Docker shows us a new way to think about container creation.

In a Nutshell, Docker is:

  • A container technology written in Go Language
  • A technology that brings to us the facility to start up a container in seconds
  • A technology that has a huge community adoption
  • A technology with its own tool to run multiples containers easily
  • A technology with its own tool to run manage a cluster of containers easily
  • A technology that uses cgroups, namespaces and file systems to create lightweight isolated process

That's it!

You'll get your hands dirty by running Docker containers in the next posts of the series.

But before that, let's just install Docker on your machine in the next post!

I hope that this article would be helpful to you!

Thanks!

Follow us to keep up to date!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Docker for Beginners Tutorial - Containers vs Virtual Machines - Hacking Code

Part 1 - Containers vs Virtual Machines

This is the first Post of the Dockers series! We're going to explore differences between Containers and Virtual Machines!

Docker for Beginners Tutorial - Docker Installation - Hacking Code

Part 2 - Docker Installation on Mac and Ubuntu

Before hacking Docker commands we must install Docker!

Docker for Beginners Tutorial - Docker Images and Containers

Part 3 - Docker Images and Docker Containers

What's the differences between Docker Images and Docker Containers?

Docker for Beginners Tutorial - Docker Images

Part 4 - Docker Images in Details

It's time to explore more about Docker Images!

Docker for Beginners Tutorial - Containers in Details

Part 5 - Docker Containers in Details

It's time to explore more about Docker Containers!

Docker for Beginners Tutorial - Dockerfile - Hacking Code

Part 6 - Custom Docker Images with Dockerfile

Let's build your own custom Docker Image with Dockerfiles

Docker for Beginners Tutorial - Docker Hub

Part 7 - Pushing Docker Images to Docker Hub

Pushing your own great Docker Images to Docker Hub to exposes it to the World!

Docker for Beginners Tutorial - Docker Volumes - Hacking Code

Part 8 - Docker Volumes with MongoDB

Stop losing your data in the Docker Container by using Docker Volumes

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: