Docker Community Forums

Share and learn in the Docker community.

[Docker volume] I am confusing about data when I mount docker volume

(Tien The PHAN) #1


I am following the book “Mastering Docker” and have a confusion about docker volume. Please help me understand clearly.

Here is my case.
Create a container redis

root@docker:~# docker container run -d --name redis --network moby-counter redis:alpine

and moby-counter

root@docker:~# docker container run -d --name moby-counter --network moby-counter russmckendrick/moby-counter

Two containers are running

83a924d3cd45 redis:alpine “docker-entrypoint.s…” 22 minutes ago Up 22 minutes 6379/tcp redis
cd5d91dd8c42 russmckendrick/moby-counter “node index.js” 5 hours ago Up 5 hours>80/tcp moby-counter

After that, I access the, and the data appears well.

Then I stop and remove redis container. The page application moby-counter is trying to connect redis in page

Next, I create a new redis with the same command

root@docker:~# docker container run -d --name redis --network moby-counter redis:alpine

The page appears well again. But, all of data what I put on this site was lost. It makes sense, because I don’t mount it to storage on Docker host.

So next, I stop redis container and create a mount volume as following

root@docker:~# docker volume ls
local 0e2881b08ee14e3da69633014c19eceb19b360909705b5f572ed666332c732f8
root@docker:~# docker container run -d --name redis -v 0e2881b08ee14e3da69633014c19eceb19b360909705b5f572ed666332c732f8:/data --network moby-counter redis:alpine

Nice! but, what happened? the data what I put to the page is still here. I am confusing.

Because I already removed the container before starting a new redis container with a volume. I still don’t know why. Could you please help me about that?

Maybe the data stored at front-end (

Thank you so much :slight_smile:


If you don’t create an external named volume (docker volume create …), the lifeclyce of a volume will depend on the lifecycle of the container, with the difference that the volume won’t be deleted by default. If you don’t specify a name for the volume, docker will create a unique id for it. Technicaly this is a named volume with a random name :slight_smile:

if you apply ‘docker rm ${stopped container name or id}’ the assigned volume is not deleted, unless you specify the option --volumes.

What you basicly did is to reattach a named volume to a new container.