Skip to content
Log In Sign Up info@muvcloud.com

Three Ways to Keep Data

TypeLives inUse for
Named volumeDocker's storage areaDatabases, app data
Bind mountA host folder you chooseConfig files, code
tmpfsMemory onlyTemporary files

Anything a container writes outside these is kept in its writable layer and is lost when the container is removed or replaced.

Named Volumes

docker volume create db_data
docker volume ls
docker volume inspect db_data
docker run -d --name db -v db_data:/var/lib/mysql --env-file db.env mysql:9.7

If the volume does not exist, docker run creates it. docker volume inspect shows the Mountpoint, by default /var/lib/docker/volumes/db_data/_data. Docker manages that folder; do not edit files there while a container uses them. A new, empty named volume is filled with whatever the image has at that path the first time it is mounted. A bind mount never is.

Bind Mounts

docker run -d --name web -p 127.0.0.1:8080:80 -v /srv/site:/usr/share/nginx/html:ro nginx:1.30

The host side must be an absolute path, such as /srv/site or "$PWD"/site. A word without a slash is read as a volume name. A bind mount hides whatever the image had at that path, and a change on the host shows up in the container at once.

-v or --mount?

docker run -d --mount type=volume,src=db_data,dst=/var/lib/mysql --env-file db.env mysql:9.7
docker run -d --mount type=bind,src=/srv/site,dst=/usr/share/nginx/html,readonly nginx:1.30

Both forms do the same job; --mount spells every part out as a key and value. One difference matters: with -v, a host folder that does not exist is created, owned by root. With --mount type=bind, Docker stops with an error instead, which catches typing mistakes in the path; the bind-create-src option makes it create the folder like -v. Docker's documentation generally prefers --mount.

Read-Only Mounts and tmpfs

Add :ro to -v, or readonly to --mount, when the container only needs to read, as with a configuration file or a website. A tmpfs mount, available on Linux hosts, keeps files in memory and loses them when the container stops; what it holds counts against the container's memory limit:

docker run -d --tmpfs /tmp myapp:1.0

Back Up and Restore a Volume

Stop the container so the files do not change during the copy, then let a temporary container pack the volume into an archive in the current folder:

docker stop db
docker run --rm -v db_data:/data:ro -v "$PWD":/backup alpine tar czf /backup/db_data.tar.gz -C /data .
docker start db

Restore into a new, empty volume, so the original stays untouched until you have checked the result:

docker volume create db_data_restored
docker run --rm -v db_data_restored:/data -v "$PWD":/backup alpine tar xzf /backup/db_data.tar.gz -C /data

For a database, a dump made with its own tool is the better backup. Copy archives off the server; see snapshot vs backup and how to extract tar.gz files.

Ownership and Permission Problems

A process in a container has a numeric user ID, and a bind mount shows host files with the same numbers. If the application runs as UID 1000 and the host folder belongs to root, writes fail with permission denied. Compare the two:

ls -ln /srv/appdata
docker exec web id

Give the folder to the ID the container uses, for example sudo chown -R 1000:1000 /srv/appdata (see chown recursive), or start the container with --user set to the folder's owner. Never use chmod 777. On AlmaLinux and Rocky Linux, SELinux can block a bind mount; with -v, add :Z for a folder used by one container or :z for a shared one (--mount cannot set the label). These options relabel the host files themselves: never use them on system folders such as /home or /usr, which can leave the host unusable, and keep SELinux enforcing.

Removing Volumes

Removing a volume deletes its data, and there is no undo. Check what uses it and back it up first:

docker volume ls
docker ps -a --filter volume=db_data
docker system df -v
docker volume rm db_data

Docker refuses while any container, even a stopped one, uses the volume. docker volume prune removes unused anonymous volumes; -a also removes unused named ones. More is in how to clean up disk space used by Docker.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations