Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Docker Volumes vs Bind Mounts Explained with Examples

Use named volumes for database data and bind mounts for live code. Back up volumes; never world-writable modes.

Friends, volumes keep database files; bind mounts keep your editor live. Chala mitrano — pick the right mount.

Quick answer

Hold this vertical picture:

  1. Create and inspect a named volume.
  2. Mount it into a container.
  3. Bind a host folder for code.
  4. Prefer volumes for DB in Compose.
  5. Back up before deleting volumes.

Language: English · मराठी · हिंदी

Container writable layerLost when removed Named volumeDocker-managed path Bind mountHost folder you choose DB dataPrefer named volume Live code editPrefer bind mount
Volumes vs binds

Why mounts matter

If MySQL data lives only in the container layer, docker rm deletes the shop database.

Why: Persist data; edit code without rebuild. What: Named volumes vs bind mounts. How: Create, inspect, and compare both.

Example 1: Create a named volume

Type this:

command -v docker >/dev/null && docker volume create labdata || printf "%s\n" "labdata (create on your Docker host)"

You will see:

labdata

What it means: Named volumes are managed by Docker and survive container deletes.

Example 2: Inspect a volume

Type this:

command -v docker >/dev/null && docker volume inspect labdata || printf "%s\n" "[{\"Name\": \"labdata\", \"Mountpoint\": \"/var/lib/docker/volumes/labdata/_data\"}]"

You will see:

[{"Name": "labdata", "Mountpoint": "/var/lib/docker/volumes/labdata/_data"}]

What it means: Mountpoint is on the Docker host — do not chmod it world-writable.

Example 5: See the difference

Type this:

printf "%s\n" "VOLUME: Docker picks the host path" "BIND: You pick /home/you/project"

You will see:

VOLUME: Docker picks the host path
BIND: You pick /home/you/project

What it means: Interview one-liner used at Infosys.

Concept 1: Named volumes

Docker manages the host path.

Why: DB files must survive container replace. What: -v NAME:/path; Compose volumes: How: Use for MySQL, Postgres, Redis dumps.

Example 3: Run with a named volume

Type this:

printf "%s\n" "docker run -d --name volbox -v labdata:/data alpine sleep 3600"

You will see:

docker run -d --name volbox -v labdata:/data alpine sleep 3600

What it means: -v NAME:/path mounts a named volume into the container.

Example 6: Compose volumes block

Type this:

printf "%s\n" "services:" "  db:" "    image: mysql:8" "    volumes:" "      - dbdata:/var/lib/mysql" "volumes:" "  dbdata:"

You will see:

services:
  db:
    image: mysql:8
    volumes:
      - dbdata:/var/lib/mysql
volumes:
  dbdata:

What it means: Compose named volume keeps MySQL data across restarts.

Example 8: Data survives container rm

Type this:

printf "%s\n" "docker rm -f volbox" "docker run --rm -v labdata:/data alpine ls /data"

You will see:

docker rm -f volbox
docker run --rm -v labdata:/data alpine ls /data

What it means: Named volume data remains after the container is gone.

Concept 2: Bind mounts

You choose the host folder.

Why: Live code reload on a laptop or EC2. What: -v /host/path:/container/path How: Prefer :ro when the container must not write.

Example 4: Bind mount a host folder

Type this:

mkdir -p /tmp/bind-code && printf "%s\n" "hello" > /tmp/bind-code/app.txt && printf "%s\n" "docker run --rm -v /tmp/bind-code:/app:ro alpine cat /app/app.txt"

You will see:

docker run --rm -v /tmp/bind-code:/app:ro alpine cat /app/app.txt

What it means: Bind mount maps a host path; :ro makes it read-only.

Example 7: Compose bind for code

Type this:

printf "%s\n" "    volumes:" "      - ./src:/app/src"

You will see:

volumes:
      - ./src:/app/src

What it means: Relative bind is great for live reload; bad for prod DB files.

Example 9: When binds break

Type this:

printf "%s\n" "Host path missing" "Permission denied on SELinux hosts" "Windows path style mismatches"

You will see:

Host path missing
Permission denied on SELinux hosts
Windows path style mismatches

What it means: Fix path and permissions — do not use world-writable modes.

Concept 3: Breakages and backup

Wrong path or shared names cause outages.

Why: Back up before volume rm. What: tar via alpine helper; careful names. How: Use the mount card.

Example 10: When volumes break

Type this:

printf "%s\n" "Forgot to declare volume in Compose" "Two stacks share one name by mistake"

You will see:

Forgot to declare volume in Compose
Two stacks share one name by mistake

What it means: Name volumes carefully per environment.

Example 11: Backup habit

Type this:

printf "%s\n" "docker run --rm -v labdata:/data -v /tmp:/backup alpine tar czf /backup/labdata.tgz -C /data ."

You will see:

docker run --rm -v labdata:/data -v /tmp:/backup alpine tar czf /backup/labdata.tgz -C /data .

What it means: Back up named volumes before docker volume rm.

Example 12: Mount card

Type this:

printf "%s\n" "1 DB -> named volume 2 live code -> bind 3 backup volumes 4 never world-writable modes"

You will see:

1 DB -> named volume 2 live code -> bind 3 backup volumes 4 never world-writable modes

What it means: Memorize for Docker Compose interviews.

Mount card

  1. Databases → named volume
  2. Live code → bind mount
  3. Backup before volume rm
  4. Never world-writable modes

Suppose: WordPress+MySQL

dbdata volume + optional theme bind.

Suppose: Node API on laptop

Bind ./src for nodemon; volume for postgres.

Ravindra Bagale's Tip

WordPress on EC2 labs at TCS: named volume for MySQL data, bind mount only for theme code you edit.

Check yourself (quiz)

Q1. Named volume vs bind?

Volume: Docker path. Bind: your host path.

Q2. What for MySQL data?

Named volume

Q3. What for live code?

Bind mount

Q4. Does volume survive docker rm?

Yes

Q5. Compose volume key?

Top-level volumes: plus service volumes list

Q6. Backup tip?

tar the volume via a helper container before rm

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

DB → volume. Code → bind. Backup. No world-writable modes. Did you understand? Let us try it once more.

Frequently asked questions

Volume vs bind mount?

Volume: Docker path. Bind: your host path.

What for databases?

Named volumes.

What for live code?

Bind mounts.

Survive docker rm?

Named volume data yes.

Compose tip?

Top-level volumes: plus service list.

Backup how?

Helper container tar of the volume.