9

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?

Jiacheng-z
  • 93
  • 1
  • 4
  • I had the same question here, with lots of trial/error and explanations: https://stackoverflow.com/questions/47043466/docker-compose-reusing-volumes I guess it can't be done? – Chris Mitchell Nov 06 '17 at 19:55

3 Answers3

4

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.

Elton Stoneman
  • 17,906
  • 5
  • 47
  • 44
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
Guido U. Draheim
  • 3,038
  • 1
  • 20
  • 19
0

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
  • too bad export/import doesn't just magically work 99% of the time. It's equivalent to writing a Dockerfile from scratch. – jiggunjer Apr 06 '20 at 19:02