Managing Data in Containers

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.

这一章介绍如何在docker容器之间管理数据

We‘re going to look at the two primary ways you can manage data in Docker.

在容器中管理数据的2个主要方法

  • 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:

数据卷是一个专门UFS的专门目录,它可以提供很多有用的特性或者共享数据

  • 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 until there are no containers still referencing those volumes. 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‘s 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 to restore 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.

Managing Data in Containers

时间: 2024-09-29 06:03:27

Managing Data in Containers的相关文章

Linking Containers Together

Linking Containers Together In the Using Docker section we touched on connecting to a service running inside a Docker container via a network port. This is one of the ways that you can interact with services and applications running inside Docker con

Docker 学习笔记

一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 – Docker Hub Docker 使用客户端-服务器 (C/S) 架构模式.Docker 客户端会与 Docker 守护进程进行通信.Docker 守护进程会处理复杂繁重的任务,例如建立.运行.发布你的 Docker 容器.Docker 客户端和守护进程可以运行在同一个系统上,当然你也可以使用 Docker

非常详细的 Docker 学习笔记

一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docker Hub Docker 使用客户端-服务器 (C/S) 架构模式.Docker 客户端会与 Docker 守护进程进行通信.Docker 守护进程会处理复杂繁重的任务,例如建立.运行.发布你的 Docker 容器.Docker 客户端和守护进程可以运行在同一个系统上,当然你也可以使用 Docker

Docker学习总结之Run命令介绍

在使用Docker时,执行最多的命令某过于run了.这个命令可以说是所有docker操作的入口.在Docker官方Reference中单独列出了一个章节来介绍Run的各种参数使用,也足以看出Docker run的重要性.有感于此,我感觉有必要好好学习一下Run命令,因此特意看了一下Run命令介绍,结合日常中的使用心得,分享一下.以下文档大部分翻译于Docker 官方Reference,肯定会存在不少错误之处,希望能抛砖引玉,大家共同讨论. Docker在执行时会将相关进程封装到相互隔离的容器(c

docker nexus oss

sonatype/docker-nexus Docker images for Sonatype Nexus with the Oracle JDK. To build: # docker build --rm --tag sonatype/nexus oss/ # docker build --rm --tag sonatype/nexus-pro pro/ To run (if port 8081 is open on your host): # docker run -d -p 8081:

Docker学习总结(12)——非常详细的 Docker 学习笔记

一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docker Hub Docker 使用客户端-服务器 (C/S) 架构模式.Docker 客户端会与 Docker 守护进程进行通信.Docker 守护进程会处理复杂繁重的任务,例如建立.运行.发布你的 Docker 容器.Docker 客户端和守护进程可以运行在同一个系统上,当然你也可以使用 Docker

[转载] docker笔记

原文: http://opskumu.github.io/docker.html docker的文章很多, 也不乏精品, 本文的好处在于作者不仅仅整理了自己对docker的理解, 还引用了很多经典的docker文献, 对于由浅入深的理解docker非常有帮助 一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docker Hub Docker 使用客户端-服

【容器技术】非常详细的 Docker 学习笔记

一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化平台 Docker Hub: 用于分享.管理 Docker 容器的 Docker SaaS 平台 -- Docker Hub Docker 使用客户端-服务器 (C/S) 架构模式.Docker 客户端会与 Docker 守护进程进行通信.Docker 守护进程会处理复杂繁重的任务,例如建立.运行.发布你的 Docker 容器.Docker 客户端和守护进程可以运行在同一个系统上,当然你也可以使用 Docker

Docker run 命令的使用方法

[编者的话]在Docker中,run应该是用户使用最多的命令了,很多读者反馈不是很明白run命令的用法,而且相关的书籍.中文资料中对run命令的描述也不是非常完整,所以DockerOne组织翻译了Docker官方的文档,以飨读者.注意,本文基于最新的Docker 1.4文档翻译. Docker会在隔离的容器中运行进程.当运行 docker run命令时,Docker会启动一个进程,并为这个进程分配其独占的文件系统.网络资源和以此进程为根进程的进程组.在容器启动时,镜像可能已经定义了要运行的二进制