Ravindra BagaleCourses & study guides मराठी

Chapter 1: What is DevOps?

DevOps

1.4 How DevOps answers that same fight

Why: the problem

So how does DevOps answer the fight from lesson 1.3? Not with one tool, but with a loop. Plan a small change. Code it. Let a machine test it every time (CI). Package it the same way every time. Deploy it with a script, not by hand. Operate it, watch it, and feed what you learn back into the next plan. At Flipkart before Big Billion Days, this loop runs hundreds of times a day across teams. Each change is small, so if something breaks, it is easy to find and undo. In this lesson you will build a tiny version of the whole loop on your laptop with only Git, Bash and Docker: a commit triggers a test, the test gates a package, the package is deployed, a health check confirms it, and a failure rolls back. Later chapters replace each piece with real tools like GitHub Actions, Jenkins and Kubernetes, but the shape stays the same.

What we will build

You will create a Git repo for hello-shop, a test script, a package step that builds a Docker image tagged with the commit, and a deploy step with health check and automatic rollback. A Git hook will run the loop on every commit, so you see plan, code, CI, package, deploy and feedback happen in seconds.

What: every term in plain English

  • DevOps loop — Plan, code, build, test, release, deploy, operate, monitor, then back to plan.
  • CI (continuous integration) — Every change is merged often and checked by automatic tests.
  • Package — Turn code into one thing you can ship, like a Docker image.
  • Image tag — A label on a Docker image, here the Git commit ID.
  • Pipeline — The ordered steps a change goes through: test, package, deploy.
  • Gate — A step that must pass before the next step can run.
  • Git hook — A script Git runs by itself at a moment, like after each commit.
  • Feedback — Information from tests and monitoring that tells you what to fix next.
Start a Git repo Add a test Add a Dockerfile Add the pipeline script Run the pipeline by hand once Run it on every commit Make a change and commit
The DevOps loop on a laptop

How: step by step

  1. Start a Git repo

    cd ~/devops-lab/hello-shop && git init -b main && git add app.py && git commit -m "hello-shop v1"
    

    What it does: Plan and code: the change is now recorded.

    Output

    [main (root-commit) 3f9c2ab] hello-shop v1
     1 file changed, 22 insertions(+)
    
  2. Add a test

    nano test.sh
    

    What it does: Example 1. The test is the CI gate.

    Output

    (file saved)
    
  3. Add a Dockerfile

    nano Dockerfile
    

    What it does: Example 2. Packaging the same way every time.

    Output

    (file saved)
    
  4. Add the pipeline script

    nano pipeline.sh
    

    What it does: Example 3. Test, package, deploy, check, roll back.

    Output

    (file saved)
    
  5. Run the pipeline by hand once

    chmod +x test.sh pipeline.sh && ./pipeline.sh
    

    What it does: See all stages.

    Output

    [test] PASS
    [package] hello-shop:3f9c2ab
    [deploy] OK
    
  6. Run it on every commit

    printf "#!/bin/sh\n./pipeline.sh\n" > .git/hooks/post-commit && chmod +x .git/hooks/post-commit
    

    What it does: Now each commit starts the loop.

    Output

    (no output)
    
  7. Make a change and commit

    sed -i "s/cart is working/cart is fast/" app.py && git commit -am "faster cart"
    

    What it does: The loop runs by itself.

    Output

    [test] PASS
    [deploy] OK hello-shop:7b21e04
    

Example 1: Write the CI gate: a test that must pass

Stack. Bash, curl, Python

Goal. Write the CI gate: a test that must pass.

test.sh

#!/usr/bin/env bash
set -euo pipefail
python3 -m py_compile app.py
PORT=8099 python3 app.py > /tmp/test-app.log 2>&1 &
pid=$!
trap 'kill $pid 2>/dev/null' EXIT
sleep 1
body=$(curl -fsS localhost:8099/)
health=$(curl -fsS localhost:8099/health)
[[ "$body" == hello-shop* ]] || { echo "[test] FAIL: bad body: $body"; exit 1; }
[[ "$health" == ok ]]       || { echo "[test] FAIL: health"; exit 1; }
echo "[test] PASS"

What each line does

  • python3 -m py_compile app.py — Catches syntax errors before anything runs.
  • PORT=8099 python3 app.py ... & — Starts a test copy on a spare port.
  • trap 'kill $pid' EXIT — Always stops the test copy, even if the test fails.
  • [[ "$body" == hello-shop* ]] — The page must start with hello-shop.
  • exit 1 — A failing test stops the whole pipeline.

Green output

$ ./test.sh
[test] PASS

