Docker : Use Persistent Storage2019/05/10 |
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] |
This example is based on the environment that SELnux is Permissive or Disabled.
|
[2] | 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 .
docker images REPOSITORY TAG IMAGE ID CREATED SIZE storage latest ea9374e1f84d 4 seconds ago 1.2 MB web_server latest 405d738492e1 5 minutes ago 566 MB srv.world/fedora_httpd latest 3e633f065d3b 12 minutes ago 595 MB docker.io/busybox latest 64f5d945efcc 9 hours ago 1.2 MB docker.io/fedora latest d09302f77cfc 8 weeks ago 275 MB # generate a Container with any name you like [root@dlp ~]# docker run -it --name storage_server storage / # exit
|
[3] | To use the Container above as a Storage Server from other Containers, add an option [--volumes-from]. |
[root@dlp ~]#
[root@8eb1993f6f6e /]# docker run -it --name fedora_server --volumes-from storage_server fedora /bin/bash df -hT Filesystem Type Size Used Avail Use% Mounted on overlay overlay 15G 3.1G 12G 21% / tmpfs tmpfs 5.9G 0 5.9G 0% /dev tmpfs tmpfs 5.9G 0 5.9G 0% /sys/fs/cgroup /dev/mapper/fedora-root xfs 15G 3.1G 12G 21% /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@8eb1993f6f6e /]# echo "persistent storage" >> /storage/testfile.txt [root@8eb1993f6f6e /]# ls -l /storage total 4 -rw-r--r--. 1 root root 19 May 10 07:28 testfile.txt |
[4] | Make sure datas are saved to run a Container of Storage Server like follows. |
[root@dlp ~]# docker start storage_server [root@dlp ~]# docker exec -it storage_server cat /storage/testfile.txt persistent storage |
[5] | 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 fedora /bin/bash
df -hT Filesystem Type Size Used Avail Use% Mounted on overlay overlay 15G 3.1G 12G 21% / tmpfs tmpfs 5.9G 0 5.9G 0% /dev tmpfs tmpfs 5.9G 0 5.9G 0% /sys/fs/cgroup /dev/mapper/fedora-root xfs 15G 3.1G 12G 21% /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@7543e4dffb4b /]# cat /mnt/testfile.txt persistent storage |
Sponsored Link |