Jiacheng-z
Jiacheng-z

Reputation: 93

How to remove configure volumes in docker images

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?

Upvotes: 9

Views: 7803

Answers (5)

thorfi
thorfi

Reputation: 327

Answer here: https://stackoverflow.com/a/78645904/2545063

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

Upvotes: 0

Toine42
Toine42

Reputation: 158

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)

Upvotes: 0

Guido U. Draheim
Guido U. Draheim

Reputation: 3281

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

Upvotes: 3

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

Upvotes: 0

Elton Stoneman
Elton Stoneman

Reputation: 19194

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.

Upvotes: 4

Related Questions