Expected result. You have a real automatic check. Every change must pass it. This is the core of CI.

Example 2: Package the app the same way every time

Stack. Docker 27

Goal. Package the app the same way every time.

Dockerfile

FROM python:3.12-slim
WORKDIR /app
COPY app.py .
ENV PORT=8000
EXPOSE 8000
USER 1000
CMD ["python", "app.py"]

What each line does

  • FROM python:3.12-slim — Start from a small, official Python image.
  • WORKDIR /app — Work inside /app in the image.
  • COPY app.py . — Put our code in the image.
  • EXPOSE 8000 — Documents the port the app uses.
  • USER 1000 — Run as a normal user, not root.
  • CMD [...] — The command that starts the app.

Green output

$ docker build -t hello-shop:3f9c2ab .
 => [1/3] FROM docker.io/library/python:3.12-slim
 => [2/3] WORKDIR /app
 => [3/3] COPY app.py .
 => naming to docker.io/library/hello-shop:3f9c2ab
$ docker images hello-shop
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
hello-shop   3f9c2ab   8d1e3c0f9a21   5 seconds ago   125MB

Expected result. The same image runs on any machine with Docker. No more hidden differences.

Example 3: Build the full loop: test, package, deploy, check, roll back

Stack. Git, Bash, Docker

Goal. Build the full loop: test, package, deploy, check, roll back.

pipeline.sh

#!/usr/bin/env bash
set -euo pipefail
TAG=$(git rev-parse --short HEAD)
./test.sh || { echo "[pipeline] stopped at test"; exit 1; }

docker build -q -t hello-shop:$TAG . > /dev/null
echo "[package] hello-shop:$TAG"

OLD=$(docker inspect -f '{{.Config.Image}}' shop 2>/dev/null || echo none)
docker rm -f shop > /dev/null 2>&1 || true
docker run -d --name shop -p 8000:8000 hello-shop:$TAG > /dev/null
sleep 2
if curl -fsS localhost:8000/health > /dev/null; then
  echo "[deploy] OK hello-shop:$TAG -> $(curl -s localhost:8000)"
else
  echo "[deploy] FAILED, rolling back to $OLD"
  docker rm -f shop > /dev/null
  [ "$OLD" != none ] && docker run -d --name shop -p 8000:8000 "$OLD" > /dev/null
  exit 1
fi

What each line does

  • TAG=$(git rev-parse --short HEAD) — The image tag is the commit ID, so you always know what runs.
  • ./test.sh || { ...; exit 1; } — The gate: no package if tests fail.
  • docker build -q -t hello-shop:$TAG . — Package step.
  • OLD=$(docker inspect -f '{{.Config.Image}}' shop ...) — Remember the version running now, for rollback.
  • curl -fsS localhost:8000/health — Feedback: is the new version healthy?
  • docker run ... "$OLD" — Rollback: start the old image again.

Green output

$ git commit -am "faster cart"
[main 7b21e04] faster cart
 1 file changed, 1 insertion(+), 1 deletion(-)
[test] PASS
[package] hello-shop:7b21e04
[deploy] OK hello-shop:7b21e04 -> hello-shop v1: cart is fast

Expected result. One commit ran the whole DevOps loop in about 10 seconds. GitHub Actions or Jenkins will do the same steps on a server in later chapters.

Verify: did it work?

  1. See which commit runs

    docker inspect -f "{{.Config.Image}}" shop
    

    What it does: The tag matches git log.

    Output

    hello-shop:7b21e04
    
  2. Compare with Git

    git log --oneline -2
    

    What it does: The top commit is the one running.

    Output

    7b21e04 faster cart
    3f9c2ab hello-shop v1
    
  3. List packaged versions

    docker images hello-shop --format "{{.Tag}}"
    

    What it does: Each commit made one image.

    Output

    7b21e04
    3f9c2ab
    

When it is red 1

[test] FAIL: bad body

Cause. The change broke the page text, so the gate stopped it.

Fix, step by step

  1. Read the message.
  2. Fix app.py.
  3. Commit again; the loop reruns.

When it is red 2

fatal: not a git repository (or any of the parent directories): .git

Cause. pipeline.sh was run outside the repo folder.

Fix, step by step

  1. cd ~/devops-lab/hello-shop
  2. Run ./pipeline.sh again.

When it is red 3

permission denied while trying to connect to the Docker daemon socket

Cause. Your user is not in the docker group.

Fix, step by step

  1. sudo usermod -aG docker $USER
  2. Log out and in again, then docker ps.

