✦ For everyone, free.

Practical knowledge for real and everyday life

Home

8.3.1 Host Path Mounts

A focused guide to Host Path Mounts, connecting core concepts with practical Docker and container operations.

Host path mounts are bind mounts that expose a specific, existing directory or file on the host filesystem directly inside a container, providing immediate, two-way visibility between host and container for that particular path.

Mounting a Host Directory

The most common form mounts an entire directory from the host into a container at a specified path.

docker run -d -v /home/user/projects/myapp:/app myapp:1.0

Files within /home/user/projects/myapp on the host are directly accessible at /app inside the container, with changes from either side immediately visible to the other.

Mounting a Single Host File

A bind mount isn't limited to entire directories — a single specific file can be mounted as well.

docker run -d -v /etc/myapp/config.yaml:/app/config.yaml myapp:1.0

This makes a specific host configuration file directly available inside the container, without needing to copy it in or rebuild the image to reflect changes.

Why the Host Path Must Already Exist

Unlike a named volume, which Docker creates automatically if it doesn't already exist, a bind mount requires the specified host path to actually exist beforehand — attempting to mount a nonexistent host directory typically results in Docker creating an empty directory there instead, which can be surprising if not expected.

docker run -d -v /home/user/nonexistent-dir:/app/data myapp:1.0
ls /home/user/nonexistent-dir

This reveals that Docker created an empty directory at the specified host path, rather than failing outright, which might not be the intended behavior if a specific, pre-populated directory was actually expected.

Verifying Two-Way Visibility

Confirming that changes made on either side of a host path mount are correctly reflected on the other validates the mount is working as intended.

echo "test" > /home/user/projects/myapp/test.txt
docker exec myapp cat /app/test.txt
Why Host Path Mounts Matter

Host path mounts provide a direct, immediate connection between specific host content and a running container, making them especially valuable for development workflows and for exposing existing host configuration or data without needing to copy it into an image or a separately managed volume.

Content in this section