Welcome, awesome folks! Today we will be diving into the topic so called mutli tier containers in docker.
What is multi-tier container ?
Suppose we have our favourite app which got its beautiful frontend as well as heavy stuff processing backend, how can we forgot about database which stores all the data.
So we have the three aspects which are :
Frontend (Container 1)
Backend (Container 2)
Database (Container 3)
Docker is a magical tool which includes all the dependencies in an container to run a specific aspect listed above.But what if we have these three containers running one for frontend , one for backend and another for database.
If these containers running are not connected to each other which means these containers are also called as isolated running containers.
We can connect these containers with the concept called network, combined called container networks. This is also referred to as bridge show in below diagram.
What is volume ?
Think of a Docker volume as a special backpack your container wears. It's a way for your container to carry things around, but it's more like Hermione's magical bag than a regular backpack.
In my personal opinion, volume can also be called as backups. Suppose you are running your todo-app on localhost without volume attached or mount. Whatever data you will be inserting in todo app after refresh that data will be lost.
Inorder to never lost your data, we create volume to store data and mount it while running your container so that if your container by chance got stopped it can recover your data.
Some of the commands of docker volume are as follows:
to see or list the volumes:
docker volume ls
Create volume
Suppose your volume name is todo-app-volume, --opt stands for option , type is none , device will carry file location for volume folder where your data will be stored in order to get recovered and o=bind means the volume should bind to device.
docker volume create --name todo-app-volume --opt type=none --opt device=(filelocation of your volume folder) --opt o=bind
How we will mount our volume on container
suppose we were running our containers like this:
docker run -d -p 8000:8000 todo-app:latest
Now by including volume in it:
docker run -d -p 8000:8000 --mount source=todo-app-volume,target=/app todo-app:latest
Here --mount will mount your volume onto the container, you will specify the volume using volume name into source and target is the file name of your workdir listed in dockerfile and followed by your container name.
Conclusion:
When directing to refreshing your localhost:8000 , whatever data you input in todo app will be saved. How so ?
When you kill your docker container and again run your docker container , you will see a magic that your data which you have written is still there which means docker volume worked!🎒✨
Congratulations folks! you just learned a advanced concept of docker.🤝👩🍳