An image was built with one file baked into it, /app/data/seed.txt, containing the words “shipped in image.” Then two containers were started from that same image. The first mounted a brand-new Docker volume at /app/data and listed the directory: seed.txt was there. The second mounted an empty folder from the host at the same path and listed it: nothing. Same image, same path, opposite results. That one difference decides more Docker storage mistakes than any other, and it’s only one of the ways volumes and bind mounts part company. Every result below came from a Docker Engine on Linux, run for this post.
Two ways to keep data outside a container
The problem both solve
A container’s own filesystem disappears with the container. Anything a database writes, a user uploads or a build produces is gone after docker rm unless it was written somewhere that outlives the container. Docker offers two main places to put it.
Volumes
Docker’s volumes documentation describes them as “persistent data stores for containers, created and managed by Docker.” A volume has a name, Docker decides where it lives on disk, and the usual way to touch it is through a container.
Bind mounts
The bind mounts page puts it plainly: “a file or directory on the host machine is mounted from the host into a container.” The host chooses the path. The container and the host are looking at the same directory, live.
Test 1: a named volume outlives its container
$ docker volume create labdata
$ docker volume inspect labdata --format '{{.Driver}} {{.Mountpoint}}'
local /var/lib/docker/volumes/labdata/_data
$ docker run --rm -v labdata:/data localbase:1 sh -c 'echo hello > /data/note.txt'
$ docker run --rm -v labdata:/data localbase:1 cat /data/note.txt
hello
The first container was removed the moment it finished, because of --rm. The second, a completely new container, read the file back. The documentation states the rule directly: a volume ensures “the data is persisted even if the container using it is removed.”
Where a volume actually lives
inspect revealed the volume’s home: a directory under /var/lib/docker/volumes. It’s an ordinary folder owned by root, which is why volumes are best handled through Docker rather than by editing that path from the host. Docker Desktop for Mac and Windows adds another layer: a Docker blog post on file sharing explains that named volumes “are created inside the VM’s own filesystem,” which makes them fast but keeps them out of reach of the host’s file browser. A bind mount, by contrast, is wherever the command line said it was, and any editor on the host can open it.

Test 2: the image’s files, kept or hidden
Empty volume: copied in
This is the experiment from the top of the post. A fresh volume called fresh was mounted at /app/data in the volseed image, and ls printed seed.txt. The volumes page explains why: when an empty volume is mounted over a directory that already has files, “these files or directories are propagated (copied) into the volume by default.”
Bind mount: covered up
The same image with an empty host folder mounted at /app/data showed only . and ... The bind mounts page warns that in this case “the pre-existing files are obscured by the mount.” They aren’t deleted from the image; they’re just hidden for as long as the mount is there.
Why it matters
Plenty of images ship default configuration, starter content or a database’s initial files in the directory meant to be persisted. Point a bind mount at that directory with an empty host folder and the application starts with nothing, often failing in ways that don’t mention storage at all. A volume, on first use, takes a copy. One catch on the volume side: the copy happens only when the volume is empty. Once it holds data, it obscures the image’s files the same way a bind mount does.
Test 3: when the host path doesn’t exist
-v quietly creates it
$ docker run --rm -v /tmp/lab/notthere2:/x localbase:1 sh -c true
$ ls -ld /tmp/lab/notthere2
drwxr-xr-x 2 root root 4096 Dec 1 22:04 /tmp/lab/notthere2
The documentation confirms the behavior: with -v, “Docker automatically creates the directory on the host for you.” The directory came out owned by root. A typo in the path doesn’t fail. It makes a new, empty, root-owned folder and mounts that instead.
–mount refuses
$ docker run --rm --mount type=bind,source=/tmp/lab/alsonot,target=/x localbase:1 sh -c true
docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /tmp/lab/alsonot
That error is exactly what a typo deserves. The volumes page says that “In general, --mount is preferred,” noting it “is more explicit and supports all the available options.”

