Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Volumes have several advantages over bind mounts:
- Volumes are easier to back up or migrate than bind mounts.
- You can manage volumes using Docker CLI commands or the Docker API.
- Volumes work on both Linux and Windows containers.
- Volumes can be more safely shared among multiple containers.
- Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
- New volumes can have their content pre-populated by a container.
- Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.
To specific volumes, use -v
or --mount
flag on docker run
command.
docker run -d -v myvol2:/app nginx:latest
# or
docker run -d --mount source=myvol2,target=/app nginx:latest
When you run one of the above commands, a myvol2
directory will be created under the /var/lib/docker/volumes
directory.
And you can just create or delete volumes without using the docker run
command:
docker volume cerate my-vol
docker volume ls
docker volume rm my-vol