With $ docker volume prune, all unused volumes will be deleted, and even if you look at $ docker volume ls, you can't tell which volume you want to delete.
$ CONTAINER_NAME=hoge
$ docker ps
CONTAINER ID        IMAGE           COMMAND                  CREATED             STATUS              PORTS                    NAMES
1fd9075c0f5d       my_project_app   "docker-entrypoint.s…"   59 minutes ago      Up 59 minutes       0.0.0.0:8080->8080/tcp   app
The value of NAMES in the last column is the container name
$ docker inspect $(docker ps -f name=${CONTAINER_NAME} -q) | jq -r '.[].Mounts[] | select(.Type == "volume").Name' | xargs docker volume rm
--Get container ID from container name
--Get volume name with a combination of docker inspect and jq
--Delete with docker volume rm {volume name}
If you want to delete volume at the same time as deleting the container
docker-compose down -v $CONTAINER_NAME
Only when using docker-compose, but this is easier
Recommended Posts