By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
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.
-v
--mount
docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage
How to clean up: docker rm -f <container_id>
docker rm -f <container_id>
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
docker run -d -p 8080:80 --mount type=bind,src=/path/to/host/dir,dst=/app myimage
⚠️ Delete all stopped containers: docker container prune
docker container prune
How to clean up: None.
Check the Volume or Bind Mount: docker inspect -f '{{.Mounts}}' <container_id>
docker inspect -f '{{.Mounts}}' <container_id>
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.
/path/to/host/dir
/app
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.
type=bind
src=/path/to/host/dir
dst=/app
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.
docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage1
docker run -d -p 8080:80 -v /path/to/host/dir:/app myimage2
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 message: "Container not found".Why it happens: The container is not running.One-line fix: Start the container with docker start <container_id>.
docker start <container_id>
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.
chmod -R 755 /path/to/host/dir
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.
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.
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.
docker ps -a
docker inspect
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.