Unboxing Containerization: A Journey into the World of Docker

Souvik DeySouvik Dey
4 min read

Understanding containerization

Containerization is a lightweight form of virtualization that allows you to run multiple isolated applications (containers) on a single host, sharing the host's operating system kernel. Containers bundle an application's code, runtime, system tools, libraries, and settings, ensuring that the application runs consistently across different environments. This isolation improves security, makes applications more portable, and simplifies dependency management.

Containers vs. Virtual Machines:

  1. Resource efficiency: Containers are more lightweight compared to VMs because they share the host's operating system kernel instead of running a separate OS for each instance. This results in lower resource consumption, as each container only includes the application and its dependencies. For example, a server running 10 containers with different applications will generally consume fewer resources than a server running 10 VMs, each with its own operating system and application.

  2. Startup time: Containers start up much faster than VMs, as they do not need to boot an entire operating system. This fast startup time is particularly advantageous in situations where applications need to scale quickly in response to demand. For instance, if an e-commerce website experiences a sudden surge in traffic, containers can be spun up rapidly to handle the increased load, ensuring a seamless user experience.

  3. Portability: Containers package an application along with its dependencies and configurations, ensuring that it runs consistently across different environments. This portability simplifies the deployment process and eliminates issues arising from inconsistencies between development, testing, and production environments. For example, a developer can build a containerized application on their local machine and be confident that it will run as expected on any server that supports the container runtime.

  4. Scalability: Containers make it easier to scale applications horizontally by adding more instances to handle increased workloads. Since containers are lightweight and have a fast startup time, it's possible to quickly launch multiple instances of an application in response to demand. For example, a microservices architecture can take advantage of containerization to independently scale each service based on its specific needs, leading to more efficient resource utilization.

  5. Isolation: Containers provide process and filesystem isolation, improving security and allowing multiple applications to coexist on the same host without conflicts. This isolation prevents one application from affecting another, even if they share the same underlying resources. For instance, if two containerized applications require different versions of a library, they can run on the same host without issues, as each container includes the specific library version it needs.

  6. Ecosystem and tooling: The container ecosystem has a wealth of tools and services that make it easier to manage, monitor, and orchestrate containers. These tools, such as Docker, Kubernetes, and container registries, simplify the process of deploying, scaling, and maintaining containerized applications.

Containerization offers several advantages over virtual machines, including resource efficiency, fast startup time, portability, scalability, isolation, and a rich ecosystem of tools and services. These benefits make containerization an increasingly popular choice for developers and organizations looking to streamline their software development and deployment processes.

Enter: Docker

Docker, the most widely used containerization platform, was created by Solomon Hykes and released as an open-source project in 2013. It was initially developed at dotCloud, a Platform-as-a-Service (PaaS) company, which later changed its name to Docker Inc. Docker quickly gained traction in the software industry, thanks to its user-friendly approach to containerization and strong community support. The technology has since become a cornerstone of modern software development, enabling developers to build, package, and deploy applications consistently and efficiently across diverse environments.

It uses a client-server architecture, where the Docker client communicates with the Docker daemon, responsible for building, running, and managing containers.

  1. Install Docker on your system: https://docs.docker.com/engine/install/

  2. Run a simple container:

     docker run --rm -it ubuntu:20.04 /bin/bash
    

    This command downloads the Ubuntu 20.04 image from Docker Hub (if not already present), creates a container, and starts an interactive shell session inside it. The --rm flag ensures the container is removed after exiting the shell.

  3. Explore the container environment:

    • Run ls and ps commands to inspect the container's filesystem and processes.

    • Exit the container by typing exit.

  4. Create a simple Python application:

    • Create a new directory and navigate to it.

    • Create a file called app.py with the following content:

        from flask import Flask
      
        app = Flask(__name__)
      
        @app.route('/')
        def hello():
            return 'Hello, Docker!'
      
        if __name__ == '__main__':
            app.run(host='0.0.0.0', port=5000).
      
  5. Create a Dockerfile in the same directory as app.py:

     FROM python:3.9
    
     WORKDIR /app
    
     COPY requirements.txt requirements.txt
     RUN pip install -r requirements.txt
    
     COPY . .
    
     CMD ["python", "app.py"]
    

    This Dockerfile uses the Python 3.9 image as a base, sets the working directory to /app, installs required packages from requirements.txt, and copies the application code. Finally, it runs app.py as the container's entry point.

  6. Create a requirements.txt file in the same directory with the following content:

     Flask==2.1.1
    
  7. Build the Docker image:

     docker build -t my-python-app .
    
  8. Run the container:

     docker run
    
0
Subscribe to my newsletter

Read articles from Souvik Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Souvik Dey
Souvik Dey

I design and develop programmatic solutions for Problem-Solving.