How to remove configure volumes in docker images

2019-02-25 18:53发布

问题:

I use docker run -it -v /xx1 -v /xx2 [image] /bin/bash to create a container.

Then commit to image and push to docker hub.

use docker inspect [image]

The Volumes detail is

"Volumes": {
            "/xx1": {},
            "/xx2": {}
        },

Now I want to remove volume /xx1 in this image.

What should I do?

回答1:

I don't think this is possible with the Docker tools right now. You can't remove a volume from a running container, or if you were to use your image as the base in a new Dockerfile you can't remove the inherited volumes.

Possibly you could use Jérôme Petazzoni's nsenter tool to manually remove the mount inside the container and then commit. You can use that approach to attach a volume to a running container, but there's some fairly low-level hacking needed to do it.



回答2:

An easy way to remove volumes is to export and then import the container image:

  • docker run --name my-container -it [image] /bin/bash
  • docker export my-container > tarball
  • docker import tarball


回答3:

There is a workaround in that you can docker save image1 -o archive.tar, editing the metadata json file, and docker import -i archive.tar. That way the history and all the other metadata is preserved.

To help with save/unpack/edit/load I have created a little script, have a look at docker-copyedit. Specifically for your question you would execute

./docker-copyedit.py from [image] into [image2] remove volume /xx1