Duration: 45–60 minutes
Level: Beginner
Learning Objectives
After this lab students will understand:
- What a Docker Volume (Bind Mount) is
- Why DevOps engineers use volumes
- Difference between copying files and mounting files
- Why developers love Docker volumes
- Live code changes without rebuilding Docker images
Real DevOps Scenario
Imagine a frontend developer is building a website.
Without Docker:
Edit HTML
↓
Upload to server
↓
Restart Nginx
↓
Refresh Browser
This is slow.
With Docker Volume:
Edit HTML
↓
Save
↓
Refresh Browser
↓
Changes appear instantly
This is exactly how many developers work locally.
Today's Goal
Instead of copying the website inside the Docker image,
we will let Docker use our local folder.
Mac Folder
↓
Docker Volume
↓
Container
↓
Browser
Step 1 Create Project
mkdir docker-volume-lab
cd docker-volume-lab
Step 2 Create Website
touch index.html
Open
nano index.html
Paste
<!DOCTYPE html>
<html>
<head>
<title>Docker Volume Lab</title>
</head>
<body>
<h1>Docker Volume Lab</h1>
<h2>Hello Students!</h2>
<p>This file comes directly from my laptop.</p>
</body>
</html>
Save
CTRL+O
ENTER
CTRL+X
Step 3 Pull Nginx Image
docker pull nginx
Explain
We only download Nginx.
We do NOT create our own image today.
Step 4 Run Container Using Volume
docker run -d \
-p 8080:80 \
-v $(pwd):/usr/share/nginx/html \
--name volume-lab \
nginx
Explain Every Option
docker run
Create container
-d
Detached mode
Run in background
-p 8080:80
Laptop
8080
↓
Container
80
Browser connects to
localhost:8080
Nginx listens on
80
-v
Most important option today.
Volume
or
Bind Mount
$(pwd)
Means
Current Folder
Example
/Users/john/docker-volume-lab
:
Means
Connect
this folder
to
that folder
/usr/share/nginx/html
Nginx serves websites from this folder.
Instead of copying files,
Docker reads directly from your laptop.
Architecture
Mac
index.html
↓
Docker Volume
↓
Container
↓
Nginx
↓
Browser
Step 5 Check Running Container
docker ps
Expected
volume-lab
Up
Step 6 Open Browser
http://localhost:8080
Expected
Docker Volume Lab
Hello Students!
This file comes directly from my laptop.
Step 7 Change Website
Open
nano index.html
Change
<h2>Hello Students!</h2>
to
<h2>Hello DevOps Engineers!</h2>
Save.
Step 8 Refresh Browser
No Docker commands.
No rebuild.
No restart.
Just refresh.
Expected
Hello DevOps Engineers!
Explain Why
Docker is NOT storing the website.
Docker is reading directly from
your laptop.
Laptop File
↓
Docker
↓
Browser
Step 9 Verify Inside Container
Enter container
docker exec -it volume-lab bash
Go to
cd /usr/share/nginx/html
Check
ls
See
index.html
Open
cat index.html
Students will see
the updated file.
Explain
This file is NOT inside Docker.
Docker is reading it from the laptop.
Exit
exit
Step 10 Delete Website
Back on Mac
rm index.html
Refresh Browser.
Expected
404 Not Found
Why?
Because Docker is reading
the laptop folder.
If the laptop file disappears,
the website disappears.
Step 11 Stop Container
docker stop volume-lab
Step 12 Remove Container
docker rm volume-lab
Verify
docker ps -a
Container is gone.
Key Difference
Lab 1
Dockerfile
↓
COPY
↓
Image
↓
Container
Website lives
inside
the image.
Lab 3
Laptop
↓
Volume
↓
Container
Website stays
on the laptop.
Docker simply reads it.
Why DevOps Engineers Use Volumes
Volumes are commonly used for:
- Local development (edit code without rebuilding images)
- Sharing files between the host and containers
- Persisting database data (e.g., MySQL, PostgreSQL)
- Keeping logs outside containers
- Storing uploaded files
Challenge Exercise
Ask students to:
- Add their name to the page:
<h3>Created by Samara</h3>
Add today's date.
Change the background color:
<body style="background-color: lightblue;">
- Refresh the browser.
No rebuild.
No restart.
If the changes appear immediately, they have successfully used a Docker volume.
Top comments (0)