Exploring the Lightweight Advantage: Docker Containers vs. Virtual Machines

Exploring the Lightweight Advantage: Docker Containers vs. Virtual Machines

ยท

5 min read

Introduction:

In the ever-evolving landscape of software development and deployment, two technologies have emerged as key players: Docker containers and Virtual Machines (VMs). Both serve the purpose of isolating applications and their dependencies, but they do so in different ways.

In this blog, we will delve into the characteristics of Docker containers and VMs, highlighting the lightweight nature of Docker containers and providing hands-on proof to support this claim.

Understanding Virtual Machines:

A Virtual Machine is a software-based emulation of a physical computer. It operates as an independent instance with its own operating system (OS) and resources, such as CPU, memory, and storage. VMs rely on a hypervisor to manage multiple instances on a single physical host.

This approach ensures isolation but comes with a cost in terms of resource overhead and slower performance due to the need to run a full operating system for each VM.

Defining Docker Containers:

Docker containers, on the other hand, provide a more lightweight alternative. Docker is a containerization platform that allows applications and their dependencies to be packaged into a single, portable container.

Unlike VMs, containers share the host OS kernel, which significantly reduces resource overhead. Containers are quick to start, consume fewer resources, and offer consistent behavior across different environments.

Why Docker Containers are Lightweight:

  • Shared Kernel: Docker containers share the host OS kernel, eliminating the need for a separate OS for each application instance, making them more efficient in resource utilization.

  • Reduced Overhead: Containers exclude the need for a hypervisor, reducing the overhead associated with running multiple operating systems simultaneously.

  • Faster Deployment: Docker containers can be spun up or down in seconds, providing faster deployment and scaling compared to VMs.

Hands-On Proof:

Prerequisites:

  • Terminal

  • AWS services (EC2 instances)

  • Basic Docker Knowledge

Step 1: Launch an EC2 Instance

Begin by creating an EC2 instance of type t2.micro. If you're unsure, refer to my earlier article on creating an EC2 instance.

Step 2: SSH Into Your EC2 Instance

Use the following command to SSH into your EC2 instance:

ssh -i <your-key.pem> ec2-user@<your-ec2-instance-ip>

Step 3: Elevate to Root User

Switch to the root user to gain elevated privileges:

sudo su -

Step 4: Install Docker

Install Docker on your EC2 instance (ubuntu) with the following command:

apt install docker.io

Step 5: Check Virtual Machine Information

Navigate to the EC2 terminal and run the following commands to gather information about your virtual machine:

To determine the type of VM:

uname -a

This command provides detailed information about the system, including the machine architecture, kernel version, and more.

To get the type of OS on your virtual machine:

cat /etc/os-release

This command displays information about the operating system, including its name, version, and ID. It's a comprehensive way to understand the OS running on your EC2 instance.

Step 6: Create a Container Named c1 with CentOS Image

Use the following command to create a Docker container named c1, based on the CentOS image:

docker run -itd --name c1 centos

Explanation of the command:

  • docker run: Initiates the process of creating and running a Docker container.

  • -itd: Combines three flags:

    • -i (interactive): Keeps STDIN open even if not attached.

    • -t (tty): Allocates a pseudo-TTY for the container.

    • -d (detach): Runs the container in the background (detached mode).

  • --name c1: Assigns the name "c1" to the container for easy reference.

  • centos: Specifies the base image for the container, in this case, CentOS.

This command creates a detached container named "c1" based on the CentOS image. It allows you to interact with the container, and the container will continue running in the background.

To check the container status, run the below command

Step 7: Check Processes Running on the Virtual Machine (EC2 Instance)

To examine the processes running on your virtual machine (EC2 instance), use the following command:

ps -ef | wc -l

Explanation of the command:

  • ps -ef: Lists information about all processes running on the system in a detailed format.

  • |: Redirects the output of the ps command to the next command.

  • wc -l: Performs a word count on the output, specifically counting the number of lines.

This command provides a count of all processes running on your virtual machine. It's a quick way to gauge the level of activity and the number of processes in your EC2 instance.

To see details of all 112 running processes on your virtual machine (EC2 instance), execute the following command

ps -ef

This command provides a comprehensive list of all processes running on the system, including details such as process ID (PID), user, CPU usage, memory usage, and more. It offers a more in-depth view of the currently active processes on your EC2 instance.

Step 8: Check Processes Running on Docker Container c1

To view all processes running on your Docker container named c1, use the following command:

docker exec c1 ps -ef

Explanation of the command:

  • docker exec c1: Executes a command within the running container named c1.

  • ps -ef: Lists information about all processes running inside the container.

This command allows you to inspect the processes running specifically within the Docker container named c1. It provides details similar to the ps -ef command on the host machine but focuses on processes within the container environment.

Conclusion: Unveiling the Lightness of Containers

The virtual machine, laden with a fully-fledged operating system, boasts over 112 processes, whereas its container counterpart operates with just 2 processes.

VMs encapsulate an entire operating system, complete with unnecessary processes, while containers eliminate dependencies and retain only essential binaries for streamlined functionality.

The era of lightweight, adaptable computing is here, and containers are at the forefront of this transformative journey.

Thanks for joining this exploration! May your code be clean, your containers be light, and your tech journey be filled with discoveries. Until our next adventure, happy coding! ๐Ÿš€โœจ

ย