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

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

问题描述

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

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

如 docker 用户指南所述,数据卷旨在持久容器文件系统之外的数据.这也简化了多个容器之间的数据共享.

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.

虽然 Docker 永远不会删除卷中的数据(除非您使用 docker rm -v 删除关联的容器),但未被任何 Docker 容器引用的卷称为 悬垂卷em>.那些悬垂的卷难以摆脱且难以访问.

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 access.

为了防止那些悬空的卷,诀窍是使用您想要持久化的数据卷创建一个额外的 docker 容器,以便始终至少有该 docker 容器引用该卷.通过这种方式,您可以删除运行 wordpress 应用程序的 docker 容器,而不会失去访问该数据量内容的便利性.

In order to prevent those dangling volumes, the trick is to create an additional docker container using the data volume you want to persist 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.

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

一定有一些简单的方法可以备份我的容器和卷数据,但我在任何地方都找不到.

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

备份 docker 镜像

要备份 docker 镜像,请使用 docker save 命令,该命令将生成一个tar 存档,以后可以使用 docker load 命令.

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.

你可以通过不同的方式备份一个docker容器

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.

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

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

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

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.

在您的特定情况下,数据卷用于存储 MySQL 服务器的数据.因此,如果要为此卷导出 tar 存档,则需要先停止 MySQL 服务器.为此,您必须停止 wordpress 容器.

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.

另一种方法是远程连接到 MySQL 服务器以生成带有 mysqldump 命令.但是,为了使其工作,您的 MySQL 服务器必须配置为接受远程连接,并且还具有允许远程连接的用户.您使用的 wordpress docker 图像可能不是这种情况.

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.

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

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

docker run 命令对 -v 选项.现在可以向它传递卷名.以这种方式创建的卷被命名并且以后易于引用,从而缓解了悬垂卷的问题.

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.

Docker 引入了docker volume prune命令可以轻松删除所有悬空卷.

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

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

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