Volumes in docker
Course: Kubernetes Part 2 - chapter 18
Pretext
Section titled “Pretext”The following docker image runs a Node.js app that writes peridically to the filesystem -
https://hub.docker.com/r/100xdevs/write-random
Nodejs Code
$1bRun it in docker
Section titled “Run it in docker”Try running the image above in your local machine
docker run 100xdevs/write-randomTry going to the container and seeing the contents of the container
docker exec -it container_id /bin/bashcat randomData.txtWhere is this file being stored?
Section titled “Where is this file being stored?”The data is stored in the docker runtime filesystem . When the container dies, the data dies with it. This is called ephemeral storage
Volumes in docker
Section titled “Volumes in docker”
If you want to persist data across container stops and starts, you can use Volumes in Docker
Bind mounts
Section titled “Bind mounts”Replace the mount on the left with a folder on your own machine
docker run -v /Users/harkiratsingh/Projects/100x/mount:/usr/src/app/generated 100xdevs/write-randomVolume Mounts
Section titled “Volume Mounts”- Create a volume
docker volume create hello- Mount data to volume
docker run -v hello:/usr/src/app/generated 100xdevs/write-randomIf you stop the container in either case, the randomFile.txt file persists