Home > Blockchain >  delete docker volume does not remvoe the local file
delete docker volume does not remvoe the local file

Time:04-11

I run a docker container of nginx with the following: docker container run -d --name nginx3 -p 85:80 -v $(pwd):/usr/share/nginx/html nginx , then when I add files in the container volume (/usr/share/nginx/html) they are also added locally on the $pwd folder.

But when I remove the container, image, and volume with docker rm -vf $(docker ps -aq) && docker rmi -f $(docker images -aq) && docker volume prune the files on my local $pwd folder are still there.. why were they not deleted when I removed the volume?

CodePudding user response:

That's because docker volume prune delete the docker volumes and not the mounted volumes from the host.

If you define a volume with docker volume create nginx_volume and then use

docker container run -d --name nginx3 -p 85:80 -v nginx_volume:/usr/share/nginx/html nginx

the volume will be deleted

CodePudding user response:

You are not using a docker volume, you are using a bind mount. This was not that clear with the -v syntax, that's why docker recommends the new --mount syntax for new users:

This creates a bind mount from the host OS. Docker is not owner of it and therefore not deleting the folder if you unmount the binding.

 docker run -d \
  -it \
  --name devtest \
  --mount type=bind,source="$(pwd)"/target,target=/app \
  nginx:latest

Further reading

This creates a docker volume, which is managed by docker. And therefore all volume-commands can be applied:

 docker run -d \
  --name devtest \
  --mount source=myvol2,target=/app \
  nginx:latest

Further reading

  • Related