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

Hello,

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
e01a3ccf79204ed882699a62543a39a697dccc0d9d960fe2ec46f31f6b6256cd

and moby-counter

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

Two containers are running

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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 0.0.0.0:800->80/tcp moby-counter

After that, I access the http://127.0.0.1:800, and the data appears well.

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

Next, I create a new redis with the same command

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

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
DRIVER VOLUME NAME
local 0e2881b08ee14e3da69633014c19eceb19b360909705b5f572ed666332c732f8
root@docker:~# docker container run -d --name redis -v 0e2881b08ee14e3da69633014c19eceb19b360909705b5f572ed666332c732f8:/data --network moby-counter redis:alpine
83a924d3cd45e55a05866336a944f5fff463975b49435b15b59a5474ea8d7528

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 (127.0.0.1:800)?

Thank you so much :slight_smile:


#2

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.