Docker Volume
Host system has a physical file system and container has a virtual file system. We mount VFS to host.
A directory or a folder in the physical file system is mounted to VFS.
When a container restarts, data from the physical file system is replicated into the container, thus retaining data.
Volume Types
Host Volumes
We decide where we want to mount on the host file system.
docker run -v /home/mount/data:/var/lib/mysql/data
Where /home/mount/data
is the host file system and :/var/lib/mysql/data
is the container file system (VFS).
Anonymous Volumes
Docker automatically creates a volume for it to mount to.
docker run -v /var/lib/mysql/data
Named Volumes
Similar to anonymous volumes but these can be referenced by names.
docker run -v VolName:/var/lib/mysql/data
Named volumes are mostly used in production.
A single volume can be mounted to multiple containers, especially if the containers have to share data among themselves.
Database Volume Storage Locations
- MongoDB: /data/db
- MySQL: /var/lib/mysql
- Postgres: /var/lib/postgresql/data
Data Volume Locations
- On Windows: C:/ProgramData/docker/volumes
- On Linux / Mac: /var/lib/docker/volumes
However, on Mac, we have to use this command to access the shell of the Docker VM in order to view volume information:
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh