Docker Volumes vs Bind Mounts: A Beginner’s Guide

Author

Kritim Yantra

Apr 22, 2025

Docker Volumes vs Bind Mounts: A Beginner’s Guide

If you're just starting with Docker, you might have heard about volumes and bind mounts. Both help you persist data in Docker containers, but they work differently. In this guide, we'll break them down in simple terms with examples.


Why Do We Need Volumes and Bind Mounts?

By default, Docker containers are ephemeral—meaning any data inside them disappears when the container stops. But what if you want to:

  • Keep database files even after restarting a container?
  • Share configuration files between your host machine and the container?
  • Avoid bloating the container with large files?

This is where volumes and bind mounts come in!


1. Docker Volumes

Volumes are the preferred way to persist data in Docker. They are managed by Docker and stored in a special location on the host machine.

Key Features:

Managed by Docker – You don’t need to worry about file paths.
Better performance – Great for databases (like MySQL, PostgreSQL).
Can be shared across containers – Multiple containers can use the same volume.
Backup & migration-friendly – Easy to export and import.

How to Use Volumes?

Step 1: Create a Volume

docker volume create my_volume

This creates a volume named my_volume stored in Docker’s storage directory (/var/lib/docker/volumes/ on Linux).

Step 2: Use the Volume in a Container

docker run -d --name mysql_container -v my_volume:/var/lib/mysql mysql

Here, MySQL stores its data in /var/lib/mysql, which is mapped to my_volume.

Step 3: Verify the Volume

docker volume ls  # List all volumes
docker inspect my_volume  # See details

When to Use Volumes?

  • Databases (MySQL, MongoDB, etc.)
  • When you don’t need direct access to files from the host.

2. Bind Mounts

Bind mounts link a directory on your host machine to a directory in the container. Unlike volumes, they are not managed by Docker.

Key Features:

Direct file access – Edit files on your host, and changes reflect instantly in the container.
Useful for development – Great for live code reloading.
Less secure – Containers can modify host system files.
Tied to host’s filesystem – Not portable across different systems.

How to Use Bind Mounts?

Step 1: Run a Container with a Bind Mount

docker run -d --name nginx_container -v /path/on/host:/usr/share/nginx/html nginx

Here:

  • /path/on/host = A folder on your computer.
  • /usr/share/nginx/html = Where Nginx stores web files.

Now, if you edit files in /path/on/host, the changes appear inside the container!

Step 2: Verify the Mount

docker inspect nginx_container | grep "Mounts"  # Check mounted directories

When to Use Bind Mounts?

  • Development environments (e.g., live-reloading Node.js/React apps).
  • When you need to edit config files directly from the host.

Volumes vs Bind Mounts: Quick Comparison

Feature Docker Volume Bind Mount
Managed by Docker ✅ Yes ❌ No
Persists after container removal ✅ Yes ❌ No (unless host files kept)
Good for production databases ✅ Yes ❌ No
Good for development ❌ No ✅ Yes
Portable across systems ✅ Yes ❌ No
Performance ✅ Better ️ Depends on host FS

Which One Should You Use?

  • Use Volumes for databases, production apps, and when you don’t need direct host access.
  • Use Bind Mounts for development, config files, and when you want real-time file syncing.

Bonus: tmpfs Mounts (Memory-Only Storage)

There’s a third option—tmpfs—which stores data only in RAM (disappears when the container stops). Useful for temporary files.

docker run -d --name tmpfs_container --tmpfs /app/cache nginx

Final Thoughts

  • Volumes = Best for persistent, managed storage.
  • Bind Mounts = Best for development & quick access.

Now you know how to keep your Docker data safe and efficient! 🚀

Got questions? Drop them in the comments! 👇

Happy Dockering! 🐳

LIVE MENTORSHIP ONLY 5 SPOTS

Laravel Mastery
Coaching Class Program

KritiMyantra

Transform from beginner to Laravel expert with our personalized Coaching Class starting June 19, 2025. Limited enrollment ensures focused attention.

Daily Sessions

1-hour personalized coaching

Real Projects

Build portfolio applications

Best Practices

Industry-standard techniques

Career Support

Interview prep & job guidance

Total Investment
$200
Duration
30 hours
1h/day

Enrollment Closes In

Days
Hours
Minutes
Seconds
Spots Available 5 of 10 remaining
Next cohort starts:
June 19, 2025

Join the Program

Complete your application to secure your spot

Application Submitted!

Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.

What happens next?

  • Confirmation email with program details
  • WhatsApp message from our team
  • Onboarding call to discuss your goals

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts