Docker Volumes
By default, any data created inside a container disappears when the container is removed. Volumes provide persistent storage that outlives containers. They are the preferred way to manage data in Docker.
What Are Docker Volumes?
Volumes are directories (or files) that exist outside the container’s union filesystem. They are managed by Docker and stored in a part of the host filesystem (
/var/lib/docker/volumes/ on Linux). Volumes can be shared among multiple containers and survive container removal.Creating and Managing Volumes
Create a volume:
docker volume create my-dataList volumes:docker volume lsInspect a volume:docker volume inspect my-dataRemove a volume (only if not in use):docker volume rm my-dataUsing a Volume with a Container
Mount a volume into a container using the
-v or --mount flag. For example, mount my-data to /data inside a container:docker run -v my-data:/data alpine touch /data/hello.txtNow the volume contains hello.txt. If you run another container with the same volume, you’ll see the file.Using the more explicit
--mount syntax:docker run --mount source=my-data,target=/data alpine ls /dataAnonymous Volumes
If you use
-v /container/path without specifying a name, Docker creates an anonymous volume. These are harder to manage, so it's better to name volumes.Backup and Restore
You can back up a volume by running a temporary container that copies data out. For example:
docker run --rm -v my-data:/source -v $(pwd):/backup alpine cp -a /source/. /backup/Two Minute Drill
- Volumes persist data beyond container lifecycle.
- Create volume:
docker volume create name. - Mount volume:
-v volume-name:/container/path. - List volumes:
docker volume ls. - Remove unused volumes:
docker volume prune.
Need more clarification?
Drop us an email at career@quipoinfotech.com
