Docker Concepts

 

๐Ÿš€ Containers and Why Use Containers? 


๐Ÿง‘‍๐Ÿ’ป Imagine This Scenario 

You're building a web app that has:

  1. ✅ A React frontend (built with Node.js)

  2. ✅ A Python API (backend)

  3. ✅ A PostgreSQL database (for data)

To work on this app, you need to install:

  • Node.js

  • Python

  • PostgreSQL

But here comes the problem...


๐Ÿ˜ฃ Common Developer Problems

  • What if your versions of Node, Python, or Postgres are different from your teammates?

  • What if your machine already has a conflicting version installed?

  • How do you make sure CI/CD pipelines or production servers run the exact same setup?


๐Ÿ’ก Solution: Use Containers

A container is a lightweight, isolated environment that includes everything an app needs to run — code, tools, libraries, runtime, etc.


๐Ÿงฑ Think of Containers Like Boxes:

Each part of your app runs in its own container, like this:

ComponentContainerWhat's Inside?
React Frontend๐ŸŸฆ Box ANode.js, your frontend code
Python API๐ŸŸง Box BPython, your API code
PostgreSQL DB๐ŸŸฅ Box CPostgreSQL database, pre-configured

๐ŸŽฏ Why Containers Are Awesome

FeatureWhat it Means
Self-containedEverything needed is inside the container — no external installs needed
๐Ÿ” IsolatedChanges in one container don't affect others or your system
๐Ÿ”„ IndependentYou can stop, restart, or delete containers without breaking anything else
๐ŸŒ PortableWorks the same way anywhere — your PC, your teammate’s PC, or the cloud

๐Ÿ†š Containers vs. Virtual Machines (VMs)

FeatureVirtual MachinesContainers
๐Ÿง  Runs entire OSYes — full OS in each VMNo — just runs the app
๐Ÿ’พ SizeHeavy — uses more memory & storageLight — small and fast
๐Ÿงฑ Isolation levelStrong (via separate OS)Strong (via process isolation)
๐Ÿš€ SpeedSlower startupStarts in seconds

๐Ÿ“Œ Bottom line: Containers are faster and lighter than VMs for running individual applications.


๐Ÿ› ️ Real-World Use: Containers + VMs Together

In the cloud, servers are usually VMs. But inside those VMs, we run containers to:

  • Run multiple apps efficiently

  • Save money by using fewer resources

  • Keep everything organized and isolated


✅ Recap

  • Containers are like mini-computers that run specific parts of your app.

  • They solve problems with version mismatches, system conflicts, and scalability.

  • They are fast, portable, and reliable.

  • Great for development, testing, and production

The kernel is like a traffic controller. It makes sure:

  • Programs get time to run on the CPU

  • Memory is used properly

  • Files are read/written correctly

  • Input/output devices (like keyboards, disks) work smoothly


๐Ÿง  Key Responsibilities of a Kernel:

FeatureWhat it does
๐Ÿงฎ Process ManagementStarts/stops programs, schedules them on the CPU
๐Ÿ’พ Memory ManagementAllocates and manages RAM for each program
๐Ÿ—ƒ File SystemHandles reading/writing files and folders
๐Ÿ”Œ Device ManagementTalks to devices like keyboard, mouse, hard disk, etc.
๐Ÿ” Security & AccessControls which program can access what resources

๐Ÿ“ฆ Types of Kernels:

  1. Monolithic Kernel – Everything runs in one big chunk (e.g., Linux)

  2. Microkernel – Only the basics are in the kernel; others run as services (e.g., Minix)

  3. Hybrid Kernel – Mix of both (e.g., Windows, macOS)


๐Ÿ“ฆ What Are Container Images? 


๐Ÿง  First, Let’s Understand the Problem

When you run a container (like a small app box), you may wonder:

"Where does it get all the files, tools, and settings it needs to work?"


๐Ÿ’ก Answer: From a Container Image

A container image is a pre-packaged set of everything needed to run your app, including:

  • Code

  • Tools (like Python, Node, PostgreSQL, etc.)

  • Libraries

  • Configuration files


๐Ÿ“Œ Real-Life Examples:

  • A PostgreSQL image includes:

    • The PostgreSQL database software

    • Config files

    • Any required tools to run it

  • A Python web app image includes:

    • The Python interpreter

    • Your app's code

    • All Python packages your app depends on


๐Ÿ” Key Concepts of Images (Explained Simply)

1. ๐Ÿ“• Images Are Immutable

Once an image is built, you cannot change it directly.

  • If you need to change something, you create a new image version or add a new layer on top.


2. ๐Ÿงฑ Images Are Made of Layers

Think of an image like a stack of layers — each layer adds or modifies files.

  • Example:

    • Layer 1: Base Python

    • Layer 2: Install Flask

    • Layer 3: Copy your app code

    • Layer 4: Add environment variables

Each layer builds on top of the previous one.


๐Ÿ”จ How You Use This in Real Projects

Step-by-Step Example: Python Web App

  1. Start from a base image (like python:3.9)

  2. Install dependencies (like Flask or FastAPI)

  3. Add your app code

  4. Set up commands to run the app

This process creates a custom image for your app, which anyone can use to run your app the same way — anywhere.


✅ Why This is Useful

