Docker data volume

Posted by affc on Tue, 04 Jan 2022 05:13:53 +0100

storage

  1. Docker provides two kinds of data storage resources for containers:

    • Image layer and container layer managed by storage driver: applicable to stateless applications, and there is no data that needs to be persisted, such as busybox
    • Data Volume: container that needs persistent data
  2. The hierarchical structure makes the creation, sharing and distribution of images and containers very efficient, thanks to the Docker storage driver. Docker supports a variety of storage drivers, including AUFS, Device Mapper, Btrfs, overlay FS, VFS and ZFS. No driver can adapt to all scenarios. The best practice is to give priority to the default storage driver of Linux distribution. During docker installation, the default driver will be selected according to the current system configuration. The default driver has the best stability because the default driver has been rigorously tested on the distribution.

    1. Focus on view Storage Driver: overlay2 information
    $ docker info
    Client:
     Debug Mode: false
    
    Server:
     Containers: 3
      Running: 3
      Paused: 0
      Stopped: 0
     Images: 109
     Server Version: 19.03.9
     Storage Driver: overlay2
      Backing Filesystem: xfs
      Supports d_type: true
      Native Overlay Diff: true
     Logging Driver: json-file
     Cgroup Driver: cgroupfs
     Plugins:
      Volume: local
      Network: bridge host ipvlan macvlan null overlay
      Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
     Swarm: inactive
     Runtimes: runc
     Default Runtime: runc
     Init Binary: docker-init
     containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
     runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
     init version: fec3683
     Security Options:
      seccomp
       Profile: default
     Kernel Version: 4.4.227-1.el7.elrepo.x86_64
     Operating System: CentOS Linux 7 (Core)
     OSType: linux
     Architecture: x86_64
     CPUs: 2
     Total Memory: 3.842GiB
     Name: jannal.docker.com
     ID: XKUN:GE65:TC7N:YAEW:VWVN:247J:EQ24:HZ2S:YSFG:ZL7P:IBUK:RN2L
     Docker Root Dir: /var/lib/docker
     Debug Mode: false
     Registry: https://index.docker.io/v1/
     Labels:
     Experimental: false
     Insecure Registries:
      127.0.0.0/8
     Registry Mirrors:
      https://7m2taepc.mirror.aliyuncs.com/
     Live Restore Enabled: false
    

Data Volume

  1. Data Volume is essentially a directory or file in the Docker Host file system and can be mount ed directly into the container file system

    • Data volumes are directories or files, not unformatted disks (block devices)
    • The container can read and write data in volume
    • volume data can be saved permanently, even if the container that uses it has been destroyed. Data volumes can be shared and reused between containers, and the transfer of data between containers will become efficient and convenient
    • Volume is actually a part of the docker host file system, so the capacity of volume depends on the unused space of the file system. At present, there is no method to set the capacity of volume
    • Docker provides two types of volumes: bind mount and docker managed volume
  2. bind mount: mount the existing directory or file on the host to the container. The default is readable and writable

    • -The format of v is < host Path >: < container path >

    • Set read-only permission - v /root:/root:ro

    • Specify a file - v /root/a.txt:/root/a_new.txt. When using a single file, it should be noted that the source file in the host must exist, or it will be given to the container as a new directory bind mount.

    • Specify a directory - v /root/install/:/root:install/:rw

  3. docker managed volume:

    • bind mount needs to specify a specific path to the host file system, which limits the portability of the container. docker managed volume does not need to specify the mount source, but only the mount point
    • -v
  4. Example

    adopt-v tell docker Need one data volume,And put it mount reach/root/a.txt
    $ docker run -it --name data-busybox -v "/root/a.txt" busybox 
    
    see data volume Specific location
    $ docker inspect c94fb9f45eea
    ...ellipsis...
    "Mounts": [
        {
            "Type": "volume",
            "Name": "6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7",
            "Source": "/var/lib/docker/volumes/6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7/_data",
            "Destination": "/root/a.txt",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ]
    ...ellipsis...
    
    $ docker volume ls
    DRIVER              VOLUME NAME
    local               3232b71e69b26e362ac8032577982d7a85847537d4eb4aa2cf54270419988621
    local               6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7
    
    docker volume Only view docker managed volume,Not yet bind mount;
    At the same time, I can't know volume The corresponding container, this information has to rely on docker inspect. 
    $ docker volume inspect 6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7
    [
        {
            "CreatedAt": "2021-09-17T10:27:17+08:00",
            "Driver": "local",
            "Labels": null,
            "Mountpoint": "/var/lib/docker/volumes/6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7/_data",
            "Name": "6504feb75a4eafeb3b9049da2bacc6d37a20b046f23e752d1e48774510a18bd7",
            "Options": null,
            "Scope": "local"
        }
    ]
    
  5. Whenever the container requests mount docker managed volume, docker will generate a directory under / var/lib/docker/volumes, which is the mount source. Since volume is located in the directory in host and is generated only when the container is started, you need to copy the shared data to volume

Copy data

  1. docker cp can copy data between the container and the Host. You can also directly copy it to / var/lib/docker/volumes/xxx through the cp command of Linux

Sharing data between containers

  1. Shared data mode

    • The shared data is placed in the bind mount and then mounted to multiple containers
    • Using volume container (a container that specifically provides volume for other containers), the provided volume can be bind mount or docker managed volume
  2. Create data volume container

    1. The function of data volume is only to provide data, and it does not need to be running itself
    $ docker create --name vc_my \
     -v /root/a.txt \
     -v /root/b.txt:/root/bb.txt \
     busybox
    ea764715a12d9073d2722ac07fd54a819996f1d3fa8c46252d9728ffb23a0b2d
     
     Viewing volume containers
     $ docker inspect vc_my
     
     --volumes-from Using data volume containers
     $ docker run -it --name data0-busybox -volumes-from=vc_my busybox
    
  3. Delete orphan volume

    $ docker volume rm $(docker volume ls -q)