How to “undeclare” volumes in docker image?

2019-01-25 10:33发布

问题:

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.

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.

  • 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!

回答1:

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



回答2:

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

  1. Take the Tutum container and build your own variant
  2. Manage persistence of the tutum container using a data container.

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:

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:

docker rm dbvol

Reference:

  • http://blog.tutum.co/2014/05/27/containerize-your-database-volume-with-tutum-mysql-images/
  • https://docs.docker.com/userguide/dockervolumes/


回答3:

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.



标签: mysql docker