Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Data Persistence Bind Mounts Mount Host Directory v hostpathcontainerpath
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-data-persistence-bind-mounts-mount-host-directory-v-hostpathcontainerpath

Docker Docker Data Persistence Bind Mounts Mount Host Directory v hostpathcontainerpath

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

Bind mounts allow you to mount a host directory into a Docker container, making files and directories accessible from within the container. -v /host/path:/container/path

2. Step-by-Step

  1. Create a new directory on the host machine for the bind mount: mkdir /host/path
  2. Create a new Docker container using the bind mount: docker run -it -v /host/path:/container/path myimage
    ⚠️ This mounts the host directory /host/path into the container at /container/path.
  3. Check the mounted directory inside the container: docker exec -it <container_id> ls /container/path
    Output: List of files and directories in the host directory.
  4. Clean up by stopping and removing the container: docker stop <container_id> and docker rm <container_id>

3. Real Commands with Examples


Example 1: Mounting a directory for development

Purpose: To allow a developer to work on code in a container.
Command: docker run -it -v /home/user/project:/project myimage Flags: -v mounts the host directory, -it allows interactive shell.

Example 2: Mounting a directory for data storage

Purpose: To store data in a container without losing it when the container is deleted.
Command: docker run -d -v /data:/data myimage Flags: -d runs the container in detached mode, -v mounts the host directory.

Example 3: Mounting a directory for configuration

Purpose: To store configuration files in a container.
Command: docker run -it -v /etc/config:/config myimage Flags: -v mounts the host directory, -it allows interactive shell.

4. Common Errors


Error 1: Permission denied

Error message: Permission denied Why it happens: The user running the Docker command does not have permission to access the host directory.
Fix: sudo docker run ...

Error 2: Directory not found

Error message: No such file or directory Why it happens: The host directory does not exist.
Fix: mkdir /host/path

Error 3: Bind mount already exists

Error message: Bind mount already exists Why it happens: The bind mount is already mounted.
Fix: docker stop <container_id> and docker rm <container_id>

5. Quick Checks


Scenario 1: Check if a directory is mounted

Situation: You want to check if a directory is mounted in a container.
Command: docker exec -it <container_id> ls /container/path Correct response: List of files and directories in the host directory.

Scenario 2: Check if a file is accessible

Situation: You want to check if a file is accessible in a container.
Command: docker exec -it <container_id> cat /container/path/file.txt Correct response: File contents.

Scenario 3: Check if a directory is writable

Situation: You want to check if a directory is writable in a container.
Command: docker exec -it <container_id> touch /container/path/newfile.txt Correct response: File created.

6. Last-Minute Reference

• The -v flag is used to mount a bind mount.
• The format is -v /host/path:/container/path.
• The host directory must exist before mounting.
• The container directory must exist before mounting.
• ⚠️ Be careful when using bind mounts, as they can expose sensitive data.
• ⚠️ Use docker stop and docker rm to clean up containers.
• ⚠️ Use sudo to run Docker commands with elevated privileges.
• The docker exec command is used to execute a command in a running container.
• The docker run command is used to create and start a new container.
• ⚠️ Use docker version to check the Docker version.




ADVERTISEMENT