Skip to main content

Command Palette

Search for a command to run...

Day 19: Docker for Beginners: A Comprehensive Guide

#Day16ofDevOpsBlog

Updated
5 min read
Day 19: Docker for Beginners: A Comprehensive Guide
T

Hey there! 👋 🌟 Welcome to my DevOps journey! 🌟

I'm Tanmaya Arora, an enthusiastic DevOps Engineer. Currently, on a learning adventure, I'm here to share my journey and Blogs about DevOps.

I believe in fostering a culture of resilience, transparency, and shared responsibility. Embracing agility and flexibility, in this adventure let's grow together in this vibrant DevOps space.

Join me in transforming software delivery through collaboration, innovation, and excellence! 🚀🔧💡

🌐 Connect with me for friendly chats, group discussions, shared experiences and learning moments.

Introduction to Virtual Machines and Containers

When starting with Docker, it's important to understand the difference between virtual machines (VMs) and containers. They both enable isolation of applications, but they do so in different ways.

💻 What is a Virtual Machine?

A Virtual Machine (VM) is an emulation of a physical computer. It runs an operating system (OS) and applications, just like a physical computer. VMs use a hypervisor (e.g., VMware, VirtualBox) to manage virtual hardware and allocate resources (CPU, memory, storage) to each VM.

  • Key Characteristics:

    • Each VM has its own OS (Guest OS).

    • Requires significant system resources (e.g., CPU, RAM).

    • Isolated environment, making it secure but resource-intensive.

Since a VM has the above things thus VM's are huge in size, consuming more resources of our system, making it slow as well sometimes.

📦 What is a Container?

A container is a lightweight, standalone executable package that includes everything needed to run an application—code, runtime, libraries, and settings. Unlike VMs, containers share the host OS kernel (containers work on shared OS) , making them more efficient in terms of resource usage because they are not bringing their own OS and are sharing the same OS as that of the host.

  • Key Characteristics:

    • Containers share the host OS kernel.

    • Lightweight and start quickly.

    • Isolated from each other and the host system.

Difference Between Containers and Virtual Machines

FeatureVirtual MachineContainer
OSRuns a full guest OS.Shares the host OS kernel.
Resource EfficiencyHigh resource consumption (CPU, RAM).Lightweight and efficient.
Startup TimeTakes minutes to start.Starts in seconds.
Isolation LevelFull isolation, including the OS.Process-level isolation.
PortabilityLess portable due to OS dependencies.Highly portable.

Introduction to Docker 🐳

What is Docker?

Docker (a container runtime) is an open-source platform that automates the deployment of applications inside containers. Docker provides a standardized environment for developers to build, ship, and run applications.

  • Key Features:

    • Docker enables the packaging of applications with all dependencies, ensuring consistent behavior across different environments.

    • It simplifies the deployment process by eliminating the need to configure the environment manually.

Why Do We Need Docker?

  • Portability: Docker containers can run consistently across different environments (e.g., local development, testing, production).

  • Efficiency: Containers are lightweight and consume fewer resources than traditional VMs.

  • Speed: Containers start up faster, making development and deployment faster.

  • Scalability: Docker makes it easy to scale applications across multiple servers.

Docker Architecture

Docker operates on a client-server architecture, with components like Docker CLI, Dockerd, and Containerd playing crucial roles.

What is Docker Engine?

Docker Engine is the core component of Docker that enables containerization. It consists of the following:

  • Docker CLI: Command-line interface used to interact with Docker.

  • Dockerd (Docker Daemon): The daemon that manages Docker containers, images, networks, and storage volumes.

  • Containerd: A container runtime responsible for managing the container's lifecycle (start, stop, etc.).

🐳 Working with Docker

What is a Dockerfile?

A Dockerfile is a script containing a series of instructions on how to build a Docker image. It defines the environment and steps required to create the containerized application. It has its own specified format of how it is written.

  • Example Dockerfile:

      # Use an official Python runtime as the base image
      FROM python:3.8-slim
    
      # Set the working directory inside the container
      WORKDIR /app
    
      # Copy the current directory contents into the container at /app
      COPY . /app
    
      # Install any required packages specified in requirements.txt
      RUN pip install --no-cache-dir -r requirements.txt
    
      # Define the command to run the application
      CMD ["python", "app.py"]
    

What is a Docker Image?

A Docker image is a read-only template used to create Docker containers. It contains the application code, libraries, dependencies, and environment required to run the application.

  • Docker Images vs. Containers: An image is the blueprint, while a container is a running instance of that image.

Basic Docker Commands

  • Pull an Image:

      docker pull <image_name>
    

    Example:

      docker pull nginx
    
  • List Docker Images:

      docker images
    
  • Build an Image from a Dockerfile:

      docker build -t <image_name> .
    

    Example:

      docker build -t my-python-app .
    
  • Run a Container:

      docker run -d --name <container_name> <image_name>
    

    Example:

      docker run -d --name my-nginx nginx
    
  • List Running Containers:

      docker ps #Lists all running containers
      docker ps -a #Lists all containers even the stopped ones
    
  • Stop a Container:

      docker stop <container_name>
    

    Example:

      docker stop my-nginx
    
  • Remove a Container:

      docker rm <container_name>
    

    Example:

      docker rm my-nginx
    
  • Remove an Image:

      docker rmi <image_name>
    

    Example:

      docker rmi my-python-app
    

Conclusion

Docker is a powerful tool that makes it easy to build, package, and deploy applications consistently across different environments. By using Docker, you can streamline your development workflow, reduce conflicts between development and production environments, and scale your applications effortlessly. With a solid understanding of Docker basics, you’re now ready to dive deeper into containerization and explore advanced features like Docker Compose and Docker Swarm.