Volumes & Bind mount
While working with containers, when we run a container it will have all required files and data with in it, as soon as we remove container the data will be lost for ever. This is a unique unique problem of docker. When we are running database softwares all data will be lost along with the container if data is maintained in container. Also as data is with in container and container instance is always unique it is not possible to move data from that container, can't shared with other container for scalability.
To overcome this problem docker provides two solutions
Volume
Volumes: Volumes are virtual storage drives, they will linked to container and stores data, they will not be deleted when respective container removed.
- Volumes can be mentioned through Docker configuration file or we can send them as parameter while creating containers.
docker run <image> -v <volume-name:path/in/container>
Bind mounts
Bind mounts: Will allow to link host machine folders as volume to docker container. The host folder in side containers works like its own filesystem. This facility will enable developers to write code from host machine and run them through docker containers live.
- In bind mounts host files overwrites everything in container.
- They can't be mentioned in images (Dockerfile), they can added while creating containers because they are used only in run time.
We will configure them just like volumes but only different is we provide source directory path instead of name left side.
docker run <image> -v <host/path:container/path>
#Example command for volume
docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_paSSWORD=True -v mysql-db-volume:/var/lib/mysqlmysql mysql
#Example command for bindmount
docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_paSSWORD=True -v /Users/vikram/workspace/mysql:/var/lib/mysqlmysql mysql
Create volume
Docker volumes can be created and used with docker
docker volume create <volume name> # creates new volume
docker volume ls # shows all available volumes
docker volume inspect # provide configuration of volume