Docker provides two main mechanisms for persisting and sharing data: named volumes (managed by Docker) and bind mounts (host filesystem paths). Understanding the differences and use-cases helps you choose the right storage strategy for your containers. This guide covers both on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Ubuntu 26.04 LTS with Docker Engine installed
- A user in the docker group
Step 1 – Named Volumes
docker volume create mydata
docker volume ls
docker volume inspect mydata
Step 2 – Mount a Named Volume
docker run -d --name db
-v mydata:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=rootpass
mysql:9
Step 3 – Bind Mounts
Bind mounts map a host directory into the container:
mkdir -p ~/myapp/html
echo 'Hello bind mount!
' > ~/myapp/html/index.html
docker run -d --name webserver
-v ~/myapp/html:/usr/share/nginx/html:ro
-p 8080:80 nginx:alpine
Step 4 – Read-Only vs Read-Write
# Read-only (ro):
docker run -v /host/data:/app/data:ro myimage
# Read-write (default):
docker run -v /host/data:/app/data myimage
Step 5 – tmpfs Mounts (in-memory)
docker run -d --name tmpapp
--mount type=tmpfs,destination=/app/cache
nginx:alpine
Step 6 – Backup a Volume
docker run --rm
-v mydata:/source:ro
-v $(pwd):/backup
ubuntu tar czf /backup/mydata-backup.tar.gz -C /source .
Step 7 – Share Volumes Between Containers
docker run -d --name writer -v shared:/data alpine sh -c 'echo hello > /data/msg.txt'
docker run --rm --volumes-from writer alpine cat /data/msg.txt
Conclusion
You can now effectively manage persistent data with Docker volumes and bind mounts on Ubuntu 26.04 LTS. Use named volumes for database and application data, and bind mounts for development workflows where you want live file editing.