Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Data Persistence When to use Volumes vs Bind Mounts
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-data-persistence-when-to-use-volumes-vs-bind-mounts

Docker Docker Data Persistence When to use Volumes vs Bind Mounts

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~4 min read

1. Core Command / Concept

Mounting persistent data to a Docker container using either Volumes or Bind Mounts. Use the -v flag for Volumes and the -v flag with the --mount option for Bind Mounts.

2. Step-by-Step

  1. Create a new Docker container with a Volume: docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage
  2. What to check first: The container is running and the Volume is mounted.
  3. The exact command with flags: docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage
  4. What output to expect: The container ID.
  5. How to clean up: docker rm -f <container_id>

  6. Create a new Docker container with a Bind Mount: docker run -d -p 8080:80 --mount type=bind,src=/path/to/host/dir,dst=/app myimage

  7. What to check first: The container is running and the Bind Mount is mounted.
  8. The exact command with flags: docker run -d -p 8080:80 --mount type=bind,src=/path/to/host/dir,dst=/app myimage
  9. What output to expect: The container ID.
  10. How to clean up: docker rm -f <container_id>

  11. ⚠️ Delete all stopped containers: docker container prune

  12. What to check first: The containers are stopped.
  13. The exact command with flags: docker container prune
  14. What output to expect: The number of deleted containers.
  15. How to clean up: None.

  16. Check the Volume or Bind Mount: docker inspect -f '{{.Mounts}}' <container_id>

  17. What to check first: The container is running.
  18. The exact command with flags: docker inspect -f '{{.Mounts}}' <container_id>
  19. What output to expect: The mounted Volume or Bind Mount.
  20. How to clean up: None.

3. Real Commands with Examples


Example 1: Persistent Data with Volumes

Purpose: Store data in a container that persists even after the container is deleted.
Command: docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage Flags: -v flag mounts the Volume, /path/to/host/dir is the host directory, and /app is the container directory.

Example 2: Persistent Data with Bind Mounts

Purpose: Store data in a container that persists even after the container is deleted.
Command: docker run -d -p 8080:80 --mount type=bind,src=/path/to/host/dir,dst=/app myimage Flags: --mount option mounts the Bind Mount, type=bind specifies the type, src=/path/to/host/dir is the host directory, and dst=/app is the container directory.

Example 3: Using Volumes for Multiple Containers

Purpose: Share data between multiple containers.
Command: docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage1 and docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage2 Flags: -v flag mounts the Volume for each container.

4. Common Errors


Error 1: Volume or Bind Mount not mounted

Error message: "No such file or directory".
Why it happens: The Volume or Bind Mount is not properly configured.
One-line fix: Check the Volume or Bind Mount configuration and ensure the host directory exists.

Error 2: Container not running

Error message: "Container not found".
Why it happens: The container is not running.
One-line fix: Start the container with docker start <container_id>.

Error 3: Volume or Bind Mount not accessible

Error message: "Permission denied".
Why it happens: The Volume or Bind Mount is not accessible due to permissions issues.
One-line fix: Change the permissions of the host directory with chmod -R 755 /path/to/host/dir.

5. Quick Checks


Scenario 1: Check if a Volume is mounted

Situation: You want to check if a Volume is mounted to a container.
Command: docker inspect -f '{{.Mounts}}' <container_id> What a correct response looks like: The Volume is listed in the output.

Scenario 2: Check if a Bind Mount is mounted

Situation: You want to check if a Bind Mount is mounted to a container.
Command: docker inspect -f '{{.Mounts}}' <container_id> What a correct response looks like: The Bind Mount is listed in the output.

Scenario 3: Check if a container is running

Situation: You want to check if a container is running.
Command: docker ps -a What a correct response looks like: The container is listed in the output.

6. Last-Minute Reference

  • -v flag mounts a Volume.
  • --mount option mounts a Bind Mount.
  • type=bind specifies the type of Bind Mount.
  • src=/path/to/host/dir is the host directory for a Bind Mount.
  • dst=/app is the container directory for a Bind Mount.
  • docker inspect command inspects a container.
  • docker ps -a command lists all containers.
  • ⚠️ docker container prune deletes all stopped containers.
  • ⚠️ docker rm -f <container_id> deletes a container.
  • ⚠️ chmod -R 755 /path/to/host/dir changes the permissions of a host directory.


ADVERTISEMENT