Ravindra BagaleCourses & study guides मराठी

Chapter 57: Docker Projects Part 1: Websites, Databases, LAMP, LEMP, WordPress, 3-Tier and Every Language with MySQL and MongoDB

DevOps

57.1 Project 1: launch a website with a container (nginx + pandoc)

Friends, today we build our first full project. We take a small travel website written in Markdown, turn the pages into HTML inside a build stage, put them in an nginx image, run the site, test it, ship version 1.1, and roll back to 1.0 in seconds. Type every command yourself. No copy-paste without reading.

What you will learn in this lesson

  1. Why a static website belongs in a container.
  2. How a multi-stage Dockerfile turns Markdown into a small nginx image.
  3. How to build, run, test, update, roll back and clean up.
  4. How Docker Compose runs the same site with one command.

1. Start from a real problem

Suppose we work at a small travel company like MakeMyTrip, but much smaller. Marketing wants a company website live today: home page, about page, and a friendly 404 page.

The old way is to log in to an EC2 server, install nginx by hand, copy files with scp, and edit configs on the server. Next month a new developer asks which nginx version it is. Nobody knows. If the server dies, we repeat everything from memory.

With Docker the website and the web server become one image. The same image runs on a laptop, on EC2 and in any cloud. To update we build a new tag. To roll back we run the old tag again.

Markdown pageswritten by the teamBuild stage (pandoc)turns Markdown into HTMLFinal image (nginx)only HTML + configBrowseropens the site on port 8080
Picture 1: ship the website as one image

2. Prepare the project folder

Step 1: Project folder

Why: We need a clean place for every file.

What: A project folder holds the pages, the nginx config, the Dockerfile and later Compose. Keep each project in its own folder so files never mix.

Example 1: Make the folder

Type this:

mkdir -p travelsite/content && cd travelsite && pwd

You will see:

/home/you/travelsite

What it means: You are now inside the project folder.

Example 2: See it is empty

Type this:

ls -la

You will see:

total 8
drwxr-xr-x  3 you you 4096 ... .
drwxr-xr-x  ... content

What it means: Only the empty content folder so far.

Example 3: Check Docker is awake

Type this:

docker version --format '{{.Server.Version}}'

You will see:

27.3.1

What it means: Docker Engine is running. If you see Cannot connect, start it first.

Step 2: Write the Markdown pages

Why: Pages start as simple text.

What: Markdown is plain text with light marks for headings and lists. pandoc will turn it into HTML later. We write three pages: home, about and 404.

Example 4: Write the home page

Type this:

printf '%s\n' '---' 'title: Sahyadri Trips' '---' '' '# Welcome to Sahyadri Trips' '' 'Weekend treks from Pune and Mumbai.' '' '## Popular trips' '' '- Rajgad fort trek' '- Harihar fort' '- Kalsubai peak' > content/index.md && wc -l content/index.md

You will see:

12 content/index.md

What it means: Twelve lines. The home page is ready.

Example 5: Write the about page

Type this:

printf '%s\n' '---' 'title: About us' '---' '' '# About Sahyadri Trips' '' 'We are a small team of trek leaders. We started in 2019.' > content/about.md && ls content

You will see:

about.md
index.md

What it means: Two pages now.

Example 6: Write the 404 page

Type this:

printf '%s\n' '---' 'title: Page not found' '---' '' '# Oops, this page is not here' '' 'Go back to the home page.' > content/404.md && ls content

You will see:

404.md
about.md
index.md

What it means: Three Markdown files. No HTML yet.

3. Write the nginx config

Step 3: nginx config file

Why: nginx needs clear rules.

What: nginx is a fast web server. Our site.conf tells it where the files live, how to compress pages, how to cache images, which URL is the health check, and which page to show for a missing URL.

Example 7: Write site.conf

Type this:

