Git Docker Integration
In this page:
docker build -t image_name .
docker run image_name
Git और Docker Overview
अपने application को एक Docker container की तरह package करना इसका exact runtime environment capture करता है — OS, dependencies, versions — ताकि "works on my machine" एक real risk न रहे, क्योंकि same image कहीं भी deployed हो identically चलती है।
उदाहरण: Git and Docker Overview
docker build -t myapp .
docker run myapp
.dockerignore File
एक .dockerignore file .gitignore जैसा काम करती है लेकिन Docker builds के लिए — यहाँ .git/, local env files, और node_modules-style directories list करना उन्हें build context से पूरी तरह बाहर रखता है, जो image size कम करता है और गलती से secrets या version-control internals को एक shipped container में bake होने से बचाता है।
उदाहरण: The .dockerignore File
echo ".git/\n.env\nnode_modules/" > .dockerignore
Commit Hashes से Images Tag करना
एक image को short commit hash से tag करना (docker build -t myapp:$(git rev-parse --short HEAD) .) हर चलते container को उस exact source code से जोड़ देता है जिससे यह built था, जो यह track करना कि कौन सी commit live है, या एक known-good पर rollback करना, straightforward बना देता है।
उदाहरण: Tagging Images with Commit Hashes
docker build -t myapp:$(git rev-parse --short HEAD) .
Docker Builds Automate करना
अपने CI/CD pipeline में docker build और docker push wire करने का मतलब है कि code merge होते ही automatically एक fresh image build और publish होती है — हर deploy से पहले हाथ से images build और push करने वाला manual, आसानी से भूला जाने वाला step हटाते हुए।
उदाहरण: Automating Docker Builds
steps:
- run: docker build -t myapp:${{ github.sha }} .
- run: docker push myapp:${{ github.sha }}
Local Containers चलाना
कहीं भी push करने से पहले एक fresh built image locally चलाना (docker run) आपको एक broken startup, missing environment variable, या misconfigured port mapping पकड़ने देता है जब यह अभी भी fix करना सस्ता है, बजाय इसके पहले से production में fail होने के बाद।
उदाहरण: Running Local Containers
docker build -t myapp .
docker run -p 3000:3000 myapp
- एक
.dockerignoreन होना, ताकि.git/,.envऔरnode_modulesimage में copy हो जाएँ। - हर image को
latesttag करना, ताकि आपको पता न चले कौन सी commit चल रही है;$(git rev-parse --short HEAD)इस्तेमाल करें। - uncommitted changes वाले एक folder से एक image build करना और यह मान लेना कि commit hash built code से match करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: