Docker container is not persistent, that means, the data stored in a docker container will be gone if the container is destroyed. One of the way to store persistent data while using docker container, is to use the docker volume feature to store your data.
To use the volume, we need to create the volume first.
$ docker volume create myvolume
We can inspect the volume's detailed information using inspect command. This is how we get to know where exactly in filesystem, that the volume resides. and all other information about the volume.
$ docker volume inspect myvolume
[
{
"CreatedAt": "2020-12-24T09:18:03+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvolume/_data",
"Name": "myvolume",
"Options": {},
"Scope": "local"
}
]
To make use of the volume, we will use the --volume flag of the docker run command. For example, we want to create a container from an alpine linux image, run a command in it (in this example, a date command), store the output of the command in a volume, exit, and delete the container after that.
$ docker run --rm --volume=myvolume:/tmp alpine sh -c "date > /tmp/currenttime"
Once the container finished running, we can go to the location of the volume in the filesystem, and we can retrieve the file that is created by the container, even though that container no longer exist.
$ sudo ls /var/lib/docker/volumes/myvolume/_data
currenttime
$ sudo cat /var/lib/docker/volumes/myvolume/_data/currenttime
Thu Dec 24 01:25:34 UTC 2020
So this is how my friend, one of the way to store persistent data generated using docker container.