Understanding Docker Volumes and Bind Mounts

Docker makes it easy to isolate applications, but persistent data can be tricky. In this guide, you’ll learn the difference between volumes and bind mounts, and how to use them to manage data effectively in your Docker containers.

What Are Docker Volumes?

Volumes are Docker-managed storage locations on your host. They’re ideal for persisting container data between runs or sharing data between containers.

Key Features:

  • Managed by Docker
  • Safe from accidental deletion
  • Easy to back up or move

What Are Bind Mounts?

Bind mounts link a specific folder from your host machine to a container path. They’re great for development, where live code updates on the host should reflect inside the container.

Key Features:

  • Full control over the file path
  • Real-time file syncing (host ↔ container)
  • Useful for local development environments

Volumes vs. Bind Mounts

FeatureVolumeBind Mount
Managed by Docker✅ Yes❌ No
Custom path❌ No (auto-generated)✅ Yes (you choose the host path)
Backup-friendly✅ Easy⚠️ Manual effort required
Use in production✅ Recommended❌ Dev use only (risk of misconfig)

Practical Example

Let’s see a practical example with a small Python app.

File Structure:

VolumesAndBindMounts/
├── app.py
├── Dockerfile

app.py:

import time

while True:
    with open("/data/log.txt", "a") as f:
        f.write(f"Logging at {time.ctime()}\n")
    time.sleep(5)

Dockerfile:

FROM python:3.11-slim
WORKDIR /app
COPY . .
CMD ["python", "app.py"]

Using a Volume

docker build -t logger-app .
docker run -d --name volume-demo -v logdata:/data logger-app
  • logdata:/data creates a volume mounted to /data in the container.
  • Logs are now saved outside the container and persist even if it’s removed.

Check the volume:

docker volume ls
docker volume inspect logdata

Using a Bind Mount

mkdir ./host_logs

docker run -d --name bind-demo -v ${PWD}/host_logs:/data logger-app
  • Now log.txt will appear in your host_logs folder on your host machine.
  • Any file change in that folder is immediately reflected inside the container.

Clean Up

docker rm -f volume-demo bind-demo
docker volume rm logdata
rm -rf host_logs

Final Thoughts

Docker volumes and bind mounts serve different purposes:

  • Use volumes for production and persistent data
  • Use bind mounts for local development and testing

Understanding when and how to use each can improve your workflow and keep your data safe.

You can read more useful articles like Top 10 Docker Commands Every Developer Should Know.

Follow us for the more helpful posts!

We hope this is a useful post for you.

5 1 vote
Article Rating
Aaron LX

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x