Docker Ep 16: Named Volumes
→ Docker has two types of external data storage mechanisms:
a. Volumes — Anonymous Volume and Named Volume
b. Bind Mounts
→ In this article, we will dockerize the node.js application using Named Volume.
→ Volumes are managed by docker.
→ By this we mean, docker keeps someplace in the host machine the path which is mapped with the path inside Docker container
Docker Volume Commands:
- Helper volume commands:
docker volume --help
2. Get the list of Volumes
Method 1: Command
docker volume ls
Method 2: Directly through Microsoft’s Docker extension as shown in the image below:
3. Remove all volume at once
docker volume prune
4. Remove specific volume:
docker rm <volume-name-or-id>
Introduction to Docker Named Volume:
→ Till now we have seen volume in the listing command give random IDs. This is because it was anonymous volumes, we did not assign any names.
TRICK: If we stop the anonymous container and we inspect it again. This volume is lost and cannot be retrieved
→ STEP 1: Suppose an anonymous container is running. It is initiated with — rm flag. It means when we stop the container, it is deleted
→ STEP 2: Checking volume. We can check volumes either of the two ways
docker volume ls
or from Microsoft’s docker extension as shown in the screen below:
→ STEP 3: Stopping the container. This will delete the container. This is the demerit of Anonymous container.
The Anonymous volume is gone and does not exist anymore.
Deleting Anonymous Volume Container = Deleting Volume created with it = Data loss
→ With Named volume container will survive volume when container’s shutdown ✅
→ Folders in hard drive will survive, hence creating new container will not affect the previous data 😀
NOTE: Data is available in Named volume. It cannot be edited directly because we do not really have access to this host machine (i.e. managed by Docker) 💡
→ It is hidden somewhere by the docker in the host machine and managed by Docker and it is not meant to be edited by.
Dockerizing Node.js App with Named Volume:
—> We cannot write named volume instruction inside Docker file.Hence we can remove the volume instruction from Docker File. check code here.
VOLUME ["app/feedback"] // Remove this line - used by Anonymous Volume
→ We will add named volume from Docker command. Let us follow below steps:
STEP 1: Delete previous image (if any)
docker rmi <image-name:tag>
eg. docker rmi feedback-node:volumes
STEP 2: Build a new docker image
Important to note, we should delete VOLUME instruction from Dockerfile (it is only for Anonymous volumes)
docker build -t <image-name>:<tag> .
eg. docker build -t feedback-node:volumes .
STEP 3: Create a Named Volume container
→ Command is same as previous a new flag is to be added with volume name and path
-v <volume-name>:<volume-path> // ADDITIONAL FLAG docker run -d -p 3000:80 --rm --name <container-name> -v <volume-name>:<volume-path> <image-name>eg. docker run -d -p 3000:80 --rm --name feedback-app -v feedback-may2022:/app/feedback feedback-node:volumes
STEP 4: Let us check list of docker volumes. This can be done in one of the two ways:
a. docker command:
docker volume ls
b. Microsoft’ extension graphical view, both can be seen from below
→ Finally we have created a named volume.
Time to test, whether this can persist data
STEP 5: Open Node.js application in browser. It is a feedback form that saved data inside app/feedback/<name>.
http://localhost:3000/
→ Enter some feedback. Click save.
→ Let us check the file path. Feedback exist.
http://localhost:3000/feedback/awesome.txt
→ Till here was working in Anonymous Volume. When we delete this container and recreate it data loss was happening.
→ Let us check same with Named Volume
STEP 6: Stop the container
→ As the container was created using — rm flag. Stopping the container will delete it. Let us see in below image
docker stop <container-name>
i.e. docker stop feeback-app
STEP 7 — Recreating same container
→ Command is same as previous a new flag is to be added with volume name and path
-v <volume-name>:<volume-path> // ADDITIONAL FLAGdocker run -d -p 3000:80 --rm --name <container-name> -v <volume-name>:<volume-path> <image-name>eg. docker run -d -p 3000:80 --rm --name feedback-app -v feedback-may2022:/app/feedback feedback-node:volumes
→ Without adding new feedback let us open same file:
http://localhost:3000/feedback/awesome.txt
→ So finally we were able to persist data with the help of Named Volume 😀
Video:
Closing Thoughts:
In this article, we understood the second type of docker volume. We finally saved the container data to be lost.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.