Docker for Beginners - The Prerequisites

Docker for Beginners - The Prerequisites

Understanding Docker in a Fun Way

Introduction

Before diving into Docker, let's consider why it exists. To understand this, we need to first grasp the concept of a virtual machine (VM). We'll explore how VMs solved a problem and how their solution eventually became part of the problem itself. Let's begin with a relatable analogy.

Imagine you're a bookseller and publisher attending an international book fair to reach a wider audience. Setting up your stall throws unexpected challenges your way:

  • The unpredictable weather, from scorching sun to sudden rain, disrupts sales and promotions.

  • Different regions at the fair use varied payment systems, leading to transaction complications.

  • Language barriers and incompatible communication methods hinder collaboration with customers and neighboring stalls.

While you manage to sell some books, the profit doesn't justify the high overhead in logistics, operations, and manpower. You wanted to expand your business, but these complexities made it very slow to progress.

The next year, you attend a more community-driven, well-organized book fair. This time, things are different:

  • The fairground offers protection against any weather woes.

  • A unified payment system simplifies transactions.

  • Clear guidelines, local support, and efficient communication channels ease interaction with other vendors and authorities.

This organized experience translates to significant profits, leaving you satisfied after accounting for logistics, operations, and manpower costs. Now, you can truly focus on what matters - expanding your business!

If you think closely, your first book fair experience mirrors a virtual machine. We can create isolated environments on our computers (bookstall in the fair), like separate rooms, to build, run, debug, and test software programs with specific needs without affecting other programs on the host computer.

Think of VMs as running different operating systems on the same machine, but each "room" (called a guest) has its own operating system(kernel), tools, and libraries, separate from the main computer (called the host). This isolation is great for testing and development, but like your chaotic first fair, VMs can be heavy to set up, resource-hungry, and difficult to scale or move around.

Now, let's revisit your second, well-organized book fair experience. Everything was in place, eliminating the headaches you faced before. With similar costs in logistics, operations, and manpower, you turned a profit this time. No weather worries, efficient support, unified payments, and smooth communication allowed you to focus solely on your business, leading to success and growth.

Docker is also a software that creates isolated environments within host computers. Here's the key difference: Docker avoids the hefty overhead of VMs and provides you with almost everything you need, like a pre-built, organized booth at the fair. This allows you to concentrate on building great software, not fixing your development environment.

What is a Virtual Machine

representation of virtual machine

Photo by Brian Kostiuk on Unsplash

Imagine you're developing software with Python, MySQL, and Nginx, each requiring specific versions. Now, you have to work on the same tech stack but with different versions on your computer. One solution might be to buy a new computer with all the necessary hardware and install the additional dependencies. However, this approach is expensive, as we can't keep buying hardware whenever we need to run different dependencies. In such cases, virtual machines (VMs) come to the rescue. VMs help create entirely isolated environments within a computer, allowing the development of software with specific dependencies. You would create a virtual machine with a separate operating system, installing all necessary dependencies without affecting your host computer. This is how VMs solved the problem of acquiring separate hardware.

Recall that I mentioned VMs solved a problem, but the solution itself became part of the problem. Now, let's delve into how VMs are created to understand it better.

Virtual machines encompass everything a regular computer has—the only distinction being that VMs don't require separate hardware, such as CPU, RAM, ROM, Motherboard, etc. Instead, VMs utilize the same hardware as the host (i.e., the original computer).

While creating a virtual machine, resources are allocated for that VM. You inform the host computer about the amount of RAM, storage, etc., the virtual machine or the guest would use. Once allocated, these resources become exclusive to the virtual machine, unavailable for the main computer. What's the issue, you ask? This is the solution that turned into a problem—a waste of resources. Predicting the exact resources needed to run the required software in the VM is challenging, leading to resource allocation based on guesswork. This results in potential wastage, as the VM doesn't always utilize all allocated resources, leaving some idle and blocked.

Furthermore, initiating an operating system in a VM takes time, causing issues when an instant isolated environment is needed. Additionally, integrating the same tools with different versions requires creating a separate VM—a complex, time-consuming, and resource-intensive process. This is how VMs addressed the problem of avoiding separate hardware but inadvertently created challenges for developers in terms of accessibility, usage, and resource management.

But hey, Innovation brings us Docker, addressing VM challenges and more. Excited? Keep reading!

What is Docker

image of docker logo

Photo by Rubaitul Azad on Unsplash

Congratulations on reaching this point! You now have a good grasp of what virtual machines are and their shortcomings. Now, let's meet their efficient cousin: Docker!

Docker is software that assists in creating isolated environments, much like a VM. The key difference is that Docker doesn't require a separate operating system within each environment to function; instead, it abstracts the host OS, allowing the creation of multiple isolated spaces to develop your software independently of other environments.

Let's illustrate this with an analogy. Suppose you want to start a food chain business. Establishing a new restaurant involves setting up the kitchen, dining hall, restroom for customers, and promotions—a considerable overhead. Now, imagine customizing your minivan and selling fast food at a park. You attract ready-made customers in the park while leveraging public facilities(restrooms, water). This setup significantly reduces the overhead of starting a new food business, utilizing already available resources. This parallels Docker's approach—it uses the existing operating system on the host computer and doesn't require a separate one to create isolated environments (in Docker's language, we call these isolated spaces "containers"), unlike virtual machines.

