SLES 15
Sponsored Link

Docker : Use Persistent Storage2019/01/22

 
When Container is removed, data in it are also lost, so it's necessary to use external filesystem in Container as persistent storage if you need.
[1] For exmaple, create a Container only for using to save data as a storage server with an image busybox.
dlp:~ #
vi Dockerfile
# create new

FROM busybox
MAINTAINER ServerWorld <admin@srv.world>

VOLUME /storage
CMD /bin/sh

# build image

dlp:~ #
docker build -t storage .
dlp:~ #
docker images

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
storage                  latest              7d75db1a210e        7 seconds ago       1.2MB
web_server               latest              3fc464a24f9a        8 minutes ago       549MB
srv.world/fedora_httpd   latest              5b6251fe21ea        15 minutes ago      499MB
fedora                   latest              26ffec5b4a8a        5 days ago          274MB
busybox                  latest              3a093384ac30        3 weeks ago         1.2MB

# generate a Container with any name you like

dlp:~ #
docker run -it --name storage_server storage

/ #
exit
[2] To use the Container above as a Storage Server from other Containers, add an option [--volumes-from].
dlp:~ #
docker run -it --name fedora_server --volumes-from storage_server fedora /bin/bash
[root@a764aeaafc44 /]#
df -hT

Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/vda2      btrfs   40G  3.6G   37G  10% /
tmpfs          tmpfs   64M     0   64M   0% /dev
tmpfs          tmpfs  8.7G     0  8.7G   0% /sys/fs/cgroup
/dev/vda2      btrfs   40G  3.6G   37G  10% /storage
shm            tmpfs   64M     0   64M   0% /dev/shm
tmpfs          tmpfs  8.7G   40K  8.7G   1% /run/secrets/SUSEConnect
tmpfs          tmpfs  8.7G     0  8.7G   0% /proc/acpi
tmpfs          tmpfs  8.7G     0  8.7G   0% /proc/scsi
tmpfs          tmpfs  8.7G     0  8.7G   0% /sys/firmware

[root@a764aeaafc44 /]#
echo "persistent storage" >> /storage/testfile.txt

[root@a764aeaafc44 /]#
ls -l /storage

total 4
-rw-r--r-- 1 root root 19 Jan 22 02:16 testfile.txt
[3] Make sure datas are saved to run a Container of Storage Server like follows.
dlp:~ #
docker start storage_server

dlp:~ #
docker exec -it storage_server cat /storage/testfile.txt

persistent storage
[4] For other way to save data in external filesystem, it's possible to mount a directory on Docker Host into Containers.
# create a directory

dlp:~ #
mkdir -p /var/lib/docker/disk01

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

dlp:~ #
docker run -it -v /var/lib/docker/disk01:/mnt fedora /bin/bash
[root@94da11638237 /]#
df -hT

Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/vda2      btrfs   40G  3.6G   37G  10% /
tmpfs          tmpfs   64M     0   64M   0% /dev
tmpfs          tmpfs  8.7G     0  8.7G   0% /sys/fs/cgroup
/dev/vda2      btrfs   40G  3.6G   37G  10% /mnt
shm            tmpfs   64M     0   64M   0% /dev/shm
tmpfs          tmpfs  8.7G   40K  8.7G   1% /run/secrets/SUSEConnect
tmpfs          tmpfs  8.7G     0  8.7G   0% /proc/acpi
tmpfs          tmpfs  8.7G     0  8.7G   0% /proc/scsi
tmpfs          tmpfs  8.7G     0  8.7G   0% /sys/firmware

[root@94da11638237 /]#
cat /mnt/testfile.txt

persistent storage
Matched Content