如何使用数据卷备份Docker容器? [英] How can I backup a Docker-container with its data-volumes?

查看:175
本文介绍了如何使用数据卷备份Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用这个Docker-image tutum / wordpress 来演示Wordpress网站。最近我发现图像使用了MySQL数据的卷。



所以问题是这样的:如果我要备份和恢复容器,我可以尝试提交一个图像,然后删除容器,并从提交的图像创建一个新的容器。但是如果我这样做的话,这个卷被删除,我的所有数据都没有了。



必须有一些简单的方法来备份我的容器加上它的卷数据,但是我可以'

解决方案


如果我想恢复容器,我可以尝试提交一个图像,然后删除容器,并从提交的图像创建一个新的容器。但是如果我这样做,则会删除卷,并且我的所有数据都已经消失


正如docker用户指南所解释的那样,数据卷旨在将数据保留在容器文件系统之外。尽管Docker将不会删除卷中的数据(除非您删除与 docker rm相关联的容器),而且可以轻松地在多个容器之间共享数据。



-v
),任何docker容器未引用的卷称为悬挂卷



这意味着一旦最后一个使用卷的容器被删除,那么这些悬挂卷很难摆脱困难。数据量变得悬而未决,并且其内容难以接受。



为了防止这些悬空的卷,诀窍是使用数据卷创建一个附加的docker容器想留下来所以至少会有引用该卷的docker容器。这样,您可以删除运行wordpress应用程序的Docker容器,而不会轻易访问该数据卷内容。



这样的容器称为数据卷容器


必须有一些简单的方法来备份我的容器加上卷数据,但我找不到任何地方。




备用码头图像



要备份泊坞站图像,请使用停泊点保存命令,该命令将产生tar档案,以后可以使用码头加载命令。



备用码头容器



您可以通过不同的方式备份docker容器





请注意,这些命令只会备份docker容器分层文件系统。 这不包括数据卷



备份Docker数据卷



要备份数据卷,您可以使用要备份的卷运行新容器并执行tar命令以生成卷内容的存档,如 docker用户指南



在你的具体情况下,数据量用于存储MySQL服务器的数据。因此,如果要导出此卷的tar存档,则需要首先停止MySQL服务器。要执行此操作,您将不得不停止wordpress容器。



备份MySQL数据



另一种方法是远程连接到MySQL服务器以使用 mysqldump 命令。但是为了使其起作用,您的MySQL服务器必须配置为接受远程连接,并且还允许允许远程连接的用户。






编辑


这可能不是您正在使用的wordpress Docker图片

Docker最近推出了 Docker卷插件 允许将卷的处理委托给由供应商实现的插件。



docker run 命令具有 -v 选项。现在可以传递一个卷名称。以这种方式创建的卷是命名为,稍后可以稍后引用,轻松解决悬挂卷的问题。



编辑2



Docker推出了 docker volume prune 命令可轻松删除所有悬挂卷。


I've been using this Docker-image tutum/wordpress to demonstrate a Wordpress website. Recently I found out that the image uses volumes for the MySQL-data.

So the problem is this: If I want to backup and restore the container I can try to commit an image, and then later delete the container, and create a new container from the committed image. But if I do that the volume gets deleted and all my data is gone.

There must be some simple way to backup my container plus its volume-data but I can't find it anywhere.

解决方案

if I want to revert the container I can try to commit an image, and then later delete the container, and create a new container from the committed image. But if I do that the volume gets deleted and all my data is gone

As the docker user guide explains, data volumes are meant to persist data outside of a container filesystem. This also ease the sharing of data between multiple containers.

While Docker will never delete data in volumes (unless you delete the associated container with docker rm -v), volumes that are not referenced by any docker container are called dangling volumes. Those dangling volumes are difficult to get rid of and difficult to access.

This means that as soon as the last container using a volume is deleted, the data volume becomes dangling and its content difficult to acess.

In order to prevent those dangling volumes, the trick is to create an additional docker container using the data volume you want to remain ; so that there will always be at least that docker container referencing the volume. This way you can delete the docker container running the wordpress app without losing the ease of access to that data volume content.

Such containers are called data volume containers.

There must be some simple way to back up my container plus volume data but I can't find it anywhere.

backup docker images

To backup docker images, use the docker save command that will produce a tar archive that can be used later on to create a new docker image with the docker load command.

backup docker containers

You can backup a docker container by different means

  • by committing a new docker image based on the docker container current state using the docker commit command
  • by exporting the docker container file system as a tar archive using the docker export command. You can later on create a new docker image from that tar archive with the docker import command.

Be aware that those commands will only backup the docker container layered file system. This excludes the data volumes.

backup docker data volumes

To backup a data volume you can run a new container using the volume you want to backup and executing the tar command to produce an archive of the volume content as described in the docker user guide.

In your particular case, the data volume is used to store the data for a MySQL server. So if you want to export a tar archive for this volume, you will need to stop the MySQL server first. To do so you will have to stop the wordpress container.

backup the MySQL data

An other way is to remotely connect to the MySQL server to produce a database dump with the mysqldump command. However in order for this to work, your MySQL server must be configured to accept remote connections and also have a user who is allowed to connect remotely. This might not be the case with the wordpress docker image you are using.


Edit

Docker recently introduced Docker volume plugins which allow to delegate the handling of volumes to plugins implemented by vendors.

The docker run command has a new behavior for the -v option. It is now possible to pass it a volume name. Volumes created in that way are named and easy to reference later on, easing the issues with dangling volumes.

Edit 2

Docker introduced the docker volume prune command to delete all dangling volumes easily.

这篇关于如何使用数据卷备份Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