BenefitExplanation
๐ŸŽฏ Focus on your appDon’t worry about Python, Node, etc. — it’s already in the image
๐Ÿ“ฆ ReusableShare the same image with your team, CI/CD, and production
๐Ÿ”’ Safe & StableSince images don’t change, you always get consistent results
๐ŸŒ PortableRun your image anywhere — your laptop, cloud, or CI server

๐Ÿ“ค Where Do You Store and Share Container Images?

Now that you know what a container image is, the next question is:

๐Ÿง  Where do I store my images and how can I share them with others or use them on another machine?


๐Ÿข Step 1: Understand the Need for an Image Registry

You can store images on your local machine, but that only works for you.

If you want to:

  • Use the same image on another computer

  • Share it with your team

  • Deploy it to production or cloud

๐Ÿ‘‰ You need to push the image to a central location.


๐ŸŒ Step 2: What is an Image Registry?

An image registry is like an online storage center for container images.

It allows you to:

  • Upload (push) your images

  • Download (pull) them on any system

  • Share and organize your images


๐Ÿ“Œ Popular Container Registries

RegistryTypeUse Case
Docker HubPublicMost common, used by default
Amazon ECRPrivateFor AWS users
Azure Container Registry (ACR)PrivateFor Azure users
Google Container Registry (GCR)PrivateFor Google Cloud users
Harbor, GitLab, ArtifactoryPrivate/localFor companies/self-hosting

๐Ÿ—‚️ Step 3: Registry vs. Repository — What's the Difference?

TermMeaningAnalogy
RegistryThe overall storage system for images (like Docker Hub or ECR)A library building
RepositoryA collection of related images in the registry (e.g., myuser/myapp)A bookshelf inside it
ImageA specific version of your container app (e.g., myapp:v1.0)A book on the shelf

๐Ÿ“˜ Example:
In Docker Hub, myuser/myapp:latest

  • Docker Hub = Registry

  • myuser/myapp = Repository

  • :latest = Specific image tag/version


✅ Summary of Steps

  1. ๐Ÿ› ️ Build your container image locally

  2. ๐Ÿ” Login to your image registry (e.g., docker login)

  3. ๐Ÿ“ฆ Tag the image correctly (e.g., docker tag)

  4. ๐Ÿ“ค Push it to the registry (docker push)

  5. ๐ŸŒ Anyone can pull the image using the image name (if public or with permission)


๐Ÿš€ Getting Started with Docker Compose:

If you've been working with Docker, chances are you've started with single-container applications. But what happens when your app grows? Maybe it needs a database, a message queue, or a caching layer like Redis. Should you put everything in one big container? Definitely not.

Let’s explore the right way to run multi-container applications using Docker Compose.


๐Ÿง  Why Not One Big Container?

One best practice for containers is:
⚠️ “Each container should do one thing and do it well.”

That means:

  • A Node.js server should not contain your MySQL database.

  • A React app should not be bundled with your Redis.

Managing multiple containers manually using docker run commands quickly becomes a mess — with flags, network settings, and ports. That’s where Docker Compose shines.


๐Ÿ“ฆ What is Docker Compose?

Docker Compose lets you:

  • Define multi-container applications in a single compose.yaml file.

  • Easily manage all services with simple commands like up and down.

  • Share configuration with your team (just one file to clone).

Think of it as a blueprint for running multiple containers together, easily.


๐Ÿ“ Dockerfile vs Compose File

  • ๐Ÿงฑ Dockerfile: Defines how to build an image.

  • ๐Ÿงฌ Compose file (YAML): Defines how to run one or more containers using built images.

They work together: A Compose file may use a Dockerfile to build an image.


๐Ÿ› ️ Let’s Build Something: A To-Do List App with Node.js + MySQL

In this hands-on, we’ll:

  • Clone a sample app

  • Run it using Docker Compose

  • Explore the containers, network, and volume

  • Tear everything down cleanly


✅ Step 1: Install Docker Desktop

Install Docker Desktop from:
๐Ÿ‘‰ https://www.docker.com/products/docker-desktop/


✅ Step 2: Clone the App

git clone https://github.com/dockersamples/todo-list-app cd todo-list-app

Inside this folder, you'll see a compose.yaml file. This file describes:

  • The Node.js app service

  • The MySQL service

  • Network and volume configuration

Explore the file to see how everything fits together.


✅ Step 3: Run the Application

docker compose up -d --build

You’ll see something like:

[+] Running 5/5 ✔ Network todo-list-app_default Created ✔ Volume "todo-list-app_todo-mysql-data" Created ✔ Container todo-list-app-app-1 Started ✔ Container todo-list-app-mysql-1 Started

✅ Step 4: Open the App

Go to your browser and open:

๐Ÿ”— http://localhost:3000

You’ll see the to-do app running. Add, complete, or delete tasks!


✅ What Just Happened?

  • ⬇️ Images for node and mysql were pulled

  • ๐ŸŒ A network was created

  • ๐Ÿ’พ A volume was created for MySQL data persistence

  • ๐Ÿงฑ Two containers were started and linked

  • ๐ŸŽฏ Port 3000 exposed the app to your browser


๐Ÿงน Step 5: Stop and Clean Up

To stop and remove everything:

docker compose down

You'll see:

Container todo-list-app-app-1 RemovedContainer todo-list-app-mysql-1 RemovedNetwork todo-list-app_default Removed

๐Ÿงฝ Want to Remove the Volume Too?

docker compose down --volumes

Output:

Volume todo-list-app_todo-mysql-data Removed

Volumes are not deleted by default in case you want to restart the stack and keep your data.

Comments