CentOS 8
Sponsored Link

Docker : Use External Storage2020/12/14

 
When Container is removed, data in it are also lost, so it's necessary to use external filesystem in Containers as persistent storages if you need.
[1] It's possible to mount a directory on Docker Host into Containers.
# create a directory

[root@dlp ~]#
mkdir -p /var/lib/docker/disk01

[root@dlp ~]#
echo "persistent storage" >> /var/lib/docker/disk01/testfile.txt
# run a Container with mounting the directory above on [/mnt]

[root@dlp ~]#
docker run -it -v /var/lib/docker/disk01:/mnt centos /bin/bash
[root@3af5df55cbcc /]#
df -hT

Filesystem          Type     Size  Used Avail Use% Mounted on
overlay             overlay   75G  3.0G   73G   4% /
tmpfs               tmpfs     64M     0   64M   0% /dev
tmpfs               tmpfs    7.8G     0  7.8G   0% /sys/fs/cgroup
shm                 tmpfs     64M     0   64M   0% /dev/shm
/dev/mapper/cl-root xfs       75G  3.0G   73G   4% /mnt
tmpfs               tmpfs    7.8G     0  7.8G   0% /proc/acpi
tmpfs               tmpfs    7.8G     0  7.8G   0% /proc/scsi
tmpfs               tmpfs    7.8G     0  7.8G   0% /sys/firmware

[root@3af5df55cbcc /]#
cat /mnt/testfile.txt

persistent storage
[2] It's also possible to configure external storage by Docker Data Volume command.
# create [volume01] volume

[root@dlp ~]#
docker volume create volume01

volume01
# display volume list

[root@dlp ~]#
docker volume ls

DRIVER    VOLUME NAME
local     volume01

# display details of [volume01]

[root@dlp ~]#
docker volume inspect volume01

[
    {
        "CreatedAt": "2020-12-11T10:33:36+09:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/volume01/_data",
        "Name": "volume01",
        "Options": {},
        "Scope": "local"
    }
]

# run a container with mounting [volume01] to [/mnt] on container

[root@dlp ~]#
docker run -it -v volume01:/mnt centos
[root@d97446251ba6 /]#
df -hT /mnt

Filesystem          Type  Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs    75G  3.0G   73G   4% /mnt

[root@d97446251ba6 /]#
echo "Docker Volume test" > /mnt/testfile.txt

[root@d97446251ba6 /]#
exit
[root@dlp ~]#
cat /var/lib/docker/volumes/volume01/_data/testfile.txt

Docker Volume test
# possible to mount from other containers

[root@dlp ~]#
docker run -v volume01:/var/volume01 centos /usr/bin/cat /var/volume01/testfile.txt

Docker Volume test
# to remove volumes, do like follows

[root@dlp ~]#
docker volume rm volume01

Error response from daemon: remove volume01: volume is in use - [d97446251ba6ca6adeecbed6cb4d7e94a77cbc83bfb2ee6a15ba5b1e7d918d6b, 35f196402ba86aa873cabda59ada3d80c98e022db2f69b70365be43b5e43299c]

# if some containers are using the volume you'd like to remove like above,

# it needs to remove target containers before removing a volume

[root@dlp ~]#
docker rm 35f196402ba86aa873cabda59ada3d80c98e022db2f69b70365be43b5e43299c

[root@dlp ~]#
docker rm d97446251ba6ca6adeecbed6cb4d7e94a77cbc83bfb2ee6a15ba5b1e7d918d6b
[root@dlp ~]#
docker volume rm volume01

volume01
Matched Content