
Docker has two options for containers to store files on the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts.
Volumes are stored in a part of the host filesystem which is managed by Docker (
/var/lib/docker/volumes/on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker.
Create volume
docker volume create my-vol
List volumes
docker volume ls
Inspect a volume
docker volume inspect my-vol
Remove a volume
docker volume rm my-vol
To remove all unused volumes and free up space
docker volume prune
How to determine what containers use the docker volume?
docker ps -a --filter volume=<VOLUME_NAME_OR_MOUNT_POINT>
docker run -d --name devtest -v myvol2:/app nginx:latest
Use docker inspect devtest to verify that Docker created the volume and it mounted correctly. Look for the Mounts section.
The example below shows a single Docker Compose service with a volume:
services:
frontend:
image: node:lts
volumes:
- myapp:/home/node/app
volumes:
myapp: