How to deal with persistent storage (e.g. databases) in docker -
how people deal persistent storage docker containers? using approach: build image, e.g. postgres, , start container with
docker run --volumes-from c0dbc34fd631 -d app_name/postgres
imho, has drawback, must not ever (by accident) delete container "c0dbc34fd631".
another idea mount host volumes "-v" container, however, userid within container not match userid host, , permissions might messed up.
note: instead of --volumes-from 'cryptic_id'
can use --volumes-from my-data-container
my-data-container
name assigned data-only container, e.g. docker run --name my-data-container ...
(see accepted answer)
docker 1.9.0 , above
use volume api
docker volume create --name hello docker run -d -v hello:/container/path/for/volume container_image my_command
this means data container pattern must abandoned in favour of new volumes.
actually volume api better way achieve data-container pattern.
if create container -v volume_name:/container/fs/path
docker automatically create named volume can:
- be listed through
docker volume ls
- be identified through
docker volume inspect volume_name
- backed normal dir
- backed before through
--volumes-from
connection
the new volume api adds useful command let identify dangling volumes:
docker volume ls -f dangling=true
and remove through name:
docker volume rm <volume name>
as @mpugach underlines in comments can rid of dangling volumes nice 1 liner:
docker volume rm $(docker volume ls -f dangling=true -q) # or using 1.13.x docker volume prune
docker 1.8.x , below
the approach seems work best production use data container.
the data container run on barebone image , nothing except exposing data volume.
then can run other container have access data container volumes:
docker run --volumes-from data-container some-other-container command-to-execute
in this blog post there description of called container volume pattern clarifies main point of having data containers.
docker documentation has definitive description of container volume/s pattern.
following backup/restore procedure docker 1.8.x , below
backup:
sudo docker run --rm --volumes-from data -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data
- --rm: remove container when exits
- --volumes-from data: attach volumes shared data container
- -v $(pwd):/backup: bind mount current directory container; write tar file
- busybox: small simpler image - quick maintenance
- tar cvf /backup/backup.tar /data: creates uncompressed tar file of files in /data directory
restore:
# create new data container $ sudo docker run -v /data -name data2 busybox true # untar backup files new container᾿s data volume $ sudo docker run --rm --volumes-from data2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar data/ data/sven.txt # compare original container $ sudo docker run --rm --volumes-from data -v `pwd`:/backup busybox ls /data sven.txt
here nice article excellent brian goff explaining why use same image container , data container.
Comments
Post a Comment