Docker Data Management Named volume

  • 2020-06-07 05:50:58
  • OfStack

Docker data management: Named volume

In Docker, Named volume and data container can be used for data management.

Single Container use Helloworld

Step 1: Create 1 Named Volume

Prior confirmation of volume information, no VOLUME exists


[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
[root@host88 volumes]#

Confirm/var/lib/docker/volumes condition


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#

Create a data volume named volname, which can be created using the -ES38en parameter, or docker volume create.


[root@host88 volumes]# docker run -it -v volname:/volumedata/dbdata debian
root@b2e3523a6dd9:/# cd volumedata/dbdata
root@b2e3523a6dd9:/volumedata/dbdata# ls -l
total 0
root@b2e3523a6dd9:/volumedata/dbdata#

Verify outside of Container that the volname has been created successfully


[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

Confirm/var/lib/docker/volumes below


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 25 06:23 volname
[root@host88 volumes]# find . -type f
[root@host88 volumes]# find . -type d
.
./volname
./volname/_data
[root@host88 volumes]#

No files exist except for the directory structure

Step 2: Save data in Container Hello world


root@b2e3523a6dd9:/volumedata/dbdata# ls -l
total 0
root@b2e3523a6dd9:/volumedata/dbdata# echo "hello, world" >>helloworld
root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld
hello, world
root@b2e3523a6dd9:/volumedata/dbdata# ls -l
total 4
-rw-r--r-- 1 root root 13 Jul 25 06:26 helloworld
root@b2e3523a6dd9:/volumedata/dbdata#

Externally verify that the information already exists


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world
[root@host88 volumes]#

Step 3: Modify the file externally directly


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world
[root@host88 volumes]# echo "hell, this is `hostname`" >>./volname/_data/helloworld
[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world
hell, this is host88
[root@host88 volumes]#

Confirm information internally


root@b2e3523a6dd9:/volumedata/dbdata# ls -l
total 4
-rw-r--r-- 1 root root 34 Jul 25 06:29 helloworld
root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld
hello, world
hell, this is host88
root@b2e3523a6dd9:/volumedata/dbdata#

Append one more message before exiting from Container


root@b2e3523a6dd9:/volumedata/dbdata# echo "hello, I will exit from `hostname`" >>helloworld
root@b2e3523a6dd9:/volumedata/dbdata# cat helloworld
hello, world
hell, this is host88
hello, I will exit from b2e3523a6dd9
root@b2e3523a6dd9:/volumedata/dbdata#

Step 4: See if the data still exists after exiting Container


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
0

The data is still there. Using docker volume ls, you can see that the data volume just volname still exists.


[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

Data volume management

docker volume management at present basically has the following four: create/ls/inspect/rm

Query (ls)


[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

Normal environment 1 would not have produced such a quiet result.

inspect


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
3

This information may feel very familiar, finish see docker insepect result reveals that the content is 1 to, the following is docker inspect b2e3523a6dd9 mounts related information


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
4

Delete (rm)

Delete the previous confirmation


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
5

Before deleting volume, you need to delete the container on which it is dependent


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
6

Deleting container does not remove volume1 and delete it


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]#

Using docker volume rm removes all information cleanly


[root@host88 volumes]# docker volume rm volname
volname
[root@host88 volumes]# ll
total 0
[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
[root@host88 volumes]#

Long running Docker environment, become a zombie is not just a var/lib/docker volumes below the actual data

There is also a lot of information in docker volume ls that is completely unknown, and some of the actual data associated with it has been deleted

This is common in many poorly thought out environments, even though it's a simple helloworld

The issues that need to be taken into account in data management are still worthy of due attention.

Create (create) :

You can create volume using run and -v as in the example, or you can create docker volume create


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
[root@host88 volumes]#
9

Some volume will also be created using the opt parameter (or -ES197en).

If the name parameter is not specified, docker will kindly pick one for you, as follows


[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]# docker volume create
e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d
[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
local    e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d
[root@host88 volumes]#

It's too annoying to watch. Delete it all under 1.


[root@host88 volumes]# docker volume rm $(docker volume ls -q)
volname
e54a0022fdff1e0e57b8635317e0b51b1e36c3c9b8c48a051e7778a45f08a83d
[root@host88 volumes]#

It is important to note that the name must be unique to 1, so as mentioned earlier, not using docker volume rm can cause problems.

The next time you try to create an volume with the same name only to find that it already exists, the creation fails.

Multiple Container shares 1 data volume

Step 1: Create 1 Named Volume

Create 1 named volume the way you like


[root@host88 volumes]# docker volume create --name=volname
volname
[root@host88 volumes]# docker volume ls
DRIVER    VOLUME NAME
local    volname
[root@host88 volumes]#

Step 2: Passerby A Container is connected to it


[root@host88 volumes]# docker run -it -v volname:/volumedata/dbdata debian
root@5a43b6347b53:/#

A passer-by using Debian wants to know who is the host of docker


root@5a43b6347b53:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var volumedata
root@5a43b6347b53:/# cd volumedata/dbdata
root@5a43b6347b53:/volumedata/dbdata# ls -l
total 0
root@5a43b6347b53:/volumedata/dbdata# echo "hello, world by `hostname`, who is host?" >> helloworld
root@5a43b6347b53:/volumedata/dbdata# cat helloworld
hello, world by 5a43b6347b53, who is host?
root@5a43b6347b53:/volumedata/dbdata#

Step 3: Host and passerby B

The host sees this information


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world by 5a43b6347b53, who is host?
[root@host88 volumes]#

At the same time, passerby B also connected with the volume


[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata centos
[root@6365668cea55 /]#

BTW, Docker cannot use relative paths now, so the front/of volname:/volumedata/dbdata is still indispensable.
Stranger B said although you are not looking for me, but I saw, this is Shared, I can reply, say I don't know.


[root@6365668cea55 dbdata]# ls -l
total 4
-rw-r--r-- 1 root root 43 Jul 25 09:36 helloworld
[root@6365668cea55 dbdata]# cat helloworld
hello, world by 5a43b6347b53, who is host?
[root@6365668cea55 dbdata]# echo "hello, world by `hostname`, I do not know" >> helloworld
[root@6365668cea55 dbdata]# cat helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
[root@6365668cea55 dbdata]#

Step 4: Host and passerby C

When does the host see updates, and as the person who should be responding to the message, have every right to pretend not to


[root@host88 volumes]# pwd
/var/lib/docker/volumes
[root@host88 volumes]# ll
total 0
drwxr-xr-x 3 root root 18 Jul 25 05:31 volname
[root@host88 volumes]# find . -type f
./volname/_data/helloworld
[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
[root@host88 volumes]#

Passers-by use ubuntu, he thinks the data design is really bad, he said he did not want to see such a message, people do not reply to all


[root@host88 ~]# docker run -it -v volname:/volumedata/dbdata ubuntu
root@730209b03ea6:/# cd volumedata/dbdata
root@730209b03ea6:/volumedata/dbdata# ls -l
total 4
-rw-r--r-- 1 root root 87 Jul 25 09:44 helloworld
root@730209b03ea6:/volumedata/dbdata# cat helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
root@730209b03ea6:/volumedata/dbdata# echo "hello, world by `hostname`, please do not reply to all" >> helloworld
root@730209b03ea6:/volumedata/dbdata# cat helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
hello, world by 730209b03ea6, please do not reply to all
root@730209b03ea6:/volumedata/dbdata#

Step 5: Everyone had read the message and decided not to speak

It was so boring, as opposed to the real world, that no one kept jumping up and down and saying, "Please cut me out of mail link.


[root@6365668cea55 dbdata]# cat helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
hello, world by 730209b03ea6, please do not reply to all
[root@6365668cea55 dbdata]#

root@5a43b6347b53:/volumedata/dbdata# cat helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
hello, world by 730209b03ea6, please do not reply to all
root@5a43b6347b53:/volumedata/dbdata#

[root@host88 volumes]# cat ./volname/_data/helloworld
hello, world by 5a43b6347b53, who is host?
hello, world by 6365668cea55, I do not know
hello, world by 730209b03ea6, please do not reply to all
[root@host88 volumes]#

The actual Container can be done better with the same Volume, and the read-write permission can be set reasonably, which can meet many practical scenarios.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: