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?
2
Answers
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 usethe volume will be deleted
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.
Further reading
This creates a docker volume, which is managed by docker. And therefore all volume-commands can be applied:
Further reading