A makes a host path appear inside a container. Both sides see the same files: no copy, no sync.
host container "docs"
/home/learner/site/index.html ◄────► /usr/share/nginx/html/index.html
same fileVolume or bind mount
| Named volume | Bind mount | |
|---|---|---|
| Source | NAME | Absolute host path |
| Location | /var/lib/docker/volumes/NAME/_data | Wherever you choose |
| Created by | Docker | You (or -v, as root, if missing) |
| Initial content | Copied from the image if empty | Whatever the host path holds |
| Best for | Application data | Code, config, files you edit |
-v treats the part before the colon as a host path when it starts with /, ~ or ., and as a volume name otherwise.
Serve a project directory
docker run -d --name docs -p 8080:80 -v ~/site:/usr/share/nginx/html nginx:1.27
curl -s localhost:8080<h1>Release notes</h1>Edit on the host and request again. No restart:
echo '<p>v2.1: faster uploads</p>' >> ~/site/index.html
curl -s localhost:8080<h1>Release notes</h1>
<p>v2.1: faster uploads</p>This is the usual development loop: the editor writes to the host, the container serves or runs the result.
Files the container writes are root-owned
Processes in most containers run as root (uid 0). Files they create on a bind mount keep uid 0 on the host:
docker exec docs sh -c 'date > /usr/share/nginx/html/build.txt'
ls -l ~/sitetotal 8
-rw-r--r-- 1 root root 29 Oct 11 20:44 build.txt
-rw-r--r-- 1 learner learner 51 Oct 11 20:44 index.htmllearner can delete build.txt (deleting depends on the directory, which learner owns) but cannot edit it:
$ echo x >> ~/site/build.txt
bash: /home/learner/site/build.txt: Permission deniedTake ownership back with sudo:
sudo chown learner:learner ~/site/build.txtOn real projects the longer-term fix is to run the container as your own uid with docker run --user, so its files are yours from the start.