Docker : Use Persistent Storage


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             VIRTUAL SIZE
storage             latest              65c5cce81114        20 seconds ago      1.113 MB
docker.io/centos    latest              14dab3d40372        6 days ago          194.7 MB
docker.io/busybox   latest              fc0db02f3072        13 days ago         1.113 MB

# generate a Container with any name you like

[root@dlp ~]# 
docker run -i -t --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 -i -t --name centos_server --volumes-from storage_server centos /bin/bash
[root@b9b7a2d35b51 /]# 
df -hT 

Filesystem                              Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:0-67164897-..... ext4    99G  266M   94G   1% /
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /dev
shm                                     tmpfs   64M     0   64M   0% /dev/shm
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/mapper/centos-root                 xfs     27G  3.2G   24G  13% /storage
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /run/secrets

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

[root@b9b7a2d35b51 /]# 
ll /storage 

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

[root@dlp ~]# 
docker attach 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@www ~]# 
mkdir -p /var/docker/disk01 

[root@www ~]# 
echo "persistent storage" >> /var/docker/disk01/testfile.txt
# run a Container with mounting the directory above on /mnt

[root@www ~]# 
docker run -i -t -v /var/docker/disk01:/mnt centos /bin/bash
[root@bc9a4d5578a6 /]# 
df -hT 

Filesystem                              Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-253:0-67164897-..... ext4    99G  266M   94G   1% /
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /dev
shm                                     tmpfs   64M     0   64M   0% /dev/shm
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/mapper/centos-root                 xfs     27G  3.2G   24G  13% /mnt
tmpfs                                   tmpfs  2.0G     0  2.0G   0% /run/secrets

[root@bc9a4d5578a6 /]# 
cat /mnt/testfile.txt 

persistent storage

No comments:

Post a Comment