Docker : Use Persistent Storage2018/06/12 |
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 ./
docker images REPOSITORY TAG IMAGE ID CREATED SIZE storage latest f250059f0ff5 12 seconds ago 1.15MB web_server latest 46fdaf40a0fe 6 minutes ago 313MB srv.world/ubuntu_apache2 latest b2d00f9d9fb4 About an hour ago 218MB ubuntu latest 113a43faa138 6 days ago 81.2MB busybox latest 8c811b4aec35 2 weeks ago 1.15MB # 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:~#
root@d36cc3eadc2f:/# docker run -it --name ubuntu_server --volumes-from storage_server ubuntu /bin/bash df -hT Filesystem Type Size Used Avail Use% Mounted on overlay overlay 78G 2.8G 71G 4% / tmpfs tmpfs 64M 0 64M 0% /dev tmpfs tmpfs 8.9G 0 8.9G 0% /sys/fs/cgroup /dev/mapper/ubuntu--vg-root ext4 78G 2.8G 71G 4% /storage shm tmpfs 64M 0 64M 0% /dev/shm tmpfs tmpfs 8.9G 0 8.9G 0% /proc/scsi tmpfs tmpfs 8.9G 0 8.9G 0% /sys/firmwareroot@d36cc3eadc2f:/# echo "persistent storage" >> /storage/testfile.txt root@d36cc3eadc2f:/# ls -l /storage total 4 -rw-r--r-- 1 root root 19 Jun 12 02:26 testfile.txt |
[3] | 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 |
[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
df -hT Filesystem Type Size Used Avail Use% Mounted on overlay overlay 78G 2.8G 71G 4% / tmpfs tmpfs 64M 0 64M 0% /dev tmpfs tmpfs 8.9G 0 8.9G 0% /sys/fs/cgroup /dev/mapper/ubuntu--vg-root ext4 78G 2.8G 71G 4% /mnt shm tmpfs 64M 0 64M 0% /dev/shm tmpfs tmpfs 8.9G 0 8.9G 0% /proc/scsi tmpfs tmpfs 8.9G 0 8.9G 0% /sys/firmwareroot@4526babc56ec:/# cat /mnt/testfile.txt persistent storage |
Sponsored Link |
|