🔗 (Post 2): Docker Volumes & Networks: Managing Data & Communication!

🔗 (Post 2): Docker Volumes & Networks: Managing Data & Communication!

🔗 (Post 2): Docker Volumes & Networks: Managing Data & Communication!

Docker is powerful not just because it runs apps, but because it gives you tight control over data persistence and inter-container communication. In this blog, we’ll demystify Docker volumes and networks with clean examples and code you can run right away.

📦 Volumes in Docker

Volumes are Docker’s way of storing persistent data outside your container’s ephemeral layer. Even if your container dies, volume data survives.

🔹 Anonymous vs Named Volumes

Anonymous volumes: Created without a name. Harder to manage.

docker run -v /data busybox

Named volumes: Easier to reference, reuse, or delete.

docker volume create mydata
docker run -v mydata:/data busybox

📁 Mounting Host Directories

This binds a local folder from your machine to a path inside the container — ideal for development.

docker run -v $(pwd)/myapp:/usr/src/app node:18

Note: On Windows, use %cd% or provide full paths.

💾 Docker Volume Backup & Restore

🔸 Backup volume:

docker run --rm -v mydata:/data -v $(pwd):/backup alpine \
  tar czf /backup/backup.tar.gz -C /data .

🔸 Restore volume:

docker run --rm -v mydata:/data -v $(pwd):/backup alpine \
  tar xzf /backup/backup.tar.gz -C /data

🌐 Docker Networking Essentials

Every container has its own IP address. Docker helps them communicate using different types of networks.

🌉 Bridged Network (Default)

Great for container-to-container networking with custom isolation.

docker network create my-bridge

Run two containers on the same network:

docker run -d --name db --network my-bridge mongo
docker run -it --network my-bridge mongo mongo --host db

Explanation: The app container can now use db as the hostname to connect to MongoDB.

🖥️ Host Network

Shares the host’s network stack. Useful for performance-heavy or system-level containers.

docker run --network host nginx

🚪 Exposing & Mapping Ports

To access services from your host machine:

docker run -p 8080:80 nginx
  • -p 8080:80 maps port 80 inside the container to port 8080 on your host

🤝 Volume Sharing Between Containers

Let’s create a shared volume between a writer and reader container.

docker volume create sharedvol

docker run -d --name writer --mount source=sharedvol,target=/data busybox \
  sh -c "echo 'Hello Docker' > /data/hello.txt && sleep 3600"

docker run --rm --mount source=sharedvol,target=/data busybox \
  cat /data/hello.txt

🥳 Yes, the reader container sees data written by another container!

🧪 Example: App ↔ DB Communication

Here’s how to connect a Node.js app to a MongoDB container using Docker networking.

🔸 Dockerfile for Node.js

# Dockerfile
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "index.js"]

🔸 index.js

const { MongoClient } = require("mongodb");
const url = "mongodb://db:27017";
MongoClient.connect(url).then(() => {
  console.log("✅ Connected to MongoDB container");
});

🔸 Run App & DB on Same Network

docker network create app-net

docker run -d --name db --network app-net mongo

docker build -t my-node-app .
docker run --network app-net my-node-app

💥 Your app is now talking to a database inside another container — no IP addresses needed, just container names!

📋 Summary

  • 📦 Use named volumes for durable, manageable data.
  • 📁 Mount host directories for development or live sync.
  • 🌐 Use Docker networks for safe, elegant container-to-container communication.
  • 🧩 Use -v and -p to mount volumes and expose ports.

You now have the core foundation to manage data and networking in Docker like a pro.

In our next post, we’ll dive into 🧩 (Post 3): Docker Compose in Action: Building Multi-Container Apps!

— Blog by Aelify (ML2AI.com)