Docker Community Forums

Share and learn in the Docker community.

How to reset / clean references to previous volumes in docker

TLDR; If you have a docker project with a volume-mapping pointing to the root of the project folder and you rename that folder + re-create the project with the same docker configuration in the original folder name, then that volume will point to the renamed (old) folder instead of the current one. Pruning seems to have no effect on this.

How can this rerefence/pointer be deleted?


Prior to my problem I had been experimenting with a github project that provided docker configuration to run things. In the docker-compose file there was a volume directive stating:

  volumes:
   - .:/var/www

After playing around with it, I decided to do a fresh clone and start from scratch, but because I wanted to be able to see the things I had tried out so far I renamed the folder of the project on my local disk to [folder name]-bak and unzip the original zip-file, which created a folder with the same name, but without my changes.

After building the project again, it then appeared that my changes on the local file-system where no longer reflected in the docker container and worse - I could still see all my old changes from the previous docker image. I suspected some kind of cache, so pruned everything there is to prune (system, volume, etc.) but still I kept seeing my old changes. Until I finally realized that maybe the volume reference to the local filesystem had changed when I renamed the parent folder to -bak . And this appeared to be the case. Any modification I did in the -bak folder was reflected in the docker container of the new folder. (btw, because it provided a web-app, I could test these changes in a browser)

How can I ‘reset’ this? I want docker to forget this reference and create a reference to the local filesystem as if I did it for the first time.

Since Docker 1.13.x you can use Docker container prune:

docker container prune
This will remove all stopped containers and should work on all platforms the same way.

There is also a Docker system prune:

docker system prune
which will clean up all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes, in one command.

For older Docker versions, you can string Docker commands together with other Unix commands to get what you need. Here is an example on how to clean up old containers that are weeks old:

$ docker ps --filter “status=exited” | grep ‘weeks ago’ | awk ‘{print $1}’ | xargs --no-run-if-empty docker rm