Skip to content

Volumes in docker

Course: Kubernetes Part 2 - chapter 18

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
$1b

Try running the image above in your local machine

docker run 100xdevs/write-random

Try going to the container and seeing the contents of the container

docker exec -it container_id /bin/bash
cat randomData.txt

The data is stored in the docker runtime filesystem . When the container dies, the data dies with it. This is called ephemeral storage

If you want to persist data across container stops and starts, you can use Volumes in Docker

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-random
  • Create a volume
docker volume create hello
  • Mount data to volume
docker run -v hello:/usr/src/app/generated 100xdevs/write-random

If you stop the container in either case, the randomFile.txt file persists