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

Git Docker Integration

Docker with Git is like packing your whole lunch, plate and table into one box so it tastes the same at any house, and Git keeps the recipe for the box.
Syntax
bash
docker build -t image_name .
docker run image_name

Git and Docker Overview

Packaging your application as a Docker container captures its exact runtime environment — OS, dependencies, versions — so "works on my machine" stops being a real risk, since the same image runs identically wherever it's deployed.

Example: Git and Docker Overview

bash
docker build -t myapp .
docker run myapp

The .dockerignore File

A .dockerignore file works like .gitignore but for Docker builds — listing .git/, local env files, and node_modules-style directories here keeps them out of the build context entirely, which shrinks image size and avoids accidentally baking secrets or version-control internals into a shipped container.

Example: The .dockerignore File

bash
echo ".git/\n.env\nnode_modules/" > .dockerignore

Tagging Images with Commit Hashes

Tagging an image with the short commit hash (docker build -t myapp:$(git rev-parse --short HEAD) .) ties every running container back to the exact source code it was built from, which makes tracking down which commit is live, or rolling back to a known-good one, straightforward.

Example: Tagging Images with Commit Hashes

bash
docker build -t myapp:$(git rev-parse --short HEAD) .

Automating Docker Builds

Wiring docker build and docker push into your CI/CD pipeline means a fresh image gets built and published automatically whenever code merges — removing the manual, easy-to-forget step of building and pushing images by hand before every deploy.

Example: Automating Docker Builds

bash
steps:
  - run: docker build -t myapp:${{ github.sha }} .
  - run: docker push myapp:${{ github.sha }}

Running Local Containers

Running a freshly built image locally (docker run) before pushing it anywhere lets you catch a broken startup, missing environment variable, or misconfigured port mapping while it's still cheap to fix, rather than after it's already failing in production.

Example: 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. #}
Common Mistakes
  1. Not having a .dockerignore, so .git/, .env and node_modules get copied into the image.
  2. Tagging every image as latest, so you cannot tell which commit is running; use $(git rev-parse --short HEAD).
  3. Building an image from a folder with uncommitted changes and assuming the commit hash matches the built code.
🔒

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.