By default containers store data inside their memory. This data get deleted along with container. And since data is stored inside the container, it can not be shared with other containers.
This is what volume solves for us
When we mount a location to some volume, docker redirects the read and write on that path to the volume.
Let's assume that a container is trying to write something in app/data/info
.
Usually this data would be written inside container's file system. So that would be container-file-system/app/data/info
.
If we have mounted app/data/info
to a volume, then when the container tries to write on this location, docker will redirect this operation to the volume.
Since this data then remains outside container's file system, it remains independent of container life cycle.
I have created a simple app. It performs crud operation based and stores files in a json rather than a DB. This is just an example we are using this not whats done in production apps. But yes there we can have reports or config files in such setup.
We have two prepopulated students there. We can use this postman collection to make calls and add more records and fetch e.t.c
Bootup ubuntu and naviagte to the repo. Let's create the docker images and containers
docker build -t student-app-a:v1.0 .
docker run --name student-app-1 -p 8989:3300 student-app-a:v1.0
docker run --name student-app-2 -p 9090:3300 student-app-a:v1.0
Now we have our app running in 2 containers. Its the same version. This is pretty common in production apps.
Let's go ahead add new student in app-1. We have the request in our postman collection. Now hit the endpoint to get student list in app-2. We don't have the newly added student there. This means :
Let's try to generate and use volume to fix our issue
docker stop student-app-1 student-app-2
docker rm student-app-1 student-app-2
docker volume create student-app-volume
docker volume ls
docker run --name student-app-1 -p 8989:3300 -v student-app-volume:/app/student-app/data student-app-a:v1.0
docker run --name student-app-2 -p 9090:3300 -v student-app-volume:/app/student-app/data student-app-a:v1.0
docker stop student-app-1 student-app-2
docker rm student-app-1 student-app-2
This data won't be removed regardless of how many times we remove and add container. As long as the volume is intact, our data is safe.
But as of now you would have noticed, it's quiet a pain creating containers and passing them all the relevant args like port expose volumes names etc
Well that is what we are going to solve in the next section - Docker Compose