Loading
Loading
Docker is a tool that lets you package your application with everything it needs (Python, libraries, configuration) into a Container that runs the same way on any machine.
| Property | Container (Docker) | Virtual Machine | |----------|-------------------|-----------------| | Size | Megabytes (MB) | Gigabytes (GB) | | Start time | Seconds | Minutes | | Resource isolation | Process level | Hardware level | | Operating system | Shares Linux kernel | Full OS | | Performance | Near native | Slower |
Docker Architecture:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Docker Client (docker CLI) โ
โ docker build / docker run / docker push โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Docker Engine (Docker Daemon) โ
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โContainer1โ โContainer2โ โContainer3โ โ
โ โ FastAPI โ โ Redis โ โ Nginx โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Docker Hub / Registry (Image repository) โ
โ hub.docker.com โ python, redis, postgres.. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Image: A read-only template containing everything the app needs.
docker pull python:3.11-slim # download image
docker images # show local images
Container: A running instance of an Image.
docker run python:3.11-slim python --version # run container
docker ps # running containers
docker ps -a # all containers
Volume: Persistent storage outside the Container.
docker run -v /host/data:/app/data myimage # mount directory
# Image management
docker pull nginx # download image
docker build -t myapp:v1 . # build image from Dockerfile
docker push myapp:v1 # push image to registry
docker rmi myapp:v1 # delete image
# Container management
docker run -d -p 8080:8000 myapp # run in background with port mapping
docker stop container_id # stop container
docker rm container_id # delete container
docker logs container_id # view logs
docker exec -it container_id bash # enter container shell
# ู
ุญุงูุงุฉ Docker Concepts ุจู Python
# Docker Architecture Simulation
from dataclasses import dataclass, field
from typing import Optional
import time
import json
# โโโ Docker Data Models โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@dataclass
class DockerImage:
name: str
tag: str
size_mb: int
base: str
layers: list[str]
def full_name(self) -> str:
return f"{self.name}:{self.tag}"
@dataclass
class DockerContainer:
id: str
image: DockerImage
name: str
status: str = "stopped"
port_mapping: dict[int, int] = field(default_factory=dict)
env_vars: dict[str, str] = field(default_factory=dict)
volumes: list[str] = field(default_factory=list)
started_at: Optional[float] = None
def uptime(self) -> str:
if not self.started_at or self.status != "running":
return "N/A"
elapsed = round(time.time() - self.started_at)
return f"{elapsed}s"
class DockerEngine:
"""ู
ุญุงูุงุฉ Docker Engine ุงูู
ุญูู"""
def __init__(self):
self.images: dict[str, DockerImage] = {}
self.containers: dict[str, DockerContainer] = {}
self._counter = 0
def _new_id(self) -> str:
self._counter += 1
return f"c{self._counter:06x}"
def pull(self, name: str, tag: str = "latest") -> DockerImage:
"""docker pull"""
key = f"{name}:{tag}"
if key not in self.images:
# ู
ุญุงูุงุฉ images ู
ุนุฑููุฉ
known = {
"python:3.11-slim": DockerImage("python", "3.11-slim", 130, "debian:slim", ["base", "python", "pip"]),
"python:3.11": DockerImage("python", "3.11", 910, "debian", ["base", "python", "dev"]),
"alpine:3.19": DockerImage("alpine", "3.19", 7, "scratch", ["musl", "busybox"]),
"nginx:alpine": DockerImage("nginx", "alpine", 43, "alpine", ["nginx", "config"]),
"redis:7-alpine": DockerImage("redis", "7-alpine", 28, "alpine", ["redis"]),
}
img = known.get(key) or DockerImage(name, tag, 200, "debian", ["base", "app"])
self.images[key] = img
print(f" ๐ฅ Pulling {key}... ({img.size_mb}MB)")
else:
print(f" โ
{key} ู
ูุฌูุฏ ู
ุญููุงู")
return self.images[key]
def build(self, tag: str, size_mb: int = 180) -> DockerImage:
"""docker build"""
name, t = tag.split(":") if ":" in tag else (tag, "latest")
img = DockerImage(name, t, size_mb, "python:3.11-slim", ["python", "deps", "app"])
self.images[tag] = img
print(f" ๐จ Building {tag}...")
for layer in ["Copying files", "Installing deps", "Running RUN commands", "Setting CMD"]:
print(f" Step: {layer}... โ
")
print(f" โ
Successfully built {tag} ({size_mb}MB)")
return img
def run(self, image_name: str, name: str = "", ports: dict = None,
env: dict = None, detach: bool = True) -> DockerContainer:
"""docker run"""
img = self.images.get(image_name)
if not img:
img = self.pull(*image_name.split(":"))
cid = self._new_id()
cname = name or f"container_{cid}"
container = DockerContainer(
id=cid, image=img, name=cname, status="running",
port_mapping=ports or {}, env_vars=env or {},
started_at=time.time(),
)
self.containers[cid] = container
mode = "d" if detach else ""
port_str = " ".join(f"-p {h}:{c}" for h, c in (ports or {}).items())
print(f" ๐ docker run -{mode} {port_str} {image_name}")
print(f" Container ID: {cid}")
print(f" Status: running โ
")
return container
def ps(self, all: bool = False):
"""docker ps"""
filtered = self.containers.values() if all else [
c for c in self.containers.values() if c.status == "running"
]
filtered = list(filtered)
print(f"\n {'CONTAINER ID':<14} {'IMAGE':<25} {'STATUS':<10} {'PORTS':<20} {'NAME'}")
print(f" {'โ'*80}")
for c in filtered:
ports = ", ".join(f"{h}->{p}" for h, p in c.port_mapping.items())
print(f" {c.id:<14} {c.image.full_name():<25} {c.status:<10} {ports:<20} {c.name}")
def stop(self, container_id: str):
"""docker stop"""
if container_id in self.containers:
self.containers[container_id].status = "stopped"
print(f" โน๏ธ Stopped: {container_id}")
# โโโ ุชุดุบูู ุงูู
ุญุงูุงุฉ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
docker = DockerEngine()
print("๐ณ Docker Concepts Simulation")
print("="*55)
# 1. docker pull
print("\n๐ฅ 1. ุชุญู
ูู Images:")
docker.pull("python", "3.11-slim")
docker.pull("redis", "7-alpine")
# 2. docker build
print("\n๐จ 2. ุจูุงุก Image ุงูุชุทุจูู:")
docker.build("my-ai-api:v1", size_mb=185)
# 3. docker run
print("\n๐ 3. ุชุดุบูู Containers:")
api = docker.run("my-ai-api:v1", "ai-api",
ports={8080: 8000}, env={"PORT": "8000"})
redis = docker.run("redis:7-alpine", "cache",
ports={6379: 6379})
# 4. docker ps
print("\n๐ 4. Containers ุงูุดุบูุงูุฉ:")
docker.ps()
# 5. ู
ูุงุฑูุฉ Container vs VM
print(f"\n{'='*55}")
print("๐ Container vs Virtual Machine:")
print(f"{'ุงูุฎุงุตูุฉ':<22} {'Container':^14} {'VM':^14}")
print("โ"*55)
comparison = [
("ุงูุญุฌู
", "~200 MB", "~20 GB"),
("ููุช ุงูุชุดุบูู","ุซูุงูู", "ุฏูุงุฆู"),
("ุนุฒู ุงูู
ูุงุฑุฏ","ุนู
ููุฉ", "ุฃุฌูุฒุฉ"),
("ุงูุฃุฏุงุก", "ููุชูู ุชูุฑูุจุงู","ุฃุจุทุฃ 10-20%"),
("ุงูุงุณุชุฎุฏุงู
", "ุชุทุจููุงุช", "ุจูุฆุงุช ูุงู
ูุฉ"),
]
for row in comparison:
print(f" {row[0]:<22} {row[1]:^14} {row[2]:^14}")
# 6. docker stop
print("\nโน๏ธ 5. ุฅููุงู ุงูู Containers:")
docker.stop(api.id)
docker.stop(redis.id)
docker.ps(all=True)
print("\n๐ Docker: ุชุทุจููุงุชู ุชุนู
ู ูู ูู ู
ูุงู ุจููุณ ุงูุทุฑููุฉ!")