jenkins - Docker:如何将容器及其数据克隆到一个新容器中

标签 jenkins docker

有没有办法将容器及其数据克隆到具有不同起始参数的新容器中?

目前我只能在没有数据的情况下启动一个新的克隆容器(来自自定义图像)。

我告诉你我必须做什么:我用一些启动参数启动了一个“docker-jenkins”容器,然后配置了它,但现在我注意到我忘记了一些重要的启动参数,所以我想重新启动同一个容器添加更多启动参数...

问题是(如果我理解得很好)我无法修改现有正在运行的容器的启动参数,所以我的想法是用不同的参数启动一个克隆的(包含数据)但我不明白该怎么做...

有人可以帮我吗?

最佳答案

1.使用卷

如果您的唯一目的是持久化您需要使用 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/

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

例子:

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

来源:

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


<强>2。使用提交创建快照

另一种方法是使用 docker commit 命令。

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.

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

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

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

关于jenkins - Docker:如何将容器及其数据克隆到一个新容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40318156/

相关文章:

git - Jenkins:不想触发 Git 标签(无工作区)

ruby-on-rails - 如何在 docker 上安装 Ruby?

docker - AZEROTHCORE-./bin/acore-docker-build脚本在本地返回 "unable to find image ' acbuild:latest'

docker - 如何在 Dockerfile 中为 ENTRYPOINT 指定工作目录

Docker Desktop Kubernetes 无法连接到服务器 : EOF

service - 开源项目的免费构建服务器服务?

java - Maven 不解析父 pom.xml 文件中的属性

java - 正则表达式排除不包含单词但包含某些单词的行

java - 服务器拒绝连接 : None of the protocols were accepted

docker - 在 Docker 客户端上配置代理后,Docker 容器内没有 http/https 连接