Docker. Ep 15 - Docker Volumes, Anonymous Volume

Amir Mustafa
5 min readApr 27, 2022

--

Understanding the Problem:

→ In the previous article, we dockerized the application and everything was working fine.

→ When creating a container, our application asked for feedback and it was saved inside the container.

→ The problem is if we delete the container and create a new instance of the container from the same image, all previous feedback data is lost.

→ This problem is for any files we have saved in a container. Once the container is deleted, all files of the previous containers are lost.

→ We would have a comparable scenario if we have a user account or with product data submitted by users or any other type of data which shouldn’t be disappearing suddenly. It should survive.

→ In reality, this problem will come quite often. We will be changing in code and rebuilding the image and containers. Files will be lost

Docker Volumes:

→ Now we know the problem. How can we solve this problem

→ Docker has a built-in feature called volumes.

→ Volumes help in persisting data and solving the above problem.

→ Volumes are folders in our computer that we make Docker aware of and are then mapped to a folder inside the Docker container.

→ Here we can connect the host machine path and container path.

→ Changes in one will be reflected in the other one i.e.

a. If we add a file to the host machine, it is accessible inside a container.

b. If the container adds a file inside the mapped path it is available outside of the container inside the host machine as well.

→ Volumes persist data even if the container is deleted

First Try — Anonymous Volumes:

STEP1: Adding Volume instruction in Dockerfile

→ For creating Anonymous Volumes, we go to Dockerfile in the codebase.

→ By Anonymous, we mean only the docker path is given. Docker itself will create some path in the host computer which we do not need to know.

VOLUME ["/app/feedback"]

→ If we check the server.js file, the temp path should not be stored, whereas feedback path files should be stored

const tempFilePath = path.join(__dirname, "temp", adjTitle + ".txt");  // Need not to be stored, temporary dataconst finalFilePath = path.join(__dirname, "feedback", adjTitle + ".txt"); // Must be storedawait fs.writeFile(tempFilePath, content);
exists
(finalFilePath, async (exists) => {
if (exists) { res.redirect("/exists"); } else { await fs.copyFile(tempFilePath, finalFilePath); await fs.unlink(tempFilePath); res.redirect("/"); }});

STEP 2: Building the Image:

docker build -t <docker-image-name>:<tag> .eg: docker build -t feedback-node:volumes .

STEP 3: Creating Container on the above image:

docker run -d -p <browser-port>:<docker-port> -rm --name <docker-container-name> <docker-image>eg. docker run -d -p 3000:80  -rm --name feedback-app feedback-node:volumes

→ A container is created.

→ Let us now run our node.js application and fill in feedback. Click Save.

→ Let us try to access the feedback file.

→ We are able to access it. Which was also working previously

→ The problem is when we delete this container and create a new container. The file should be accessible.

STEP 4: Delete Current container:

docker stop <container-name>
eg. docker stop feedback-node
(as we are using --rm flag container is auto deleted on stopping)

STEP 5: Re-create container:

docker run -d -p <browser-port>:<docker-port> -rm --name <docker-container-name> <docker-image>eg. docker run -d -p 3000:80  -rm --name feedback-app feedback-node:volumes

→ Let us try to access the feedback file, without adding new feedback

→ We get a file not reachable error 😧 😵

Video:

Closing Thoughts:

In this article, we have seen the first type of docker volume, we see Anonymous Docker Volume did not fix our current problem. We are still unable to access the previous container's file in the new container.

Here comes the concept of the second type of docker volume — Named Docker Volume which we will see in the next article.

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.

--

--

Amir Mustafa
Amir Mustafa

Written by Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/

Responses (1)