4.5.11 · HinglishSoftware Engineering

Docker — images, containers, Dockerfile, docker-compose

2,003 words9 min readRead in English

4.5.11 · Coding › Software Engineering


Docker exist kyun karta hai?

VMs (Virtual Machines) kyun nahi use karte? Ek VM poori hardware + ek full guest OS ko virtualize karta hai (gigabytes, slow boot). Ek container host ka Linux kernel share karta hai aur sirf userspace ko virtualize karta hai (megabytes, milliseconds mein boot). Bahut kam cost mein isolation milti hai.

Figure — Docker — images, containers, Dockerfile, docker-compose

Core objects kya hain?


Layer system kaise kaam karta hai (aur kyun yeh clever hai)

Images bottom-up build hoti hain. Socho jaise har instruction ek stack par ek transparent sheet add kar raha ho:

[ writable container layer ]   ← sirf yahi read/write hai, har container ke liye
---------------------------
[ layer 4: COPY app code   ]
[ layer 3: pip install deps]   ← read-only, containers/images ke beech SHARED
[ layer 2: apt-get python  ]
[ layer 1: FROM ubuntu     ]   ← base

Ek real Dockerfile, line by line

FROM python:3.12-slim          # base image (ek chhota Debian + Python)
WORKDIR /app                   # /app mein cd (agar na ho toh create hota hai)
COPY requirements.txt .        # sirf manifest copy karo (cache-friendly)
RUN pip install -r requirements.txt   # deps install karo -> cached layer
COPY . .                       # ab source code copy karo
EXPOSE 8000                    # port document karta hai (publish NAHI karta)
CMD ["python", "app.py"]       # container start hone par default process

Essential commands (HOW aap actually isko drive karte ho)

docker build -t myapp:1.0 .      # '.' mein Dockerfile se image build karo
docker run -d -p 8080:8000 myapp:1.0   # -d detached, host:container port map
docker ps                        # running containers list karo
docker exec -it <id> bash        # running container ke andar shell kholo
docker logs <id>                 # container ka stdout/stderr dekho
docker stop <id> && docker rm <id>     # pehle stop phir remove karo
docker images                    # images list karo
docker volume create data        # persistent storage

docker-compose: multiple containers orchestrate karna

# docker-compose.yml
services:
  web:
    build: .                 # local Dockerfile se build karo
    ports:
      - "8080:8000"
    depends_on:
      - db                   # pehle db start karo
    environment:
      - DATABASE_URL=postgres://db:5432/mydb
  db:
    image: postgres:16
    volumes:
      - pgdata:/var/lib/postgresql/data   # restarts ke baad bhi DB persist karo
volumes:
  pgdata:                    # named volume container deletion ke baad bhi survive karta hai

Recall Feynman: ek 12-saal ke bache ko samjhao

Socho tum ek cake bana rahe ho ek recipe card se. Recipe card image hai — yeh kabhi nahi badlti aur tum isse dost ko copy kar sakte ho. Jab tum actually bake karte ho aur cake table par hota hai, woh container hai — tum use kha sakte ho (use kar sakte ho) aur usse mess bhi kar sakte ho. Dockerfile woh hai jab tum recipe step by step likhte ho. docker-compose ek party menu hai: woh kehta hai "cake, juice, aur pizza saath banao, aur unhe ek hi table par rakhو taaki woh ek doosre ko cheezein pass kar sakein." Agar tum cake phenko, recipe card phir bhi safe hai — isliye tum hamesha ek aur identical cake bana sakte ho.


Flashcards

Docker image aur container mein kya fark hai?
Image ek immutable read-only template (layers) hai; container us image ka running instance hai jisme ek thin writable layer add hoti hai.
Docker VM ki tarah guest OS ki jagah host kernel kyun use karta hai?
Lightweight rehne ke liye — containers namespaces/cgroups ke zariye sirf userspace virtualize karte hain, isliye woh MBs ke hote hain aur milliseconds mein boot karte hain, jabki VMs GBs ke hote hain aur slow boot hoti hai.
-p 8080:8000 mein kaun sa number host hai aur kaun sa container?
Host:Container → 8080 host port hai, 8000 container port hai.
COPY . . se pehle requirements.txt copy karke deps install kyun karte hain?
Expensive dependency-install layer ko cached rakhne ke liye; pehle code copy karna har code change par us cache ko invalidate kar deta.
Dockerfile mein RUN aur CMD mein kya fark hai?
RUN build time par execute hota hai aur image mein bake hota hai; CMD container start par run hone wala default command set karta hai.
CMD aur ENTRYPOINT mein kya fark hai?
ENTRYPOINT fixed executable hai; CMD default arguments deta hai jo user docker run par override kar sakta hai.
docker-compose mein web service db service tak kaise pahunchti hai?
Compose ek shared network create karta hai aur har service ka naam ek DNS hostname ke roop mein register karta hai, isliye web naam se db se connect karta hai.
Container delete hone par uske filesystem ke andar likhe data ka kya hota hai?
Woh lost ho jaata hai, kyunki woh ephemeral writable layer mein rehta hai; data persist karne ke liye volume use karo.
EXPOSE 8000 actually kya karta hai?
Yeh sirf port document karta hai; yeh usse publish NAHI karta — reach karne ke liye run time par phir bhi -p chahiye.
OOP mein image container ki tarah ___ se ___ jaisi relationship rakhta hai?
Class se object (blueprint vs instance).

Connections

  • Virtual Machines vs Containers
  • Linux Namespaces and cgroups
  • CI-CD Pipelines — images woh build artifact hain jo deploy hoti hain
  • Kubernetes — containers ko scale par orchestrate karta hai (compose uska single-host cousin hai)
  • Microservices Architecture — har service apne container mein
  • Environment Variables and 12-Factor App
  • Volumes and Persistent Storage

Concept Map

solves

lighter than

shares

isolated by

runs full

read-only template of

instantiated as

adds

lost unless

each instruction builds

enables

orchestrates many

is like a

Docker

Works on my machine problem

Virtual Machine

Container

Host Linux kernel

Namespaces + cgroups

Guest OS on hypervisor

Image

Stacked layers

Writable top layer

Volume

Dockerfile

Caching + sharing

docker-compose.yml

Class blueprint