Docker:如何将容器及其数据克隆到一个新容器中 [英] Docker: how to clone container and its data into a new one

查看:2508
本文介绍了Docker:如何将容器及其数据克隆到一个新容器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将容器及其数据克隆到具有不同起始参数的新容器中?

Is there a way to clone a container and its data into a new one with different starting parameters?

目前,我只能在没有数据的情况下启动新的克隆容器(从自定义映像开始).

At the moment I'm only able to start a new cloned container (from custom image) WITHOUT the data.

我告诉你我必须做什么:我用一些启动参数启动了一个"docker-jenkins"容器,然后对其进行了配置,但是现在我注意到我忘记了一些重要的启动参数,因此我想重新启动同一容器并添加更多起始参数...

I tell you what I have to do: I started a "docker-jenkins" container with some starting parameters and then configured it, but now I noticed that I forgot some important starting parameters so I wanna restart the same container adding more starting parameters...

问题是(如果我理解得很清楚)我无法修改现有正在运行的容器的启动参数,因此我的想法是使用不同的参数启动一个克隆的容器(数据已包含),但我不知道该怎么做...

The problem is (if I understand well) that I cannot modify the starting parameters of existing running container, so my idea is to start a cloned one (data INCLUDED) with different parameters but I don't understand how to do it...

有人可以帮助我吗?

推荐答案

1.使用卷

如果唯一要保留的数据,则需要使用Volumes.

If your sole point is to persist your data you need to use Volumes.

数据卷是一个或多个内的特别指定的目录 绕过联合文件系统的容器.数据量提供 持久性或共享数据的几个有用功能:

A data volume is a specially-designated directory within one or more containers that bypasses the Union File System. Data volumes provide several useful features for persistent or shared data:

  • 创建容器时将初始化卷.如果容器的基础映像包含指定安装点的数据, 现有数据将被复制到新卷中 初始化. (请注意,这在安装主机时不适用 目录.)
  • 数据量可以在容器之间共享和重用.
  • 直接更改数据量.
  • 更新图像时将不包括对数据量的更改.
  • 即使容器本身已删除,数据卷仍然存在.
  • Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)
  • Data volumes can be shared and reused among containers.
  • Changes to a data volume are made directly.
  • Changes to a data volume will not be included when you update an image.
  • Data volumes persist even if the container itself is deleted.

来源:

https://docs.docker.com/engine/tutorials/dockervolumes/

从本质上讲,您是将计算机上的文件夹映射到容器中的一个文件夹. 当您杀死容器并生成一个新实例(具有修改后的参数)时,您的卷(具有现有数据)将被重新映射.

Essentially you map a folder from your machine to one into your container. When you kill the container and spawn a new instance (with modified parameters) your volume (with the existing data) is re-mapped.

示例:

docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

来源:

https://hub.docker.com/_/jenkins/


2.使用提交创建快照

另一种途径是利用docker commit命令.

A different route is to make use of the docker commit command.

将容器的文件更改或设置提交到 一个新的形象.这样,您可以通过运行 交互式外壳程序,或将工作数据集导出到另一台服务器. 通常,最好使用Dockerfiles在 文件化和可维护的方式.

It can be useful to commit a container’s file changes or settings into a new image. This allows you debug a container by running an interactive shell, or to export a working dataset to another server. Generally, it is better to use Dockerfiles to manage your images in a documented and maintainable way.

提交操作将不包含卷中包含的任何数据 安装在容器内. https://docs.docker.com/engine/reference/commandline/commit/

The commit operation will not include any data contained in volumes mounted inside the container. https://docs.docker.com/engine/reference/commandline/commit/

$ docker ps
ID                  IMAGE               COMMAND             CREATED             STATUS              PORTS
c3f279d17e0a        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours
197387f1b436        ubuntu:12.04        /bin/bash           7 days ago          Up 25 hours
$ docker commit c3f279d17e0a  svendowideit/testimage:version3
f5283438590d
$ docker images
REPOSITORY                        TAG                 ID                  CREATED             SIZE
svendowideit/testimage            version3            f5283438590d        16 seconds ago      335.7 MB

也可以使用更改后的配置进行提交:

It is also possible to commit with altered configuration:

docker commit --change='CMD ["apachectl", "-DFOREGROUND"]' -c "EXPOSE 80" c3f279d17e0a  svendowideit/testimage:version4

这篇关于Docker:如何将容器及其数据克隆到一个新容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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