docker volume checked

I checked the volume of docker.

-The internal data of the docker container disappears when deleted. -Mount the host and docker volume managed by docker for data persistence. -Files and directories specified for mounting remain even after the container is deleted (it feels like the data is saved outside the container). -There are two types of mounts. The type that synchronizes the host directory and the one that mounts the volume space managed by docker. -The former is called bind mount and the latter is called volume. -The former is used to synchronize the code under development in the container for development (you can place the code in the container while editing it with an editor). The latter is used when it is not necessary to manage it on the host side (mysql data, socket file connecting nginx and php, etc.)

Basic usage

Can be mounted with the -v option when running docker run The format is -v host path: container path. Relative path cannot be used for host path, so write it as absolute path.

↓a.Synchronize txt
$ docker run -it --rm -v ~/Documents/test/docker/volume_test/a.txt:/a.txt centos:centos8
[root@defef339acca /]# ls -la 
-rw-r--r--   1 root root    4 Dec 15 12:02 a.txt
Abbreviation

If you update a.txt on the host side, it will be reflected on the container side as well. Also, even if it is changed from the container side, it will be reflected on the host side.

$ echo "aaa" > a.txt
↓ Container side
[root@defef339acca /]# cat a.txt 
aaa

Deleting a.txt on the host side causes a bug on the container side. It exists with ls -la, but you can't access the file.

[root@defef339acca /]# ls -la 
-?????????   ? ?    ?       ?            ? a.txt
[root@defef339acca /]# cat a.txt 
cat: a.txt: No such file or directory

An error occurs when trying to delete a.txt on the container side. File editing is possible.

[root@57c0c3b18570 /]# rm a.txt 
rm: remove regular file 'a.txt'? yes
rm: cannot remove 'a.txt': Device or resource busy

If you try to mount a.txt without it on the host side, a directory named a.txt will be created and mounted.

[root@47850246319a /]# ls -la 
drwxr-xr-x   2 root root   64 Dec 15 12:12 a.txt

You can mount directories as well as files.

$ mkdir hoge
$ touch hoge/a.txt
$ docker run -it --rm -v ~/Documents/test/docker/volume_test/hoge:/hoge centos:centos8
[root@3c0109bbdf3c /]# ls -la 
drwxr-xr-x   3 root root   96 Dec 15 12:24 hoge

How to use Multiple volume mount

You can use multiple -v options.

$ docker run -it --rm -v ~/Documents/test/docker/volume_test/a.txt:/a.txt -v ~/Documents/test/docker/volume_test/b.txt:/b.txt centos:centos8
[root@19354f136991 /]# ls -la 
-rw-r--r--   1 root root    0 Dec 15 12:14 a.txt
-rw-r--r--   1 root root    0 Dec 15 12:14 b.txt

How to use mount docker volume

You can mount docker volumes by changing the value of the -v option I used the absolute path when mounting the host directory, but specify the volume name when mounting the docker volume -v volume name: container path

$ docker run -it --rm -v hoge:/hoge centos:centos8 
[root@31ac4d5be7fa /]# ls -la 
drwxr-xr-x   2 root root 4096 Dec 15 12:40 hoge
[root@31ac4d5be7fa /]# touch hoge/a.txt
[root@31ac4d5be7fa /]# exit

--Since the container is started with rm attached, the container is destroyed at the time of exit.
↓ Create a container again
$ docker run -it --rm -v hoge:/hoge centos:centos8 

↓ Since hoge is a docker volume, you can see that it is persisted.
[root@4c03049e11c9 /]# ls hoge
a.txt

Check docker volume with command

$ docker volume ls 
DRIVER    VOLUME NAME
local     hoge

You do not have to specify the volume name when mounting the docker volume. A volume for which no volume name is specified is called an anonymous volume. In that case, specify only the container path. -v container path

$ docker run -it --rm -v /fuga centos:centos8 
[root@6806bc7b9bcf /]# ls -la 
drwxr-xr-x   2 root root 4096 Dec 15 12:44 fuga

However, if you do not specify the volume name, the volume name will be a hash value and it will be difficult to understand what volume it is, so it is not recommended.

$ docker volume ls 
DRIVER    VOLUME NAME
local     16fba3a332888fcd049a7dd569d6d27f360cc07ef58000bfee93f25e85416df9

Also, when started with no volume name + --rm, a directory was created in the container, but volume was not created and was not made persistent. Volume was created when done without --rm. (Is that the behavior like this?)

docker volume command

The docker volume command has the following:

$ docker volume 

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

Run 'docker volume COMMAND --help' for more information on a command.

Display the volume list with ls. Delete unused local volume with prune Delete a specific volume with rm

About docker volume inspect

inspect = Inspect. It seems that it will tell you the detailed information of volume. You can see where the volume is stored by looking at the Mount point. This path is not the path on the mac, but the path on the vm where the docker engine is running.

$ docker volume ls 
DRIVER    VOLUME NAME
local     16fba3a332888fcd049a7dd569d6d27f360cc07ef58000bfee93f25e85416df9
local     bbb
local     hoge

$ docker volume inspect hoge
[
    {
        "CreatedAt": "2020-12-15T12:40:52Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/hoge/_data",
        "Name": "hoge",
        "Options": null,
        "Scope": "local"
    }
]

reference

official https://matsuand.github.io/docs.docker.jp.onthefly/storage/volumes/

Recommended Posts

docker volume checked
Docker volume performance tuning
docker
[WIP] Use NFS for Docker Volume
docker memo
kubernetes + docker
spring × docker
About Docker
Docker Intellij
Docker basics
Docker installation
About Docker
Docker command
Docker memorandum
Understand Docker
Docker memorandum
Data management using volume in Docker (personal memorandum)
Make Volume faster when using Docker with vscode.
Docker mount volume permission is managed with fixuid