Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a named volume with docker-compose?

If I have a docker-compose file like:

version: "3" services:   postgres:     image: postgres:9.4     volumes:       - db-data:/var/lib/db volumes:   db-data: 

... then doing docker-compose up creates a named volume for db-data. Is there a way to remove this volume via docker-compose? If it were an anonymous volume, then docker-compose rm -v postgres would do the trick. But as it stands, I don't know how to remove the db-data volume without reverting to docker commands. It feels like this should be possible from within the docker-compose CLI. Am I missing something?

like image 676
Bosh Avatar asked Aug 04 '17 16:08

Bosh


People also ask

How do I remove a specific docker volume?

Remove one or more specific volumes - Docker 1.9 and laterUse the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command: List: docker volume ls.

Does docker-compose delete volumes?

Stops containers and removes containers, networks, volumes, and images created by up .

How do you remove the volume of a container?

To remove one or more Docker volumes, run the docker volume ls command to find the ID of the volumes you want to remove. If you get an error similar to the one shown below, it means that an existing container uses the volume. To remove the volume, you will have to remove the container first.

Does docker run -- RM remove volumes?

This command removes the container and any volumes associated with it. Note that if a volume was specified with a name, it will not be removed.


2 Answers

docker-compose down -v 

removes all volumes attached. See the docs

like image 182
herm Avatar answered Sep 19 '22 18:09

herm


There's no way to target the removal of a specific named volume with the docker-compose cli. Instead this can be achieved using the docker cli. See the docs.

Use docker volume ls to find the name of specific volume.

Remove the volume using docker volume rm VOLUME_NAME. You will need to have stopped and removed containers using the volume.

An example approach:

# Stop and remove container's using the target volume docker-compose stop NAME_OF_CONTAINER  # We need the force flag, "-f", as the container is still bound to the volume docker-compose rm -f NAME_OF_CONTAINER  # Next find your volume name in the following list docker volume ls  # Finally remove the volume docker volume rm VOLUME_NAME 
like image 34
jamsinclair Avatar answered Sep 18 '22 18:09

jamsinclair