Cleanup

  1. Remove the hook

    rm .git/hooks/post-commit
    

    What it does: Commits stop running the pipeline.

    Output

    (no output)
    
  2. Stop the app and remove images

    docker rm -f shop && docker image rm $(docker images hello-shop -q) -f
    

    What it does: Frees space.

    Output

    shop
    Untagged: hello-shop:7b21e04
    Untagged: hello-shop:3f9c2ab
    

Ravindra Bagale's Tip

Keep the DevOps loop in mind: code, test, package, deploy, check, and feedback. Tools change. The loop stays the same.

Practice tasks

Easy Run pipeline.sh

Why: Seeing every stage builds the mental model.

What to do: Run the pipeline by hand.

How:
  1. Read each [stage] line.

Check: You see test, package and deploy lines.

Medium Break the gate

Why: A gate must block bad changes.

What to do: Change the page text and commit.

How:
  1. Read the FAIL message.

Check: Nothing new is deployed.

Hard Force a rollback

Why: Rollback must be tested, not hoped for.

What to do: Make the health URL return 500 and commit.

How:
  1. Change /health to send 500.
  2. Check which image runs after.

Check: The old image is running again.

Easy Say the DevOps loop in two lines

Why: If you can say it simply, you understand it. Interviewers want the short version first.

What to do: Write two short lines about the DevOps loop for a fresher who knows only basic Linux.

How:
  1. Write the problem it solves.
  2. Write one real command or file that shows it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Make a safe lab folder

Why: Practice needs a place you can throw away. Then mistakes cost nothing.

What to do: Create a lab folder for this chapter and work only inside it.

How:
  1. mkdir -p ~/devops-lab && cd ~/devops-lab
  2. Run every command of this lesson from here.
  3. Check with pwd before you delete anything.

Check: All lab files live in ~/devops-lab.

Medium Repeat it in a container

Why: Your laptop is not the server. A clean Ubuntu container shows what a fresh server needs.

What to do: Run the main steps of this lesson again inside an Ubuntu container.

How:
  1. docker run --rm -it -v "$PWD":/lab -w /lab ubuntu:24.04 bash
  2. apt update and install what is missing.
  3. Note every missing tool.

Check: Same result inside the container, or a list of what you had to install.

Medium Break it on purpose

Why: You must know the red message by face. In an outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers the right command. A short page saves the on-call person.

What to do: Write a runbook page for this lesson.

How:
  1. Title, when to use, exact command, how to check, how to undo.

Check: A teammate can follow it alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the command or file you used, and one error you hit.

Check: Your answer has a real command and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows who does what and in which order.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at the developer laptop, end at the user on the website.

Check: Each box matches a real thing you ran.

Homework: try at home

Try at home

At home, break the test on purpose by changing the page text, commit, and watch the loop stop. Then change it back. Draw the loop on paper with your own commit IDs.

Recap

  • DevOps is a loop: plan, code, test, package, deploy, operate, feedback.
  • Tests are gates; failing changes never ship.
  • Packages are tagged with the commit ID.
  • Every deploy checks health and can roll back.
  • Real tools later replace each step, but the loop stays the same.

FAQ

Is a Git hook a real CI system?

No. It only shows the idea. Real CI runs on a shared server like GitHub Actions or Jenkins.

Why small changes?

Small changes are easy to test, review and roll back.

What is the difference between CI and CD?

CI checks each change. CD makes every passed change ready to release, or releases it automatically. Lesson 1.6 shows this.

Do I need Docker for DevOps?

Not always, but containers are the most common way to package apps today.

What does feedback mean in real life?

Test results, alerts, dashboards and user reports that change the next plan.

Interview questions

  1. Explain the DevOps lifecycle.

    Plan, code, build, test, release, deploy, operate and monitor, with feedback from each stage into planning.

  2. What makes a good pipeline?

    Fast, automatic, tests as gates, immutable packages, health checks and easy rollback.

  3. How do you know which version is in production?

    Tag images with the commit ID and check the running image or the version endpoint.

  4. How would you do a rollback?

    Redeploy the previous known-good image tag, then fix forward.

  5. Why automate deploys?

    To remove human mistakes, make deploys repeatable and allow many small releases.

Quick quiz

Q1. What is CI?

Merging changes often and checking each one with automatic tests.

Q2. Why tag images with the commit ID?

So you always know exactly which code is running.

Q3. What is a gate?

A step that must pass before the next one runs.

Q4. What does the rollback do?

Starts the previous image again if the new one is unhealthy.

Q5. Which Git feature ran the pipeline on commit?

A post-commit hook.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards