Skip to main content

Command Palette

Search for a command to run...

🚀 How to Set Up Nginx in Docker Using Volumes and Bind Mounts —

Updated
5 min read
🚀 How to Set Up Nginx in Docker Using Volumes and Bind Mounts —
F

🎓 Certified: Aviatrix ACE

👋 Hi, I’m Betty Musari — a former food scientist now diving deep into the world of DevOps and cloud engineering. I write about my hands-on journey with AWS, Docker, and CI/CD, translating complex concepts into clear, beginner-friendly stories. With a scientific mindset and a passion for continuous learning, I'm on a mission to demystify the cloud — one container at a time. ✨As a Food Technologist & Sales Strategist turned DevOps Explorer, I leverage problem-solving and client-centric skills to build resilient cloud systems. Currently mastering CI/CD pipelines, Azure, and Infrastructure as Code (Terraform) to automate deployments like a well-oiled production line. Passionate about merging operational efficiency with technical innovation—because great systems, like great recipes, require precision and scalability.

A Friendly Step-by-Step Guide

🌟 Ahoy, Tech Adventurer! 🚀

Welcome aboard! 👋 Whether you’re a curious beginner or a rising DevOps star, you’ve just landed at the perfect port in your containerization journey. Today, we’re setting sail into the fascinating waters of Docker volumes and bind mounts — tools that may sound intimidating, but are truly your best mates when it comes to data persistence and smooth file sharing across containers.

Together, we’ll roll up our sleeves and launch an Nginx container step by step. By the time we’re done, you’ll not only understand the difference between volumes and bind mounts — you’ll be confidently using them like a seasoned Docker captain. 🐳⚓

So grab your virtual compass and terminal — it’s time to become a Dockerized hero. Let’s dive in! 💪🌊


🌊 First, Why Do We Care About Volumes or Bind Mounts?

Containers are ephemeral—which is just a fancy way of saying they’re short-lived. Once you stop or remove a container, everything inside it is gone. 💨

But here’s the good part: Docker gives us tools like volumes and bind mounts to help us persist data, so our work doesn’t disappear like a ghost!


🧠 Quick Concepts Before We Begin

🔹 What’s a Volume?

Think of a volume as a storage locker managed by Docker. It’s separate from your container but can be attached to one or more containers. Volumes are great for keeping your data safe—even if the container disappears. Plus, you can back them up, restore them, and reuse them.

🔹 What’s a Bind Mount?

A bind mount links a folder or file from your host machine directly into your container. It’s like giving Docker a window into your computer. Any changes you make in the container show up on your host—and vice versa!


✅ Prerequisites

Before we roll up our sleeves:


🔧 Step-by-Step Guide: Let’s Get Dirty (in a good way)

Step 1: Get Root Access and Update Packages

sudo su - : Switch to root (enter password when prompted)

apt update: Refresh package lists

Boom! You’re now root. Updating your packages ensures everything is up to date.


Step 2: Explore Docker's Default Storage

cd /var/lib/docker: Navigate to Docker's home

ls: See what’s in there (containers, volumes, etc.)

This is where Docker stores all the magic—containers, volumes, and more. Think of it as Docker’s brain 🧠.


Step 3: Create a Docker Volume

Let’s name it mybind_volume (you can use anything you like). This creates a persistent volume.

✔️ Verify it:

Awesome! Your volume is now live.


Step 4: Navigate Back to Home Directory

We’re back home and ready to put this volume to work!


Step 5: Run an Nginx Container With the Volume

  • -d: Detached mode (runs in the background)

  • -v mybind_volume:/app: Mounts our volume to /app inside the container

✔️ See It Running:

🔍 Inspect It:

👀 Peek at the Volume:


Step 6: Let’s Create a Host Directory


Step 7: Add a Text File With a Message

You should see: “Welcome to Lyzola Industries”


Step 8: Run an Nginx Container With a Bind Mount

This maps your host folder mybind_mount to /app/data in the container.

✔️ Verify Containers:


Step 9: Dive Into the Container

Run: docker exec -it nginx_bind /bin/sh : Opens interactive shell

Once inside, run:

🎉 Boom! You should see “Welcome to Lyzola Industries” from the file on your host, now inside the container.

✏️ Modify the File:


Step 10: Verify the Changes on the Host

Back on your host system, run:

You should see the updated message: “Welcome to Lyzola Industries, the home of quality and steeze”

That’s bind mount in action—changes made inside the container are reflected outside, and vice versa! 🔁


🎯 Final Thoughts: Volume vs Bind Mount

FeatureDocker VolumeBind Mount
Managed byDockerHost machine
Use casePersistent container dataShare/edit local files
Backup-friendly❌ (Not automatic)
Easier to share between containers⚠️ (Can be messy)

🎉 Wrapping It All Up — You're Officially Dockerized! 🐳✨

And just like that, you’ve done it! 🎊 You’ve successfully set up Nginx with Docker, navigated the magic of both volumes and bind mounts, and unlocked the power of persistent, shareable data in your containerized world.

Whether you're launching full-scale production apps or simply dipping your toes into Docker, this foundational skill will serve you well. Managing storage isn't just a nice-to-have — it’s a game-changer. It supercharges your workflow, boosts your confidence as a modern developer or DevOps pro, and keeps your data safe, accessible, and container-ready. 💾⚙️

But don’t stop here! The world of containers is vast, ever-evolving, and full of new adventures. Try experimenting with multi-container setups, Docker Compose, or advanced volume strategies. And remember — you’re never alone. The Docker community is friendly, resourceful, and always just a command away.

So keep building, keep learning, and above all — keep Dockerizing like the legend you’re becoming. 🌊🐳💙

Happy shipping! 🚢✨
Got questions or want to share your results? Drop a comment below — I’d love to hear from you!