Linux 备份正在运行的 Docker 容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15700943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:36:26  来源:igfitidea点击:

Backup a running Docker container?

linuxdockerlinux-containers

提问by Slava V

Is it possible to backup a running Docker container? Is the exportcommand suitable for doing that?

是否可以备份正在运行的 Docker 容器?该export命令适合这样做吗?

采纳答案by JuliandotNut

Posted by one friend in comments

一位朋友在评论中发表

Hi Slava, sorry that your question was closed. For the record, Slava is talking about docker.io, a runtime for linux containers. Yes, docker exportis a suitable approach. It will generate a tarball of your entire container filesystem state, and dump it on stdout. So

嗨,Slava,很抱歉您的问题已关闭。作为记录,Slava 正在谈论 docker.io,一个 Linux 容器的运行时。是的,docker export是一个合适的方法。它将生成整个容器文件系统状态的 tarball,并将其转储到 stdout。所以

docker export $CONTAINER_ID > $CONTAINER_ID-backup.tar

docker export $CONTAINER_ID > $CONTAINER_ID-backup.tar

will yield a usable tarball. You can re-import the tarball with

将产生一个可用的 tarball。您可以重新导入 tarball

docker import - slava/$CONTAINER_ID-backup < $CONTAINER_ID-backup.tar

docker import - slava/$CONTAINER_ID-backup < $CONTAINER_ID-backup.tar

Note the original metadata (eg id of the original image) will be lost. This should be fixed in future versions of docker. – Solomon Hykes Apr 2 '13 at 6:35

请注意原始元数据(例如原始图像的 id)将丢失。这应该在未来版本的 docker 中修复。– 所罗门·海克斯 2013 年 4 月 2 日 6:35

Adding here so one can find from summary that question was answered. Thanks Solomon!

在此处添加,以便您可以从摘要中找到该问题已得到解答。谢谢所罗门!

回答by Awan

you can also using save and load.. here's the sample

您还可以使用保存和加载.. 这是示例

  1. sudo docker images

    awan@google-dev:~/StarCenter/_docker$ sudo docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    google_star/_version_1.10           latest              1067d6689697b2        4 days ago          1.666 GB
    
  2. sudo docker save google_star/_version_1.10 > my_docker-backup.tgz

  3. restore it using (sudo docker load < my_docker-backup.tgz)

  4. check your images using sudo docker imagesin your new docker machine

  1. sudo docker images

    awan@google-dev:~/StarCenter/_docker$ sudo docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    google_star/_version_1.10           latest              1067d6689697b2        4 days ago          1.666 GB
    
  2. sudo docker save google_star/_version_1.10 > my_docker-backup.tgz

  3. 使用 ( sudo docker load < my_docker-backup.tgz)恢复它

  4. sudo docker images在您的新 docker 机器中使用检查您的图像

回答by semicircle21

exporthas some limitations: it won't export the data volume.

export有一些限制:它不会导出数据卷

Here's data volumemeans:

这里的数据量意味着:

  1. There's a VOLUMEdefined in the image's Dockerfile.
  2. The container is start with a parameter like this: -v /webapp
  1. VOLUME图像的 Dockerfile 中有一个定义。
  2. 容器以这样的参数开始: -v /webapp

More about data: https://docs.docker.com/userguide/dockervolumes/

有关数据的更多信息:https: //docs.docker.com/userguide/dockervolumes/

The way to handle this situation is start a new container with '--volumes-from' parameter to hook on that container, so you can visit the data volume.

处理这种情况的方法是使用' --volumes-from'参数启动一个新容器来挂钩该容器,以便您可以访问数据卷。

Examples:

例子:

  1. Visit the data: (in a bash)
  1. 访问数据:(在 bash 中)

docker run -it --volumes-from target_container ubuntu bash

docker run -it --volumes-from target_container ubuntu bash

  1. Backup to host: (a postgres container)
  1. 备份到主机:(一个 postgres 容器)

docker run -it --volumes-from some_postgres -v /host/path:/container/path?--rm ubuntu bash -c "tar -cvf /container/path/postgres-backup.tar /var/lib/postgresql/data"

docker run -it --volumes-from some_postgres -v /host/path:/container/path?--rm ubuntu bash -c "tar -cvf /container/path/postgres-backup.tar /var/lib/postgresql/data"

回答by Brian Ogden

Using the docker commitis my preferred way to back up a container (started or stopped). Creates an image that you can name:

使用docker commit是我备份容器(启动或停止)的首选方式。创建一个您可以命名的图像:

docker commit - p <container_id> <backup-name>