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
Backup a running Docker container?
提问by Slava V
Is it possible to backup a running Docker container? Is the export
command 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 export
is 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
您还可以使用保存和加载.. 这是示例
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
sudo docker save google_star/_version_1.10 > my_docker-backup.tgz
restore it using (
sudo docker load < my_docker-backup.tgz
)check your images using
sudo docker images
in your new docker machine
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
sudo docker save google_star/_version_1.10 > my_docker-backup.tgz
使用 (
sudo docker load < my_docker-backup.tgz
)恢复它sudo docker images
在您的新 docker 机器中使用检查您的图像
回答by semicircle21
export
has some limitations: it won't export the data volume.
export
有一些限制:它不会导出数据卷。
Here's data volumemeans:
这里的数据量意味着:
- There's a
VOLUME
defined in the image's Dockerfile. - The container is start with a parameter like this:
-v /webapp
VOLUME
图像的 Dockerfile 中有一个定义。- 容器以这样的参数开始:
-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:
例子:
- Visit the data: (in a bash)
- 访问数据:(在 bash 中)
docker run -it --volumes-from target_container ubuntu bash
docker run -it --volumes-from target_container ubuntu bash
- Backup to host: (a postgres container)
- 备份到主机:(一个 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 commit
is 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>