如何在docker镜像中“取消声明”卷?

时间:2021-10-27 21:37:47

I am writing a Dockerfile for setting up an image for testing a web application. I am basing it on the tutum/lamp image (https://github.com/tutumcloud/tutum-docker-lamp/blob/master/Dockerfile) because that seems to be a good base to start from.

我正在编写一个Dockerfile来设置用于测试Web应用程序的映像。我基于tutum / lamp图像(https://github.com/tutumcloud/tutum-docker-lamp/blob/master/Dockerfile),因为这似乎是一个很好的基础。

As part of my dockerfile, I want to create a mysql database and set up some stuff in it. However, the tutum/lamp image declares VOLUME ["/etc/mysql", "/var/lib/mysql" ], so if I understand correctly, any changes that I make to the MySQL database in the Dockerfile will not be persisted.

作为我的dockerfile的一部分,我想创建一个mysql数据库并在其中设置一些东西。但是,tutum / lamp图像声明了VOLUME [“/ etc / mysql”,“/ var / lib / mysql”],所以如果我理解正确,我在Dockerfile中对MySQL数据库所做的任何更改都不会被保留。

  • Do I understand that correctly?
  • 我理解正确吗?

If yes,

  • Is there a way to "undeclare" those volumes so that those directories will be part of the union file system like everything else?
  • 有没有办法“取消声明”这些卷,以便这些目录将像其他一切一样成为联合文件系统的一部分?

Thanks!

3 个解决方案

#1


4  

You can't really undeclare a volume, but you can build your own version of the original image by modifying it's dockerfile.

您无法真正取消声明卷,但您可以通过修改它的dockerfile来构建自己的原始映像版本。

#2


3  

Not possible to change an existing container, so you have two options:

无法更改现有容器,因此您有两个选择:

  1. Take the Tutum container and build your own variant
  2. 拿Tutum容器构建自己的变体

  3. Manage persistence of the tutum container using a data container.
  4. 使用数据容器管理tutum容器的持久性。

Data containers

Create a container that creates a data volume reference:

创建一个用于创建数据卷引用的容器:

docker run -it --name dbvol -v /var/lib/mysql ubuntu env

This can then be used when running the mysql database to persist the data:

然后可以在运行mysql数据库时使用它来保存数据:

docker run -d --volumes-from dbvol -p 3306:3306 tutum/mysql:5.6

The data persists as long as the "dbvol" container exists. It can be deleted at any stage:

只要“dbvol”容器存在,数据就会持续存在。它可以在任何阶段删除:

docker rm dbvol

Reference:

#3


0  

There has been no change to this problem space in years, so I have created docker-copyedit as a workaround to "undeclare" a volume by editing the metadata of a downloaded image.

多年来这个问题空间一直没有变化,因此我创建了docker-copyedit作为解决方法,通过编辑下载图像的元数据来“取消声明”卷。

#1


4  

You can't really undeclare a volume, but you can build your own version of the original image by modifying it's dockerfile.

您无法真正取消声明卷,但您可以通过修改它的dockerfile来构建自己的原始映像版本。

#2


3  

Not possible to change an existing container, so you have two options:

无法更改现有容器,因此您有两个选择:

  1. Take the Tutum container and build your own variant
  2. 拿Tutum容器构建自己的变体

  3. Manage persistence of the tutum container using a data container.
  4. 使用数据容器管理tutum容器的持久性。

Data containers

Create a container that creates a data volume reference:

创建一个用于创建数据卷引用的容器:

docker run -it --name dbvol -v /var/lib/mysql ubuntu env

This can then be used when running the mysql database to persist the data:

然后可以在运行mysql数据库时使用它来保存数据:

docker run -d --volumes-from dbvol -p 3306:3306 tutum/mysql:5.6

The data persists as long as the "dbvol" container exists. It can be deleted at any stage:

只要“dbvol”容器存在,数据就会持续存在。它可以在任何阶段删除:

docker rm dbvol

Reference:

#3


0  

There has been no change to this problem space in years, so I have created docker-copyedit as a workaround to "undeclare" a volume by editing the metadata of a downloaded image.

多年来这个问题空间一直没有变化,因此我创建了docker-copyedit作为解决方法,通过编辑下载图像的元数据来“取消声明”卷。