FreeBSD 14
Sponsored Link

Podman : Use External Storage2024/02/29

 
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.
[1] It's possible to mount a directory on Podman Host into Containers.
root@dlp:~ #
podman images

REPOSITORY                TAG         IMAGE ID      CREATED       SIZE
localhost/freebsd-nginx   latest      a0a053cc78a3  16 hours ago  1.17 GB
localhost/freebsd-httpd   latest      add46dedb2b7  22 hours ago  1.44 GB
localhost/freebsd-base    latest      2527bfa5eeb4  42 hours ago  1.05 GB
quay.io/centos/centos     stream9     ce3ac91d4020  2 weeks ago   161 MB
docker.io/library/ubuntu  latest      3db8720ecbf5  2 weeks ago   80.4 MB

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

root@dlp:~ #
podman run -it -v /var/lib/containers/disk01:/mnt freebsd-base /bin/sh
#
df -hT /mnt

Filesystem                  Type      Size    Used   Avail Capacity  Mounted on
/var/lib/containers/disk01  nullfs     73G    1.6G     71G     2%    /mnt

#
cat /mnt/testfile.txt

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

root@dlp:~ #
podman volume create volume01

volume01
# display volume list

root@dlp:~ #
podman volume ls

DRIVER      VOLUME NAME
local       volume01

# display details of [volume01]

root@dlp:~ #
podman volume inspect volume01

[
     {
          "Name": "volume01",
          "Driver": "local",
          "Mountpoint": "/var/db/containers/storage/volumes/volume01/_data",
          "CreatedAt": "2024-02-29T08:44:43.156912115+09:00",
          "Labels": {},
          "Scope": "local",
          "Options": {},
          "MountCount": 0,
          "NeedsCopyUp": true,
          "NeedsChown": true,
          "LockNumber": 46
     }
]

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

root@dlp:~ #
podman run -it -v volume01:/mnt freebsd-base /bin/sh
#
df -hT /mnt

Filesystem                                         Type      Size    Used   Avail Capacity  Mounted on
/var/db/containers/storage/volumes/volume01/_data  nullfs     73G    1.6G     71G     2%    /mnt

#
echo "Podman Volume test" > /mnt/testfile.txt

#
exit
root@dlp:~ #
cat /var/db/containers/storage/volumes/volume01/_data/testfile.txt

Podman Volume test
# possible to mount from other containers

root@dlp:~ #
podman run -v volume01:/var/volume01 freebsd-base /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): 8b37e1975fe87beefc378c4e24c185dc849604e7defebb658976b9185212d19d, e026dd578bf559d6a9ec3e00c93cb113da5d427ea72f619c7f29ce7fccfe2f0f: 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 8b37e1975fe87beefc378c4e24c185dc849604e7defebb658976b9185212d19d

root@dlp:~ #
podman rm e026dd578bf559d6a9ec3e00c93cb113da5d427ea72f619c7f29ce7fccfe2f0f
root@dlp:~ #
podman volume rm volume01

volume01
Matched Content