← Back to Git Course | Chapter 9: CI/CD & DevOps | Lesson 3 of 5

Git Docker Integration

Git के साथ Docker अपना पूरा lunch, plate और table एक box में pack करने जैसा है ताकि यह किसी भी house में same taste करे, और Git उस box की recipe रखता है।
Syntax
bash
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

bash
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

bash
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

bash
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

bash
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

bash
docker build -t myapp .
docker run -p 3000:3000 myapp
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक .dockerignore न होना, ताकि .git/, .env और node_modules image में copy हो जाएँ।
  2. हर image को latest tag करना, ताकि आपको पता न चले कौन सी commit चल रही है; $(git rev-parse --short HEAD) इस्तेमाल करें।
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.