printf '%s\n' 'server {' '    listen 80;' '    server_name _;' '    root /usr/share/nginx/html;' '    index index.html;' '' '    gzip on;' '    gzip_types text/css application/javascript image/svg+xml;' '' '    location / {' '        try_files $uri $uri.html $uri/ =404;' '    }' '' '    location ~* \.(css|png|jpg|svg)$ {' '        expires 7d;' '        add_header Cache-Control "public";' '    }' '' '    location = /healthz {' '        access_log off;' '        return 200 "ok";' '    }' '' '    error_page 404 /404.html;' '}' > site.conf && wc -l site.conf

You will see:

24 site.conf

What it means: The config is on disk.

Example 8: Show the document root line

Type this:

grep root site.conf

You will see:

root /usr/share/nginx/html;

What it means: This folder is where nginx looks for HTML files inside the container.

Example 9: Show the health URL

Type this:

grep healthz site.conf

You will see:

location = /healthz {

What it means: Docker will call this URL every few seconds.

4. The multi-stage Dockerfile

Stage 1: pandoc/coreconverts .md to .htmlCOPY --from=buildonly the HTML movesStage 2: nginx:alpineserves the pages
Picture 2: two stages, one small image

Step 4: Multi-stage Dockerfile

Why: Build tools must not ship.

What: A multi-stage Dockerfile has more than one FROM line. Stage 1 has pandoc and builds the HTML. Stage 2 is a small nginx image and only copies the HTML. pandoc never reaches production, so the image stays small and safer.

Example 10: Write the Dockerfile

Type this:

printf '%s\n' '# Stage 1: convert Markdown to HTML' 'FROM pandoc/core:3.5 AS build' 'WORKDIR /src' 'COPY content/ ./content/' 'RUN mkdir -p /out && for f in content/*.md; do name=$(basename "$f" .md); pandoc "$f" --standalone --output "/out/$name.html"; done' '' '# Stage 2: serve with nginx' 'FROM nginx:1.27-alpine' 'RUN rm /etc/nginx/conf.d/default.conf' 'COPY site.conf /etc/nginx/conf.d/site.conf' 'COPY --from=build /out/ /usr/share/nginx/html/' 'EXPOSE 80' 'HEALTHCHECK --interval=15s --timeout=3s --retries=3 CMD wget -qO- http://localhost/healthz || exit 1' > Dockerfile && wc -l Dockerfile

You will see:

13 Dockerfile

What it means: Thirteen lines. Two FROM lines mean two stages.

Example 11: Write .dockerignore

Type this:

printf '%s\n' '.git' '*.log' 'notes/' > .dockerignore && cat .dockerignore

You will see:

.git
*.log
notes/

What it means: Docker will not send these folders into the build context.

Example 12: Count the FROM lines

Type this:

grep -c '^FROM ' Dockerfile

You will see:

2

What it means: Exactly two stages, as planned.

Step 5: What each important line means

Why: Read before you build.

What: Every line in a Dockerfile becomes a layer. Knowing what each line does helps you fix errors fast.

Example 13: Show the build stage base

Type this:

grep 'AS build' Dockerfile

You will see:

FROM pandoc/core:3.5 AS build

What it means: AS build names the stage so stage 2 can copy from it.

Example 14: Show the copy from build

Type this:

grep 'COPY --from' Dockerfile

You will see:

COPY --from=build /out/ /usr/share/nginx/html/

What it means: Only the HTML crosses the stage border.

Example 15: Show the health check

Type this:

grep HEALTHCHECK Dockerfile

You will see:

HEALTHCHECK --interval=15s --timeout=3s --retries=3 CMD wget -qO- http://localhost/healthz || exit 1

What it means: Docker marks the container healthy or unhealthy.

5. Build the image

Step 6: docker build with a tag

Why: A tag is a version name.

What: docker build reads the Dockerfile. -t travelsite:1.0 names the image and its version. The number 1.0 is the tag. Later 1.1 will sit next to it on disk.

Example 16: Build version 1.0

Type this:

docker build -t travelsite:1.0 .

You will see:

[+] Building 9.8s (12/12) FINISHED
 => [build 4/4] RUN mkdir -p /out && for f in content/*.md; do ...
 => [stage-1 4/4] COPY --from=build /out/ /usr/share/nginx/html/
 => naming to docker.io/library/travelsite:1.0

What it means: Both stages ran. The final image is travelsite:1.0.

Example 17: List the image

Type this:

docker images travelsite

You will see:

REPOSITORY   TAG   IMAGE ID       CREATED          SIZE
travelsite   1.0   5c1d9e2a7b31   10 seconds ago   48.2MB

What it means: About 48 MB. pandoc is not inside this size.

Example 18: See the layers

Type this:

docker history travelsite:1.0 --format '{{.Size}}  {{.CreatedBy}}' | head -5

You will see:

8.19kB   COPY /out/ /usr/share/nginx/html/ # buildkit
4.10kB   COPY site.conf /etc/nginx/conf.d/site.conf
...

What it means: Our own layers are only a few KB. The rest is nginx.

Step 7: Look inside before you run

Why: Prove the files are there.

What: You can start a one-shot container from the image to list files or test the nginx config, without keeping the container.

Example 19: List the HTML files

Type this:

docker run --rm travelsite:1.0 ls /usr/share/nginx/html

You will see:

404.html
about.html
index.html

What it means: pandoc made three HTML files. --rm removes the container when it stops.

Example 20: Test the nginx config

Type this:

docker run --rm travelsite:1.0 nginx -t

You will see:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

What it means: The config is valid. Fix typos before you run for real.

Example 21: Check the health URL from inside

Type this:

docker run --rm travelsite:1.0 wget -qO- http://127.0.0.1/healthz

You will see:

ok

What it means: Even without publishing a port, the health URL answers inside the container.

6. Run and test the website

Container webnginx listens on door 80Host door 8080-p 8080:80 joins the doorsBrowser or curlopens localhost:8080
Picture 3: from container to browser

Step 8: Run the container

Why: Publish a port and give a name.

What: -d runs in the background. --name web gives an easy name. -p 8080:80 joins door 8080 on your computer to door 80 inside the container. --restart unless-stopped starts the site again after a reboot.

Example 22: Start the site

Type this:

docker run -d --name web -p 8080:80 --restart unless-stopped travelsite:1.0

You will see:

a91f0c2d3e4b5f6a7b8c9d0e...

What it means: The long id is the container id. Your prompt comes back at once.

Example 23: See it running

Type this:

docker ps --filter name=web --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}'

You will see:

NAMES   IMAGE            STATUS                        PORTS
web     travelsite:1.0   Up 5 seconds (health: starting)   0.0.0.0:8080->80/tcp

What it means: Health starts as starting, then becomes healthy after the first check.

Example 24: Wait for healthy

Type this:

sleep 20 && docker inspect -f '{{.State.Health.Status}}' web

You will see:

healthy

What it means: The health check passed. The site is ready.

Step 9: Test like a user

Why: curl is a tiny browser in the terminal.

What: curl asks for a page the same way a browser does. We check the home page text, the about page status code, the 404 page, and gzip.

Example 25: Home page text

Type this:

curl -s http://localhost:8080/ | grep -o 'Welcome to Sahyadri Trips'

You will see:

Welcome to Sahyadri Trips

What it means: The home page is live.

Example 26: About page status

Type this:

curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/about

You will see:

200

What it means: 200 means OK. try_files found about.html for /about.

Example 27: Missing page and gzip

Type this:

curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/missing-page
curl -sI -H 'Accept-Encoding: gzip' http://localhost:8080/ | grep -i content-encoding

You will see:

404
Content-Encoding: gzip

What it means: Missing URLs get our 404 page. gzip is on.

Step 10: Read the logs

Why: See what nginx saw.

What: docker logs shows what the container printed. For nginx that is the access log and the error log.

Example 28: Last five log lines

Type this:

docker logs --tail 5 web

You will see:

172.17.0.1 - - [28/Oct/2026:06:12:01 +0000] "GET /about HTTP/1.1" 200 812 "-" "curl/8.5.0"
...

What it means: Each request leaves one line.

Example 29: Follow live logs

Type this:

timeout 3 docker logs -f web || true

You will see:

(waits for new requests, then timeout ends it)

What it means: -f follows. Use Ctrl+C in a real terminal. Here timeout stops it for the lesson.

Example 30: Count 200 responses so far

Type this:

docker logs web 2>&1 | grep -c ' 200 '

You will see:

4

What it means: Four successful page loads in this lesson so far.

7. Update and roll back

Step 11: Ship version 1.1

Why: A new tag keeps the old one safe.

What: We add one new line to the home page, build travelsite:1.1, replace the running container, and prove the new text is live. The old 1.0 image stays on disk.

Example 31: Add a new line to the home page

Type this:

printf '%s\n' '' '## New this month' '' '- Diwali special: Raigad fort night trek' >> content/index.md && tail -3 content/index.md

You will see:

## New this month

- Diwali special: Raigad fort night trek

What it means: The source changed. The running container still serves the old HTML.

Example 32: Build and switch to 1.1

Type this:

docker build -t travelsite:1.1 .
docker rm -f web
docker run -d --name web -p 8080:80 --restart unless-stopped travelsite:1.1
curl -s http://localhost:8080/ | grep -o 'Diwali special'

You will see:

...
naming to docker.io/library/travelsite:1.1
web
a91f...
Diwali special

What it means: Version 1.1 is live.

Example 33: List both tags

Type this:

docker images travelsite

You will see:

REPOSITORY   TAG   IMAGE ID       CREATED          SIZE
travelsite   1.1   9f2c...
travelsite   1.0   5c1d...

What it means: Both versions sit side by side.

Step 12: Roll back to 1.0

Why: Safety in one command.

What: If 1.1 has a problem, remove the container and run the old tag again. Rollback is not a special tool. It is just running the previous image.

Example 34: Roll back

Type this:

docker rm -f web
docker run -d --name web -p 8080:80 --restart unless-stopped travelsite:1.0
curl -s http://localhost:8080/ | grep -c 'Diwali special'

You will see:

web
b72c...
0

What it means: Zero matches. The new line is gone. Rollback worked.

Example 35: Confirm the image tag in use

Type this:

docker inspect -f '{{.Config.Image}}' web

You will see:

travelsite:1.0

What it means: The running container is back on 1.0.

Example 36: Switch forward again

Type this:

docker rm -f web && docker run -d --name web -p 8080:80 travelsite:1.1 && curl -s http://localhost:8080/ | grep -o 'Diwali special'

You will see:

web
c83d...
Diwali special

What it means: Forward and back are the same idea: change the tag you run.

8. Same site with Docker Compose

Step 13: compose.yaml

Why: One file, one command.

What: Docker Compose reads a YAML file and starts the whole app. For one service it feels small, but the next projects add a database next to this web service, and Compose keeps one command for all of them.

Example 37: Write compose.yaml

Type this:

printf '%s\n' 'services:' '  web:' '    build: .' '    image: travelsite:1.1' '    ports:' '      - "8080:80"' '    restart: unless-stopped' '    read_only: true' '    tmpfs:' '      - /var/cache/nginx' '      - /var/run' > compose.yaml && cat compose.yaml

You will see:

services:
  web:
    build: .
    ...

What it means: read_only makes the container filesystem read-only. tmpfs gives nginx two small writable folders in memory.

Example 38: Stop the hand-run container and start Compose

Type this:

docker rm -f web
docker compose up -d --build
docker compose ps

You will see:

web
...
NAME            IMAGE            SERVICE   STATUS                  PORTS
travelsite-web-1  travelsite:1.1   web       Up 6 seconds (healthy)  0.0.0.0:8080->80/tcp

What it means: One command built and started the site.

Example 39: Test and read Compose logs

Type this:

curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8080/
docker compose logs --tail 3 web

You will see:

200
... GET / HTTP/1.1" 200

What it means: Compose manages the same container you ran by hand.

9. Common mistakes and clean up

Step 14: Port already allocated

Why: The most common first error.

What: If another container already uses door 8080, Docker refuses to start. Pick a free door or remove the old container.

Example 40: See the error

Type this:

docker run -d --name web2 -p 8080:80 travelsite:1.1

You will see:

docker: Error response from daemon: Bind for 0.0.0.0:8080 failed: port is already allocated.

What it means: Door 8080 is taken by the Compose service.

Example 41: Find who holds the port

Type this:

docker ps --format 'table {{.Names}}\t{{.Ports}}' | grep 8080

You will see:

travelsite-web-1   0.0.0.0:8080->80/tcp

What it means: Compose owns the port.

Example 42: Use another port instead

Type this:

docker run -d --name web2 -p 8081:80 travelsite:1.1 && curl -s -o /dev/null -w '%{http_code}\n' http://localhost:8081/ && docker rm -f web2

You will see:

a11b...
200
web2

What it means: 8081 works fine next to 8080.

Step 15: Clean up the project

Why: Leave the machine tidy.

What: After practice, remove containers and project images so the next project starts clean. Named volumes are not used here, so a simple remove is enough.

Example 43: Stop Compose

Type this:

docker compose down

You will see:

Container travelsite-web-1  Removed
Network travelsite_default  Removed

What it means: Compose services are gone.

Example 44: Remove project images

Type this:

docker image rm travelsite:1.0 travelsite:1.1

You will see:

Untagged: travelsite:1.0
Untagged: travelsite:1.1
Deleted: sha256:...

What it means: Both tags removed.

Example 45: Prune dangling build layers

Type this:

docker image prune -f

You will see:

Total reclaimed space: 48.2MB

What it means: Build cache leftovers are gone.

Ravindra Bagale's Tip

Never edit HTML inside a running container. Change the Markdown, build a new tag, and run it. Edits inside a container disappear when you replace it.

Check yourself

Q1. Why use two stages in this Dockerfile?

Stage 1 has pandoc to build pages. Stage 2 only has nginx and HTML, so the final image is small and safer.

Q2. What does -p 8080:80 mean?

Door 8080 on the host forwards to door 80 inside the container.

Q3. How do you roll back the website?

Remove the running container and run the older image tag again.

Q4. How do you check the nginx config inside the image?

docker run --rm travelsite:1.0 nginx -t

Q5. Why is the Compose service read_only?

Attackers cannot change site files; tmpfs gives only the folders nginx must write.

Q6. What does HEALTHCHECK do?

Docker runs a command every few seconds and marks the container healthy or unhealthy.

Friends, you built a real website image today: Markdown in, multi-stage Dockerfile, nginx config, health check, run, test, update, roll back, Compose, and clean up. Did you understand? Let us try it once more. If not, look at Pictures 1 to 3 again and type every command from Step 1 onward yourself. Next project: a todo app with a database.

Lab: try at home

  1. Create the travelsite folder and the three Markdown pages.
  2. Write site.conf and the multi-stage Dockerfile.
  3. Build travelsite:1.0 and test with nginx -t.
  4. Run on port 8080 and prove home, about and 404 work.
  5. Ship 1.1 with a new line, then roll back to 1.0.
  6. Run the same site with Compose and read_only.
  7. Clean up with compose down and image rm.

Recap

  • A static website becomes one nginx image.
  • Build pages in one stage and serve them in another.
  • Test the config with nginx -t before running.
  • Each release is a tag; rollback is running the old tag.
  • Compose starts the same site with one file and one command.
  • Clean up containers and images after the project.

Ravi Sir's Cards for this lesson

मराठी cards