Ubuntu 20.04
Sponsored Link

Docker : Use External Storage2020/06/02

 
When containers are removed, data in them 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 for containers data

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 ubuntu /bin/bash
root@c2bf4bfa9980:/#
df -hT

Filesystem                        Type     Size  Used Avail Use% Mounted on
overlay                           overlay   77G  7.2G   66G  10% /
tmpfs                             tmpfs     64M     0   64M   0% /dev
tmpfs                             tmpfs    7.9G     0  7.9G   0% /sys/fs/cgroup
shm                               tmpfs     64M     0   64M   0% /dev/shm
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      77G  7.2G   66G  10% /mnt
tmpfs                             tmpfs    7.9G     0  7.9G   0% /proc/acpi
tmpfs                             tmpfs    7.9G     0  7.9G   0% /proc/scsi
tmpfs                             tmpfs    7.9G     0  7.9G   0% /sys/firmware

root@c2bf4bfa9980:/#
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": "2021-05-12T05:09:43Z",
        "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 ubuntu
root@f7d1e8c1ec6c:/#
df -hT /mnt

Filesystem                        Type  Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv ext4   77G  7.2G   66G  10% /mnt

root@f7d1e8c1ec6c:/#
echo "Docker Volume test" > /mnt/testfile.txt

root@f7d1e8c1ec6c:/#
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 ubuntu /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 - [f7d1e8c1ec6c2513812cae796e3b0ceb5483dd10da4b22110ca2e973c3349ca5, 4ff0c7a6155cff247140680ced1452d57d1e2e388f2910532266e80bd2c41285]

# 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 f7d1e8c1ec6c2513812cae796e3b0ceb5483dd10da4b22110ca2e973c3349ca5

root@dlp:~#
docker rm 4ff0c7a6155cff247140680ced1452d57d1e2e388f2910532266e80bd2c41285
root@dlp:~#
docker volume rm volume01

volume01
Matched Content