Ubuntu 19.04
Sponsored Link

Docker : Use Persistent Storage2019/04/25

 
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.
root@dlp:~#
vi Dockerfile
# create new

FROM busybox
MAINTAINER ServerWorld <admin@srv.world>

VOLUME /storage
CMD /bin/sh

# build image

root@dlp:~#
docker build -t storage ./
root@dlp:~#
docker images

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
storage                    latest              23e90a7ff6f9        2 seconds ago       1.2MB
web_server                 latest              11c1025998ec        4 minutes ago       306MB
srv.world/ubuntu_apache2   latest              c0d10606acde        9 minutes ago       210MB
busybox                    latest              af2f74c517aa        3 weeks ago         1.2MB
ubuntu                     latest              94e814e2efa8        6 weeks ago         88.9MB

# generate a Container with any name you like

root@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].
root@dlp:~#
docker run -it --name ubuntu_server --volumes-from storage_server ubuntu /bin/bash
root@273e23d34f47:/#
df -hT

Filesystem                  Type     Size  Used Avail Use% Mounted on
overlay                     overlay   29G  2.6G   25G  10% /
tmpfs                       tmpfs     64M     0   64M   0% /dev
tmpfs                       tmpfs    5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/mapper/ubuntu--vg-root ext4      29G  2.6G   25G  10% /storage
shm                         tmpfs     64M     0   64M   0% /dev/shm
tmpfs                       tmpfs    5.9G     0  5.9G   0% /proc/acpi
tmpfs                       tmpfs    5.9G     0  5.9G   0% /proc/scsi
tmpfs                       tmpfs    5.9G     0  5.9G   0% /sys/firmware

root@273e23d34f47:/#
echo "persistent storage" >> /storage/testfile.txt

root@273e23d34f47:/#
ls -l /storage

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

storage_server
root@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

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@4c2907711509:/#
df -hT

Filesystem                  Type     Size  Used Avail Use% Mounted on
overlay                     overlay   29G  2.6G   25G  10% /
tmpfs                       tmpfs     64M     0   64M   0% /dev
tmpfs                       tmpfs    5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/mapper/ubuntu--vg-root ext4      29G  2.6G   25G  10% /mnt
shm                         tmpfs     64M     0   64M   0% /dev/shm
tmpfs                       tmpfs    5.9G     0  5.9G   0% /proc/acpi
tmpfs                       tmpfs    5.9G     0  5.9G   0% /proc/scsi
tmpfs                       tmpfs    5.9G     0  5.9G   0% /sys/firmware

root@4c2907711509:/#
cat /mnt/testfile.txt

persistent storage
Matched Content