Docker volumes are needed for data persistence. They allow us to persist data generated in docker containers in the host machine. As a result, data is not lost when restarting or removing containers. This is achieved by mounting a folder from the physical host file system into the virtual file system of docker containers. Changes made to a container's virtual file system will get replicated in the host file system and vice versa. ## Different Types of Docker Volumes ### Host Volumes Host volumes allow you to decide where on the host file system the reference is made. ![[host-volume-type.png]] ### Anonymous Volumes The volume is created just by referencing the container directory. For each container a folder is automatically generated in the host that gets mounted to the docker directory. ![[anonymous-volume-type.png]] ### Named Volumes This type of volumes is an improvement on anonymous volumes. It specifies the name that will act as a reference to a folder in the host file system. Volumes can be reference just by the given name without the need to know the full path of the volume in the host file system. Named volumes are the most used ones and the ones that should be used in production because there are additional benefits to letting docker manage the created volumes on the host file system. ![[named-volume-type.png]] ## Docker Volumes In Docker-Compose Volumes are defines in the volumes section of the docker-compose file. Then these containers are mounted to specific containers. The same volume can be mounted to multiple containers for cases where multiple containers need to share data. ![[volumes-in-docker-compose.png]]