By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Manage data persistence and sharing between containers with Docker volumes. docker volume create
docker volume ls
docker volume create myvol
docker volume inspect myvol
docker volume rm myvol
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.
docker run -d --name mydata -v myvol:/data ubuntu /bin/sh -c "while true; do echo hello > /data/hello.txt; sleep 1; done"
-d
-v
--name
ubuntu
/bin/sh
while true; do ... done
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.
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"
myapp2
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.
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"
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)
docker: Error response from daemon: failed to create volume: Volume name already exists.
docker volume rm existing_volume
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"
docker: Error response from daemon: failed to mount volume: Operation not permitted.
docker run -d --name myapp -v myvol:/app ubuntu /bin/sh -c "while true; do echo hello > /app/hello.txt; sleep 1; done"
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
docker: Error response from daemon: failed to mount volume: No such file or directory.
Situation: You want to check if a volume named "myvol" exists.Command to run: docker volume ls What a correct response looks like: myvol
myvol
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", ...]
docker inspect myapp
Mounts: ["/host/dir => /container/dir", ...]
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
Mountpoint: /var/lib/docker/volumes/myvol/_data
docker volume create
docker volume inspect
docker volume rm
docker run -v
docker run --mount
docker volume create --driver
docker volume create --name
docker volume create --opt
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.