Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Data Persistence Volumes docker volume create docker volume ls docker volume inspect docker run v mount
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-data-persistence-volumes-docker-volume-create-docker-volume-ls-docker-volume-inspect-docker-run-v-mount

Docker Docker Data Persistence Volumes docker volume create docker volume ls docker volume inspect docker run v mount

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

⏱️ ~3 min read

1. Core Command / Concept

Manage data persistence and sharing between containers with Docker volumes. docker volume create

2. Step-by-Step

  1. Check the current volumes: docker volume ls
  2. Create a new volume named "myvol": docker volume create myvol
  3. Verify the volume was created: docker volume ls
  4. Inspect the new volume: docker volume inspect myvol
  5. ⚠️ Delete the volume: docker volume rm myvol

3. Real Commands with Examples


Example 1: Persistent Data

Purpose: Store data in a container that persists even after the container is deleted.
Command: docker run -d --name mydata -v myvol:/data ubuntu /bin/sh -c "while true; do echo hello > /data/hello.txt; sleep 1; done" Brief explanation of flags: -d runs the container in detached mode, -v mounts the volume, --name sets the container name, ubuntu is the base image, /bin/sh is the command to run, and while true; do ... done is an infinite loop.

Example 2: Shared Data

Purpose: Share data between multiple containers.
Command: docker run -d --name myapp1 -v myvol:/app ubuntu /bin/sh -c "while true; do echo hello > /app/hello.txt; sleep 1; done" && docker run -d --name myapp2 -v myvol:/app ubuntu /bin/sh -c "while true; do echo world > /app/world.txt; sleep 1; done" Brief explanation of flags: Same as before, with an additional container (myapp2) that shares the same volume.

Example 3: Mounting a Host Directory

Purpose: Mount a directory from the host machine into a container.
Command: docker run -d --name myapp -v /host/dir:/container/dir ubuntu /bin/sh -c "while true; do echo hello > /container/dir/hello.txt; sleep 1; done" Brief explanation of flags: -v mounts the host directory, --name sets the container name, ubuntu is the base image, /bin/sh is the command to run, and while true; do ... done is an infinite loop.

4. Common Errors


Error 1: Volume not created

Error message or symptom: docker: Error response from daemon: failed to create volume: Volume name already exists. Why it happens: The volume name already exists.
One-line fix: docker volume rm existing_volume (replace with the actual volume name)

Error 2: Volume not mounted

Error message or symptom: docker: Error response from daemon: failed to mount volume: Operation not permitted. Why it happens: The volume is not mounted correctly.
One-line fix: docker run -d --name myapp -v myvol:/app ubuntu /bin/sh -c "while true; do echo hello > /app/hello.txt; sleep 1; done"

Error 3: Volume not accessible

Error message or symptom: docker: Error response from daemon: failed to mount volume: No such file or directory. Why it happens: The volume does not exist.
One-line fix: docker volume create myvol

5. Quick Checks


Scenario 1: Check if a volume exists

Situation: You want to check if a volume named "myvol" exists.
Command to run: docker volume ls What a correct response looks like: myvol

Scenario 2: Check if a container is using a volume

Situation: You want to check if a container named "myapp" is using a volume.
Command to run: docker inspect myapp What a correct response looks like: Mounts: ["/host/dir => /container/dir", ...]

Scenario 3: Check if a volume is mounted

Situation: You want to check if a volume named "myvol" is mounted.
Command to run: docker volume inspect myvol What a correct response looks like: Mountpoint: /var/lib/docker/volumes/myvol/_data

6. Last-Minute Reference

  • docker volume create creates a new volume.
  • docker volume ls lists all volumes.
  • docker volume inspect inspects a volume.
  • docker volume rm removes a volume.
  • docker run -v mounts a volume.
  • docker run --mount mounts a volume (alternative to -v).
  • docker volume create --driver specifies a custom volume driver.
  • docker volume create --name specifies a custom volume name.
  • docker volume create --opt specifies custom options for the volume.
  • ⚠️ docker volume rm permanently deletes a volume.


ADVERTISEMENT