• Persisting data with docker volumes for
    • stateful applications
    • databases
      • Container runs on host and uses virtual file system /var/lib/mysql/data.
      • This is volatile, so data is gone when we restart or remove the container.
  • Docker Volumes : Folder in physical host file system is mounted into the virtual file system of Docker container

Docker Volume Types

There are 3 volume types

Host Volumes

You decide where on the host file system the reference is made.

# host folder --> container folder 
docker run -v /home/mount/data:/var/lib/mysql/data

Anonymous Volumes

For each container a folder is generated by docker that gets mounted

# /var/lib/docker/volumes/random-hash/_data
docker run -v /var/lib/mysql/data

Named Volumes

  • Improvement of Anomymous volumes
  • Specifies a name for the volume to be referenced in the host file system
  • should be used in production
# /var/lib/docker/volumes/random-hash/_data referenced by my-named-volume
docker run -v my-named-volume:/var/lib/mysql/data

Where is my data on mac

# below folder does not exist
ls /var/lib/docker/volumes
# Linux - host has this directory
# Mac - Docker for mac creates a Linux VM and stores all the Docker data there
cd Library/Containers/com.docker.docker/Data
ls
# get terminal of the VM
screen com.docker.driver.amd64-linux/tty
cd /var/lib/docker/volumes
ls
ls <named-volume>/_data
# close terminal of VM : ctrl+a+k