AlmaLinux 9
Sponsored Link

Podman : Use External Storage2023/03/01

When a Container is removed, data in it are also lost, so it's necessary to use external storage on Containers if you'd like to save your data on Containers.
[1] It's possible to mount a directory on Docker Host into Containers.
# create a directory for containers data

[root@dlp ~]#
mkdir /var/lib/containers/disk01

[root@dlp ~]#
echo "persistent storage" >> /var/lib/containers/disk01/testfile.txt

# run a Container with mounting the directory above on [/mnt]
# if SELinux is [Enforcing], it needs to add [--privileged] option

[root@dlp ~]#
podman run --privileged -it -v /var/lib/containers/disk01:/mnt almalinux /bin/bash
[root@e06b7c743e5c /]#
df -hT /mnt

Filesystem                 Type  Size  Used Avail Use% Mounted on
/dev/mapper/almalinux-root xfs    71G  2.6G   69G   4% /mnt

[root@43b1ee70639f /]#
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 ~]#
podman volume create volume01

# display volume list

[root@dlp ~]#
podman volume ls

local       volume01

# display details of [volume01]

[root@dlp ~]#
podman volume inspect volume01

          "Name": "volume01",
          "Driver": "local",
          "Mountpoint": "/var/lib/containers/storage/volumes/volume01/_data",
          "CreatedAt": "2022-03-14T11:04:49.400806748+09:00",
          "Labels": {},
          "Scope": "local",
          "Options": {},
          "MountCount": 0,
          "NeedsCopyUp": true,
          "NeedsChown": true

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

[root@dlp ~]#
podman run -it -v volume01:/mnt almalinux
[root@46a957b03ba3 /]#
df -hT /mnt

Filesystem          Type  Size  Used Avail Use% Mounted on
/dev/mapper/cs-root xfs    71G  2.6G   69G   4% /mnt

[root@46a957b03ba3 /]#
echo "Podman Volume test" > /mnt/testfile.txt

[root@46a957b03ba3 /]#
[root@dlp ~]#
cat /var/lib/containers/storage/volumes/volume01/_data/testfile.txt

Podman Volume test
# possible to mount from other containers

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

Podman Volume test
# to remove volumes, do like follows

[root@dlp ~]#
podman volume rm volume01

Error: volume volume01 is being used by the following container(s): 46a957b03ba3c2cba9b6d33b247ebf813af7cb2889a6fc3452d97fdc391fe1c7, e62adf721fd07d2a6a5cccafdbf006f9f598b35da81de358c5e1dafef4644812: volume is being used

# 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 ~]#
podman rm 46a957b03ba3c2cba9b6d33b247ebf813af7cb2889a6fc3452d97fdc391fe1c7

[root@dlp ~]#
podman rm e62adf721fd07d2a6a5cccafdbf006f9f598b35da81de358c5e1dafef4644812
[root@dlp ~]#
podman volume rm volume01

Matched Content