Problem with Persistence in Containers
- Each Docker container gets an thin read-write layer, where the files generated/modified by application are present.
- Docker container gets a filesystem which is collection of image layers plus read-write layer mounted on each other but it looks like one filesystem.
- When container tries to edit existing files in image layers , this will not impact image layers as image layers are read-only.
- Internally docker storage system copies the files from read-only image layers to thin read-write layer before modification. This strategy is called as
Copy-on-write

- Refer Here for storage drivers.
- Docker acheives this functionality with the help of storage drivers.
- The lifetime of Read-Write layer is equal to lifetime of container i.e. once the container is deleted all the contents of writable layer are also deleted.
- Consider we have a database container which is used by other app containers. The data is written into db container from app containers i.e. all of this is present in Writable layer . Now if we delete db container all the data is lost.
- To solve this problem docker has introduced the concept of Volumes.
Docker Volumes
- Refer Here for the blog on Docker volumes
- Refer Here for the official docs
- Docker volumes purpose is to persist the data even after container is deleted.
- Docker Volume types
- bind mount
- Volume
- tmpfs
- I want to preserve what ever is present in tmp directory of alpine container.
- Bind mount: Mount existing folder from docker host to any folder in container.
- Create a folder
- mount it to the container
- Create some files

- Delete the container and cross check the files
- Now create a new container and mount the files to the same path

- We can also share the same folder from docker host to multiple containers.
- The same thing can be acheived with –mount
docker container run -d --mount "type=bind,source=/tmp/cont-temp,target=/tmp" --name bind6 alpine sleep 1d

- Volume Mounts: These are storage spaces generally from docker host which is managed by docker. docker has a sub command
docker volume


- Lets mount the tools folder in alpine container with my-vol

- Delete the container

- To remove the volume execute
docker volume rm - Experiment: Let me create a mysql container and postgres container
- Lets see the list of volumes

- mysql
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysqland list volumes

- When we created mysql one volume got created.
- When we create postgres as well a volume got created automatically

- This is happening because of VOLUME instruction in Dockerfile.


- Lets see the list of volumes
