``## π Introduction
This is not just a blog.
This is a complete journey of how I:
- Deployed a multi-container application on EC2
- Debugged Docker networking issues
- Fixed backend and frontend problems
- Understood MongoDB behavior
- And finally learned Docker volumes deeply
I didnβt learn this from theory.
I learned it by:
`text id="k5l1tt"
breaking things β debugging β understanding β fixing
`
ποΈ Project Architecture
My setup had 3 containers:
- Node.js App (Frontend + Backend)
- MongoDB Database
- Mongo Express (DB UI)
All managed using Docker Compose.
βοΈ Step 1: Initial Setup
I ran:
`id="g3f8t0"
docker compose up -d
`
Everything started successfully.
When I opened:
`id="v34txs"
http://<EC2-IP>:3000
`
π UI loaded
π But no data was showing from MongoDB β
β Problem 1: No Data in UI
`text id="x0k3qv"
Data from MongoDB:
`
π Empty
π Debugging Step 1: Browser Network Tab
I checked DevTools β Network
`id="zk6v0l"
/fetch-data β FAILED β
`
β Problem 2: Wrong API URL
Frontend code:
`id="s2o64r"
fetch("http://localhost:3000/fetch-data")
`
π Wrong β
Why?
- Browser β localhost = my laptop
- App β running on EC2
β Fix 1: Use Relative Path
`id="kz8yqf"
fetch("/fetch-data")
`
π Debugging Step 2: API Response
Now API returned:
`id="r1w9cz"
200 OK
`
But response:
`id="2l6c9c"
{}
`
β Problem 3: MongoDB Empty
I checked MongoDB:
`id="s1vxz1"
show dbs
use my-db
show collections
`
π No collections β
β Fix 2: Insert Data
`id="7x6pkn"
db.my-collection.insertOne({
data: "some dynamic data loaded from DB"
})
`
π Debugging Step 3: Still Not Working
Even after inserting data:
π UI still empty β
β Problem 4: Backend Not Fetching Data
Backend was not:
- Selecting DB properly
- Querying collection
- Returning response
β Fix 3: Backend Logic
`js id="v9u5ah"
const db = client.db("my-db");
db.collection("my-collection").findOne({}, function (err, result) {
res.json({
data: result.data
});
});
`
π Debugging Step 4: Still Empty UI
Now:
- API working β
- DB has data β
- Backend correct β
π UI still empty β
β Problem 5: Frontend Bug
Mistake:
`id="n3x7ml"
fetch('/fetch-data');
const jsonResponse = await response.json();
`
π response undefined β
β Fix 4: Correct Fetch
`js id="9z1qj8"
const response = await fetch('/fetch-data');
const jsonResponse = await response.json();
`
β Problem 6: DOM Not Loaded
`id="3u7q3x"
document.getElementById('dynamic')
`
π ran too early β
β Fix 5: window.onload
`js id="y5u2p0"
window.onload = async function () {
const response = await fetch('/fetch-data');
const jsonResponse = await response.json();
document.getElementById('dynamic').textContent = jsonResponse.data;
};
`
β Problem 7: Container Not Updating
Changes not reflecting β
β Fix 6: Rebuild + Recreate
`id="0j6yxn"
docker compose down
docker compose up --build --force-recreate -d
`
π Final Result
`id="l8m1n7"
Data from MongoDB: some dynamic data loaded from DB
`
πΎ The Biggest Learning β Docker Volumes
After everything worked, I discovered a critical issue.
When I ran:
`id="w5n6t2"
docker compose down -v
`
π My database data was gone π₯
β Why Data Was Lost
Because:
- Containers are temporary
- Data inside container is NOT persistent
π₯ What Is a Docker Volume?
A Docker volume is persistent storage mounted directly into a container.
π§ My Initial Wrong Thinking
βVolume is just a linkβ
β WRONG
β Correct Understanding
Volume is directly mounted into container
π¦ MongoDB Data Path
`id="d7n9rs"
/data/db
`
π What Docker Does
`yaml id="3c5y7a"
volumes:
- mongo-data:/data/db
`
π Docker:
`text id="9n4qkz"
mounts volume β /data/db
`
π Visual Flow
`id="y0h4b8"
[ Container ]
β
βΌ
/data/db
β
βΌ
[ Docker Volume ]
β
βΌ
[ Host Storage ]
`
π What Happens on Recreate
`id="5v1q6k"
docker compose down
docker compose up
`
- Container deleted β
- Volume remains β
- New container created β
- Volume mounted again β
π MongoDB reads existing data β
π‘ Real-world Analogy
- Container = Laptop π»
- Volume = External Hard Disk πΎ
β οΈ Critical Command
`id="3m9q8x"
docker compose down -v
`
π Deletes volume β
π Deletes data π₯
β Safe Command
`id="n8k2z7"
docker compose down
`
π Keeps data β
π§ Final Understanding
- Container = temporary
- Volume = persistent
- Data lives in volume
- Container just uses it
π― Final DevOps Insight
βContainers are ephemeral, but data must be persistent. Docker volumes ensure data survives container lifecycle.β
π What I Learned
This project taught me:
- Docker Compose networking
- Service name vs localhost
- EC2 deployment
- MongoDB authentication
- Backend API debugging
- Frontend async handling
- Docker rebuild vs recreate
- Docker volumes (most important π₯)
π§ Interview Answer
βI deployed a multi-container app using Docker Compose on EC2, debugged networking and application issues, and implemented Docker volumes to ensure persistent database storage across container restarts.β
π’ Connect with me
- LinkedIn: https://www.linkedin.com/in/prajwalputtaraju/
- GitHub: https://github.com/Prajwal8651
Top comments (0)