Docker. Episode 14 — Dockerizing App having different types of data
→ In this article, we will understand the kinds of data.
→ In the next few episodes, we will see we can manage different types of data and work with volumes.
→ Images are read-only and containers are read-write.
→ There are three types of data:
- Application (Code + Environment):
These are the data we discussed in the last section (Read-only) ✅
2. Temporary App Data:
These data are generated in real-time (form data-keeping temporary files) (Read + Write)
3. Permanent App Data: These data are stored in files or databases (Read + Write)
Analysing a Realtime App
→ This is a Node.js application, a basic application that accepts feedback.
→ Here we will see all the three types of data — app data, temporary data and permanent app data
→ The key of the app is it takes feedback from the user and saves it in the temp and feedback directory.
app code — when image created
temp data — feedback is first stored in temp
feedback data — if feedback is a new move from temp to feedback, otherwise restrict
→ We will be dockerizing the app, once the docker container is created our application will look like the below:
→ Let's review the code. You can also check the below video to see the real-time dockerizing of this application.
→ The page contains a couple of routes. The key thing to observe here is if feedback contains a title and body.
→ First, they are kept in a temp file named the lowercase title.txt
→ If the feedback name is new moves from temp to feedback directory.
→ If the same name file already exists in feedback user needs to enter different feedback.
→ Here we are copying in two paths — the temp path (tempFilePath) and finally one final path (finalFilePath)
→ The generated file simply contains feedback that was submitted.
→ Some other pages in the application are:
a. pages/feedback.html and pages/exists.html
Dockerizing the App:
STEP 1: Create Dockerfile
FROM node:17WORKDIR /appCOPY package.json .RUN npm installCOPY . .EXPOSE 80CMD ["node", "server.js"]
STEP 2: Creating Image:
docker build .or docker build -t feedback-node // image created with name
→ We get image ID in the end (SHA 256)
→ We can also see feedback-node named image created in docker using microsoft docker extension.
STEP 3: Creating container out of this image
→ Below command is executed
docker run -p 3000:80 -d –name feedback-app –rm feedback-node
Key: port 3000, detached mode, named the container, remove the container once closed.
→ We see our newly created container in Microsoft docker extension
→ our application is running now
STEP 4: Entering a feedback in application. Click Save button
Feedback 1: Entering a new feedback
→ The feedback is saved. Its title lowercase is the file name.
→ On a side note if we will try to view inside feedback of the local app. File will not be there.
→ This is because. It is inside the docker/app/feedback/awesome.txt from where it is rendering in the app
NOTE: After creating image from Dockerfile, web application uses docker image and not the local codebase.
→ Hence, uploaded feedback was stored in docker as shown in above image
Feedback 2: If we now try to run the same feedback again (mean same title), system will ask for a new feedback
→ So here we have seen all three types of data:
a. when created image — app data
b. first time feedback — temp data (okay, if lost)
c. permanent feedback — permanent data (if feedback does not exist, store in docker/feedback/title.txt)
In this article, we have seen way to dockerize application with different types of data in the application. This is true for all technologies say Node.js, Python, Java, PHP, etc.
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.