Docker उन tools में से एक है जो तब तक overkill लगता है जब तक आप पहली बार बिना इसके production में deploy नहीं करते — और फिर आप कभी पीछे नहीं मुड़ते। इसके core में, Docker बस आपकी app और उसके environment को एक portable unit में package करता है जिसे container कहते हैं। एक ही code, एक ही dependencies, एक ही behavior — चाहे यह आपके laptop पर चल रहा हो, CI pipeline में, या Frankfurt के किसी server पर।
mental model सीधा है: images blueprints हैं, containers उन blueprints के running instances हैं। आप एक बार image build करते हैं, फिर उससे जितने चाहें उतने containers spin up करते हैं। Volumes data को container की lifecycle से परे persist करते हैं, और networks containers को एक-दूसरे से बात करने देते हैं। Docker Compose सब कुछ एक साथ जोड़ता है जब आपको कई services को मिलकर काम करना होता है।
हमने इसे उन resources के अनुसार organize किया है जिन पर आप काम करते हैं — images, containers, volumes, networks — और फिर Docker Compose का अपना अलग section है क्योंकि यह अब लगभग एक अलग tool ही बन चुका है। अगर आप नए हैं तो Images और Containers से शुरू करें। जब वे natural लगने लगें, तो volumes और networks जल्दी समझ आ जाएंगे। और अगर आप एक से ज़्यादा service चला रहे हैं, तो Compose वही जगह है जहाँ आप रहेंगे।
एक golden rule: ऐसा data जो आपके लिए ज़रूरी है, उसे बिना volume के container के अंदर मत रखें। Containers design से ephemeral हैं। जब वे चले जाते हैं, तो उनके अंदर सब कुछ भी चला जाता है। Volumes आपकी lifeline हैं।
Images
docker build -t <name> .
वर्तमान directory में Dockerfile से image build करेंdocker build -t <name>:<tag> .
एक specific version के साथ image build और tag करेंdocker build --no-cache -t <name> .
cached layers को ignore करके scratch से build करेंdocker pull <image>
registry से image download करें (default: Docker Hub)docker push <image>
registry पर image upload करेंdocker images
सभी locally stored images की list दिखाएँdocker images -q
सिर्फ image IDs दिखाएँ (scripting के लिए उपयोगी)docker tag <image> <new:tag>
किसी मौजूदा image की ओर point करने वाला नया tag बनाएँdocker rmi <image>
एक image हटाएँdocker rmi $(docker images -q -f dangling=true)
सभी dangling (untagged) images हटाएँdocker image prune
unused images हटाएँ (dangling वाली)docker image prune -a
वे सभी images हटाएँ जो किसी container द्वारा use नहीं हो रहीdocker history <image>
image की layer history दिखाएँdocker inspect <image>
image के बारे में विस्तृत metadata JSON में दिखाएँ
Containers
docker run <image>
image से container बनाएँ और शुरू करेंdocker run -d <image>
container को background में चलाएँ (detached mode)docker run -it <image> sh
shell के साथ interactively चलाएँ (debugging के लिए बढ़िया)docker run -p 8080:80 <image>
host port 8080 को container port 80 से map करेंdocker run --name <name> <image>
एक human-friendly नाम के साथ container चलाएँdocker run --rm <image>
container exit होने पर automatically हटा देंdocker run -e KEY=value <image>
container में environment variable pass करेंdocker run --env-file .env <image>
file से environment variables load करेंdocker ps
चल रहे containers की list दिखाएँdocker ps -a
सभी containers दिखाएँ, रुके हुए भीdocker start <container>
रुका हुआ container शुरू करेंdocker stop <container>
चल रहे container को gracefully रोकें (SIGTERM, फिर SIGKILL)docker kill <container>
चल रहे container को तुरंत kill करें (SIGKILL)docker restart <container>
container को रोकें और फिर शुरू करेंdocker exec -it <container> sh
चल रहे container के अंदर shell खोलेंdocker exec <container> <cmd>
चल रहे container के अंदर एक one-off command चलाएँdocker logs <container>
container का stdout/stderr दिखाएँdocker logs -f <container>
real time में logs follow करें (tail -f जैसा)docker logs --tail 100 <container>
logs की आखिरी 100 lines दिखाएँdocker rm <container>
रुका हुआ container हटाएँdocker rm -f <container>
चल रहे container को भी force-remove करेंdocker rm $(docker ps -aq)
सभी रुके हुए containers हटाएँdocker cp <container>:/path /local
container से अपने host पर files copy करेंdocker inspect <container>
container का विस्तृत metadata JSON के रूप में दिखाएँdocker stats
सभी containers के CPU, memory, और network usage का live stream
Volumes
docker volume create <name>
एक named volume बनाएँdocker run -v <vol>:/data <image>
container के अंदर /data पर named volume mount करेंdocker run -v $(pwd):/app <image>
वर्तमान directory को container में bind-mount करेंdocker run -v $(pwd):/app:ro <image>
read-only के रूप में bind-mount करें (container लिख नहीं सकता)docker volume ls
सभी volumes की list दिखाएँdocker volume inspect <name>
volume के बारे में details दिखाएँ (mount point, driver, आदि)docker volume rm <name>
एक volume हटाएँdocker volume prune
वे सभी volumes हटाएँ जो किसी container द्वारा use नहीं हो रहे
Networks
docker network create <name>
एक user-defined bridge network बनाएँdocker network create --driver overlay <name>
एक overlay network बनाएँ (Swarm / multi-host के लिए)docker run --network <name> <image>
container को एक specific network से जोड़कर चलाएँdocker network connect <net> <container>
चल रहे container को network से connect करेंdocker network disconnect <net> <container>
container को network से disconnect करेंdocker network ls
सभी networks की list दिखाएँdocker network inspect <name>
connected containers सहित network details दिखाएँdocker network rm <name>
एक network हटाएँdocker network prune
सभी unused networks हटाएँ
Docker Compose
docker compose up
compose.yaml में defined सभी services बनाएँ और शुरू करेंdocker compose up -d
services को background में शुरू करेंdocker compose up --build
services शुरू करने से पहले images rebuild करेंdocker compose up <service>
सिर्फ एक specific service शुरू करें (और उसकी dependencies)docker compose down
up द्वारा बनाए containers, networks रोकें और हटाएँdocker compose down -v
down जैसा ही, लेकिन named volumes भी हटाएँdocker compose down --rmi all
सब कुछ रोकें और images भी हटाएँdocker compose build
सभी service images build या rebuild करेंdocker compose build --no-cache
scratch से सभी images rebuild करेंdocker compose logs
सभी services के logs दिखाएँdocker compose logs -f <service>
एक specific service के logs follow करेंdocker compose ps
Compose द्वारा managed containers की listdocker compose exec <service> sh
चल रहे service container में shell खोलेंdocker compose run <service> <cmd>
उस service के लिए नए container में one-off command चलाएँdocker compose pull
सभी services के लिए latest images pull करेंdocker compose restart <service>
एक specific service restart करेंdocker compose config
resolved Compose config validate और display करें
System और Cleanup
docker system df
images, containers, और volumes का disk usage दिखाएँdocker system prune
रुके हुए containers, unused networks, और dangling images हटाएँdocker system prune -a --volumes
nuclear option — सारी unused space reclaim करेंdocker info
system-wide Docker configuration और stats दिखाएँdocker version
Docker client और server versions दिखाएँ
throwaway containers के लिए docker run --rm इस्तेमाल करें। इसके बिना, रुके हुए containers चुपचाप जमा होते रहते हैं। कभी docker ps -a चलाएँ — आपको हैरानी होगी कि कितने zombies छुपे हैं।
अपने project में .dockerignore file ज़रूर डालें। यह .gitignore जैसी काम करती है पर builds के लिए। node_modules, .git, और local config files को exclude करने से आपका build context gigabytes से kilobytes हो सकता है और builds काफ़ी तेज़ हो जाती हैं।
अपनी Dockerfile instructions को कम बदलने वाली से ज़्यादा बदलने वाली के क्रम में रखें। Docker layers को ऊपर से नीचे cache करता है, इसलिए COPY package.json और RUN npm install को COPY . . से पहले रखने का मतलब है कि आप dependencies सिर्फ तब reinstall करते हैं जब वे वाकई बदलती हैं — हर code edit पर नहीं।
development के दौरान अलग-अलग build और up steps के बजाय docker compose up --build इस्तेमाल करें। यह सिर्फ वही rebuild करता है जो बदला है और आपको classic "मेरा change क्यों नहीं दिख रहा" confusion से बचाता है।
docker system prune नियमित रूप से चलाएँ। Docker disk space जमा करने में surprisingly अच्छा है। अगर scorched earth करना हो तो -a --volumes जोड़ें, लेकिन सावधान — यह actively in use नहीं होने वाली हर चीज़ हटा देता है, data वाले named volumes सहित।
Compose इस्तेमाल करते वक्त docker exec के बजाय docker compose exec prefer करें। आप container IDs ढूंढने के बजाय services को नाम से reference कर सकते हैं। docker compose exec db psql -U postgres हर बार 12-character hash copy करने से कहीं बेहतर है।
production images को lean रखने के लिए multi-stage builds इस्तेमाल करें। एक stage में सभी dev dependencies के साथ app build करें, फिर सिर्फ compiled output को minimal base image में copy करें। आपकी 1.2 GB Node image 150 MB हो जाती है।