Docker volumes and bind mounts allow containers to persist data beyond their lifecycle and share data with the host. This guide covers Docker volumes and bind mounts on Ubuntu 24.04 LTS.
Tested and valid on:
- Ubuntu 24.04 LTS
Prerequisites
- Ubuntu 24.04 LTS server
- Docker Engine installed
- A user in the docker group
Step 1 – Create and Use a Docker Volume
Create a named volume:
docker volume create myvolume
Use the volume in a container:
docker run -d -v myvolume:/data --name mycontainer ubuntu:24.04 tail -f /dev/null
Step 2 – List and Inspect Volumes
View all volumes and their details:
docker volume ls
docker volume inspect myvolume
Step 3 – Share a Volume Between Containers
Mount the same volume in multiple containers:
docker run -d -v myvolume:/data --name reader ubuntu:24.04 tail -f /dev/null
Step 4 – Use Bind Mounts
Mount a host directory into a container:
docker run -d -v /var/www/html:/usr/share/nginx/html:ro -p 8080:80 nginx:latest
Step 5 – Read-Only Volumes
Mount a volume as read-only:
docker run -d -v myvolume:/data:ro ubuntu:24.04 bash
Step 6 – Back Up a Volume
Backup volume data using a temporary container:
docker run --rm -v myvolume:/data -v $(pwd):/backup ubuntu:24.04 tar czf /backup/myvolume-backup.tar.gz /data
Step 7 – Remove Volumes
Remove a specific or all unused volumes:
docker volume rm myvolume
docker volume prune # remove all unused volumes
Conclusion
Docker volumes and bind mounts are now part of your Docker toolkit on Ubuntu 24.04 LTS. Use named volumes for database data and bind mounts for development source code.