This lightweight approach gives Docker significant advantages over VMs. Firstly, it's incredibly fast, spinning up containers in milliseconds compared to VMs' sluggish boots. Secondly, you can create as many containers as needed, each fully isolated yet easily communicating with others if needed – a feature often challenging with VMs.

Use Cases - Virtual Machine vs Docker

representation of vm vs docker

Photo by Mateusz Wacławek on Unsplash

Having thoroughly explored Docker and virtual machines, let’s now deep dive into their real-world use cases. Before learning something new, understanding why we need to learn it is crucial—it helps establish a strong purpose for learning, providing a clear and concise roadmap.

For this, we will evaluate case studies of an e-commerce website during a flash sale in a festive season.

Consider this scenario: A popular e-commerce platform is planning for a flash sale during a festive season. Anticipating the predicted traffic, the IT team plans to rapidly scale up their server to handle the excessive load to ensure a smooth user experience.

Now, let's discuss the potential scenarios with VMs and Docker in this context.

With Virtual Machine

  • Provisioning Bottleneck: Booting up new virtual machines even with pre-configured templates can take several minutes, or even hours if something goes wrong. This could lead to a delayed launch or an unexpected user experience during peak hours.

  • Resource Overhead: We know that each VM requires its own operating system, memory, and storage to operate smoothly. This can quickly become expensive and resource-intensive for any sudden large-scale deployments.

  • Management Complexities: Managing multiple virtual machines with their independent configuration and dependencies can become a challenge for a smooth workflow.

  • Monolithic Architecture: Creating microservices with VMs is a nightmare. Traditionally, we develop monolithic architecture with VMs.

With Docker

  • Agile Development: Launching new Docker containers with specific dependencies takes seconds, allowing instant scaling to meet demand. This allows us to handle any sudden surge in traffic smoothly and efficiently.

  • Resource Efficiency: As we have already discussed, Docker shares the host operating system(hence, does not require separate resources like RAM, storage, etc. to operate) significantly reducing extra overhead, leading to lower costs and more optimized infrastructure.

  • Simplified Management: Docker provides a simplified management system to communicate with all the docker containers or instances. Moreover, Docker provides a centralized platform to manage any docker resources like streamlining docker images, distribution, configuration, and updates. This reduces administrative costs and simplifies maintenance.

  • Portability: Have you ever heard the phrase, “It works on my machine!”. virtual machines are the devil that creates compatibility issues with different environments(development, testing, production). Docker containers, on the other hand, can operate on any host environment without any compatibility issues, ensuring consistent application behavior across different environments.

  • Microservices Architecture: Docker facilitates breaking down a monolithic architecture into smaller, independent services, each running in their containers. This enhances scalability on specific services, improves fault tolerance, eliminates single points of failure (SPOF), and makes room for independent development and deployment.

  • Continuous Integration and Continuous Delivery: Docker also helps with CI/CD pipelines automating testing and deployments. This ensures faster developments, quicker bug fixes, and more frequent feature releases.

As we can see, to handle the sudden surge in the traffic in the e-commerce platform, Docker stands out for quickly launching additional containers without creating any overhead to our existing system, allowing us easy and smooth management and quicker CI/CD pipelines in case of failures. Hence, Docker is a clear winner in maintaining a low-cost, agile software development than VMs.

Docker Components

representation of docker components

Photo by Dan Cristian Pădureț on Unsplash

Now, let's take a look into the core machinery of Docker's various services, elevating agile development efficiency. While we'll explore each component in greater detail later, this overview serves as a roadmap for unpacking the Docker ecosystem.

  • Docker Engine: Think of it as the brain of Docker, managing containers and the entire system behind the scenes. It's made up of two parts: a daemon that constantly monitors and runs containers and a flexible API that lets you communicate with the whole Docker ecosystem.

  • Docker Images: Lightweight executable documents containing application code, libraries, and runtime, akin to a Class in Object-Oriented Programming, capable of generating multiple instances.

  • Docker Container: An instance of the Docker image, running in an isolated environment without disrupting other containers or the host’s dependencies.

    In respect of Object Oriented Programming paradigm, think of the Docker container as the instance of the Docker image which contains the exact behavior of the image.

  • Docker Compose: Docker compose is a tool for defining and running multi-container applications, simplifying the creation and management of multiple Docker containers simultaneously.

  • Docker CLI: The command line interface for interacting with Docker, providing enriched and powerful commands to manage the Docker ecosystem

  • Docker Hub: It is a cloud-based registry for sharing, managing, and hosting public and private Docker images. Anyone can host their Docker images as public or private and any public images could be copied to create containers on the go.

  • Docker Network: Docker Network bridges communication between Docker containers, ensuring isolated and secure networking within the Docker environment.

While Docker offers numerous other services, these foundational components provide a solid starting point for exploring the Docker ecosystem.

Conclusion

Docker is a game-changer in agile software development, alleviating numerous challenges throughout the software development life cycle. It has truly simplified the lives of developers.

This narrative aims to highlight Docker's utility, contrasting it with the pre-Docker era using virtual machines. Virtual machines, being party guests bringing their entire house, were humorously described by my professor as the "ultimate RSVP!" Docker kicks those cumbersome guests out! It offers a lightweight, container-based approach, letting us scale effortlessly and collaborate seamlessly.

Stay tuned for the next episode, where we'll explore Docker installation and guide you through creating your first Docker container.

Did you find this article valuable?

Support The Backend Diaries by becoming a sponsor. Any amount is appreciated!