Test 4: read-only and ownership
Read-only mounts
$ docker run --rm -v labdata:/data:ro localbase:1 sh -c 'echo x > /data/y'
sh: can't create /data/y: Read-only file system
The :ro suffix, or readonly with --mount, lets a container read configuration or content without any chance of changing it. It works for both volumes and bind mounts.
Files written through a bind mount
A container running as root wrote index.html into a bind-mounted host folder. On the host, ls -l showed it as root root. That’s the everyday annoyance of bind mounts during development: build output and logs that the regular user can’t edit or delete without sudo. Running the container as the host user’s ID with --user avoids it.
The security side
The documentation is blunt: “Bind mounts have write access to files on the host by default.” Mount a broad path like a home directory or /etc read-write, and anything in the container can change it.
Test 5: cleaning up
Anonymous volumes pile up
A container started with -v /cache, with no name before the colon, got an anonymous volume named by a 64-character hash. When that container was removed with a plain docker rm, the volume stayed behind in docker volume ls. The documentation notes an exception: anonymous volumes are removed along with containers started with --rm.
What prune removes
docker volume prune -f deleted only the anonymous volume and left the named fresh and labdata volumes alone. The prune reference explains: “By default, it only removes anonymous volumes.” Adding -a changes that, removing “all unused volumes, not just anonymous ones.” In the lab, docker volume prune -af deleted every named volume not attached to a container, including ones holding data. It asks for confirmation without -f for good reason.

Test 6: backing a volume up and restoring it
A throwaway container does the copying
Because a volume’s files sit under a root-owned Docker directory, the supported way to copy them is to start a temporary container that mounts both the volume and a host folder. Docker’s own example, on the volumes page, runs tar in a fresh container with the host’s current directory mounted as /backup. The same pattern was run against a test volume called bk holding two small files, one of them in a subfolder:
$ docker run --rm --entrypoint tar -v bk:/data -v "$PWD":/backup localtest/pg16:lab cf /backup/bk.tar -C /data .
$ tar tf bk.tar
./
./sub/
./sub/b.txt
./a.txt
Restoring into a new volume
A second, empty volume called bk2 was created, and the archive was extracted into it the same way, with xf in place of cf. A third container then read /data/sub/b.txt from bk2 and printed more, the exact contents written to the original. The image used for the copy doesn’t matter as long as it includes tar. The first attempt used a minimal BusyBox-style image and failed with exec: "tar": executable file not found in $PATH, a reminder that the tiniest base images leave out tools people assume are everywhere.
Stop the application first
Copying a database’s files while the database is writing to them can produce a backup that won’t open. For a database volume, stop the container, or use the database’s own dump tool, before running the tar step.
Which one to use
Volumes for application data
Databases, uploaded files and anything the application owns belong in named volumes. They pre-populate from the image, don’t depend on a host path existing, and avoid the ownership tangle.
Bind mounts for files a person edits
Source code during development, a config file kept in Git, or a folder of content that another program on the host also uses are natural bind mounts. Use --mount so a wrong path fails loudly, and add readonly whenever the container only needs to read.
tmpfs for what shouldn’t survive
There’s a third mount type for data that shouldn’t be kept at all. Per Docker’s tmpfs documentation, a tmpfs mount “is temporary, and only persisted in the host memory,” and it is “only available if you’re running Docker on Linux.” In a test container started with --tmpfs /scratch, mount showed tmpfs on /scratch type tmpfs (rw,nosuid,nodev,noexec,relatime). Scratch files, caches and secrets that should never touch disk belong there.
Sources
- Docker Docs, “Volumes”: https://docs.docker.com/engine/storage/volumes/
- Docker Docs, “Bind mounts”: https://docs.docker.com/engine/storage/bind-mounts/
- Docker Docs, “tmpfs mounts”: https://docs.docker.com/engine/storage/tmpfs/
- Docker blog, “File Sharing with Docker Desktop”: https://www.docker.com/blog/file-sharing-with-docker-desktop/
- Docker Docs, “docker volume prune”: https://docs.docker.com/reference/cli/docker/volume/prune/
