How to remove configure volumes in docker images
Asked Answered
A

5

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?

Argile answered 20/9, 2016 at 8:18 Comment(1)
I had the same question here, with lots of trial/error and explanations: #47043966 I guess it can't be done?Ales
D
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.

Diaphane answered 20/9, 2016 at 10:5 Comment(0)
E
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
Evonevonne answered 22/4, 2018 at 21:55 Comment(0)
W
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
Waldenburg answered 23/1, 2018 at 23:9 Comment(1)
too bad export/import doesn't just magically work 99% of the time. It's equivalent to writing a Dockerfile from scratch.Magneton
A
0

I stumbled upon this question in 2023, and since release 1.10.1 you can now use Buildah for this specific task.

For instance, to remove volume /test_volume from image test-image:latest and save the resulting image in updated-test-image:latest :

img=$(buildah from test-image:latest)
buildah config --volume /test_volume- $img
buildah commit $img updated-test-image:latest

(Pay extra attention to the - after /test_volume)

Absquatulate answered 20/12, 2023 at 16:34 Comment(0)
A
0

Answer here: https://mcmap.net/q/1317772/-quot-remove-quot-a-volume-in-a-dockerfile

So, turning up with an actual solution here, use COPY --from to copy the files from the postgres image to the Debian image it's based on (while leaving out the VOLUME directive (along with everything else too).

Dockerfile

FROM postgres:15-bookworm as postgres-docker

FROM debian:bookworm-slim as postgres-build

# We do this because the postgres-docker container has a VOLUME on /var/lib/postgresql/data
# This copies the filesystem without bringing the VOLUME with it.
COPY --from=postgres-docker / /

# DO THE REST OF YOUR STUFF, e.g.:
VOLUME /var/lib/postgresql

# https://hub.docker.com/_/postgres - see 15-bookworm for the stuff we are setting here
# Note: We need to set these because we are using COPY --from=postgres-docker - see above
ENTRYPOINT ["docker-entrypoint.sh"]
ENV LANG en_US.utf8
ENV PG_MAJOR 15
ENV PATH $PATH:/usr/lib/postgresql/${PG_MAJOR}/bin
ENV PGDATA /var/lib/postgresql/data
ENV SLEEP_ON_EXIT=0
STOPSIGNAL SIGINT
CMD ["postgres"]

EXPOSE 5432
Ancheta answered 21/6 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.