设为首页 收藏本站
查看: 1417|回复: 0

[经验分享] 转:docker:Managing Data in Containers

[复制链接]

尚未签到

发表于 2015-4-18 05:22:06 | 显示全部楼层 |阅读模式
  http://docs.docker.com/userguide/dockervolumes/
  

Managing Data in Containers
  So far we've been introduced to some basic Docker concepts, seen how to work with Docker images as well as learned about networking and links between containers. In this section we're going to discuss how you can manage data inside and between your Docker containers.
  We're going to look at the two primary ways you can manage data in Docker.


  • Data volumes, and
  • Data volume containers.

Data volumes
  A data volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide several useful features for persistent or shared data:


  • Data volumes can be shared and reused between containers
  • Changes to a data volume are made directly
  • Changes to a data volume will not be included when you update an image
  • Volumes persist until no containers use them

Adding a data volume
  You can add a data volume to a container using the -v flag with the docker run command. You can use the -v multiple times in a single docker run to mount multiple data volumes. Let's mount a single volume now in our web application container.

$ sudo docker run -d -P --name web -v /webapp training/webapp python app.py
  This will create a new volume inside a container at /webapp.

  Note: You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image.


Mount a Host Directory as a Data Volume
  In addition to creating a volume using the -v flag you can also mount a directory from your own host into a container.

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
  This will mount the local directory, /src/webapp, into the container as the /opt/webapp directory. This is very useful for testing, for example we can mount our source code inside the container and see our application at work as we change the source code. The directory on the host must be specified as an absolute path and if the directory doesn't exist Docker will automatically create it for you.

  Note: This is not available from a Dockerfile due to the portability and sharing purpose of it. As the host directory is, by its nature, host-dependent, a host directory specified in a Dockerfile probably wouldn't work on all hosts.

  Docker defaults to a read-write volume but we can also mount a directory read-only.

$ sudo docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
  Here we've mounted the same /src/webapp directory but we've added the ro option to specify that the mount should be read-only.

Mount a Host File as a Data Volume
  The -v flag can also be used to mount a single file - instead of just directories - from the host machine.

$ sudo docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash
  This will drop you into a bash shell in a new container, you will have your bash history from the host and when you exit the container, the host will have the history of the commands typed while in the container.

  Note: Many tools used to edit files including vi and sed --in-place may result in an inode change. Since Docker v1.1.0, this will produce an error such as "sed: cannot rename ./sedKdJ9Dy: Device or resource busy". In the case where you want to edit the mounted file, it is often easiest to instead mount the parent directory.


Creating and mounting a Data Volume Container
  If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it's best to create a named Data Volume Container, and then to mount the data from it.
  Let's create a new named container with a volume to share.

$ sudo docker run -d -v /dbdata --name dbdata training/postgres echo Data-only container for postgres
  You can then use the --volumes-from flag to mount the /dbdata volume in another container.

$ sudo docker run -d --volumes-from dbdata --name db1 training/postgres
  And another:

$ sudo docker run -d --volumes-from dbdata --name db2 training/postgres
  You can use multiple --volumes-from parameters to bring together multiple data volumes from multiple containers.
  You can also extend the chain by mounting the volume that came from the dbdata container in yet another container via the db1 or db2 containers.

$ sudo docker run -d --name db3 --volumes-from db1 training/postgres
  If you remove containers that mount volumes, including the initial dbdata container, or the subsequent containers db1 and db2, the volumes will not be deleted. To delete the volume from disk, you must explicitly call docker rm -v against the last container with a reference to the volume. This allows you to upgrade, or effectively migrate data volumes between containers.

Backup, restore, or migrate data volumes
  Another useful function we can perform with volumes is use them for backups, restores or migrations. We do this by using the --volumes-from flag to create a new container that mounts that volume, like so:

$ sudo docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
  Here we've launched a new container and mounted the volume from the dbdata container. We've then mounted a local host directory as /backup. Finally, we've passed a command that uses tar to backup the contents of the dbdata volume to a backup.tar file inside our /backup directory. When the command completes and the container stops we'll be left with a backup of our dbdata volume.
  You could then restore it to the same container, or another that you've made elsewhere. Create a new container.

$ sudo docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
  Then un-tar the backup file in the new container's data volume.

$ sudo docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar
  You can use this techniques above to automate backup, migration and restore testing using your preferred tools.

Next steps
  Now we've learned a bit more about how to use Docker we're going to see how to combine Docker with the services available on Docker Hub including Automated Builds and private repositories.
  Go to Working with Docker Hub.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58151-1-1.html 上篇帖子: Docker学习笔记之一,搭建一个JAVA Tomcat运行环境 下篇帖子: 深入浅出Docker(一):Docker核心技术预览
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表