CentOS 7
Sponsored Link

Docker : Use Persistent Storage
2015/12/21
 
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.
[[email protected] ~]#
vi Dockerfile
# create new

FROM busybox
MAINTAINER ServerWorld <[email protected]>

VOLUME /storage
CMD /bin/sh

# build image

[[email protected] ~]#
docker build -t storage .
[[email protected] ~]#
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

[[email protected] ~]#
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".
[[email protected] ~]#
docker run -i -t --name centos_server --volumes-from storage_server centos /bin/bash
[[email protected] /]#
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

[[email protected] /]#
echo "persistent storage" >> /storage/testfile.txt

[[email protected] /]#
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.
[[email protected] ~]#
docker start storage_server

[[email protected] ~]#
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

[[email protected] ~]#
mkdir -p /var/docker/disk01

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

[[email protected] ~]#
docker run -i -t -v /var/docker/disk01:/mnt centos /bin/bash
[[email protected] /]#
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

[[email protected] /]#
cat /mnt/testfile.txt

persistent storage