Docker Chapter VI storage volumes

Several forms of Docker volume

Stateful container has data persistence requirements. When mentioned in the previous article, Docker using hierarchical file system, the file system changes occurred in the uppermost layer of the container. Over the life of the vessel, which is ongoing, including the container after being stopped. However, when the container is removed, the data layer also will be removed. Thus, Docker form of volume (volume) to provide the persistent store to the container. There are several forms as follows Docker volume.

 

  Do not use Docker volume

By default, the container does not use any volume, this time, the data containers are stored in a container, the container's life cycle only in the memory, will be deleted as the container is removed. Of course, you can also use the docker commit command it persisted as a new image.

  

  Data volume (data volume)

 

A data volume is a particular directory container, bypassing the Union file system. It is designed to save data, regardless of the vessel's life cycle. Therefore, when you delete a container, Docker certainly will not automatically delete a volume. There are several ways to use the data volume:

  (1) using the "-v container within the directory" form

[root@localhost ~]# docker run -dit --name v1 -v /test_data busybox
ca436842125042039399e9b8c5854d1e45e27c9798f4f4547749f6f07593643e
[root@localhost ~]# 

  

  "Over Mounts": [            -v / test_data create a directory test_data docker container and the file system by combining directory created automatically docker hanging together, I understand and container corresponding to the host each mapping file directory
            {
                "Type": "volume",
                "Name": "16eebb552f650fe30b7e896bd7d1b30b530e8ec566809f133c7e277665316f45",
                "Source": "/var/lib/docker/volumes/16eebb552f650fe30b7e896bd7d1b30b530e8ec566809f133c7e277665316f45/_data",
                "Destination": "/test_data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

  

[root@localhost ~]# docker exec -it ca4368421250 sh

/ # Ls
bin        etc        proc       sys        tmp        var
dev        home       root       test_data  usr
/ # 

 

In fact, after busybox container is deleted, / var / lib / docker / volumes / 16eebb552f650fe30b7e896bd7d1b30b530e8ec566809f133c7e277665316f45 / _data directory and its contents are still preserved, but the start of a new container can no longer use this directory, that is to say, there are data not automatically be reused 

  

-V used to mount a directory on the host to the directory container  

#format :    -v host_file:docker_faile
 
[root@localhost /]# docker run -it --name v2 -v /boot:/boot busybox / # Ls bin boot dev etc home proc root sys tmp usr var / # cd boot /boot # ls System.map-3.10.0-957.el7.x86_64 config-3.10.0-957.el7.x86_64 efi grub grub2 initramfs-0-rescue-3e3864368ff04293b38d71342d1cb389.img initramfs-3.10.0-957.el7.x86_64.img initramfs-3.10.0-957.el7.x86_64kdump.img symvers-3.10.0-957.el7.x86_64.gz vmlinuz-0-rescue-3e3864368ff04293b38d71342d1cb389 vmlinuz-3.10.0-957.el7.x86_64

  

Using data container

If data to be shared between the containers, preferably using data container. This container will not run the application, but only to mount a volume. such as:

Creating a data container:
#format: --volumes-from v2 # inherited a container volume file

[root@localhost boot]# docker run -it --name v3 --volumes-from v2 busybox / # / # Ls bin boot dev etc home proc root sys tmp usr var / # ls /boot System.map-3.10.0-957.el7.x86_64 config-3.10.0-957.el7.x86_64 efi grub grub2 initramfs-0-rescue-3e3864368ff04293b38d71342d1cb389.img initramfs-3.10.0-957.el7.x86_64.img initramfs-3.10.0-957.el7.x86_64kdump.img symvers-3.10.0-957.el7.x86_64.gz vmlinuz-0-rescue-3e3864368ff04293b38d71342d1cb389 vmlinuz-3.10.0-957.el7.x86_64

  

Use docker volume command

[root@localhost boot]# docker volume --help

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.
[root@localhost boot]# 

Docker new version introduces a docker volume commands to manage Docker volume.

(1) using the default 'local' driver to create a volume

[root@localhost boot]# docker volume create --name voll
full
[root@localhost boot]# docker volume inspect voll
[
    {
        "CreatedAt": "2019-06-12T22:58:45-04:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/voll/_data",
        "Name": "voll",
        "Options": {},
        "Scope": "local"
    }
]
[root@localhost boot]# 

Using this volume

[root@localhost _data]# docker run -it --name v6 -v voll:/volume busybox
/ # Ls
bin     dev     etc     home    proc    root    sys     tmp     usr     var     volume
/ # cd volume/
/volume # ls
1qaz
/volume # 

  

You can delete the volume of the container when the container is removed using a docker rm -vf command.

 

[Root @ localhost /] # docker volume ls # forcibly remove the container and the volume file
DRIVER              VOLUME NAME
local               d8a9ddb3f7ce482f170b88485b1f5802a9437c0921daf5e77ab170a18bf4ad4e
[root@localhost /]# docker rm -vf v1
v1
[root@localhost /]# docker volume ls
DRIVER              VOLUME NAME
[root@localhost /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost /]# 

  

 

There are many scripts on github can automate clean up alone volume, such as:

  

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/zy09/p/11015035.html