Remove a named volume with docker-compose?

PostgresqlDockerDocker ComposeVolumesDocker Named-Volume

Postgresql Problem Overview


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?

Postgresql Solutions


Solution 1 - Postgresql

docker-compose down -v

removes all volumes attached. See the docs

Solution 2 - Postgresql

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

Solution 3 - Postgresql

I had the same issue as you, excepting I wanted to discard the working state of my grafana container while leaving the other containers running, which are running detached (ie. sudo docker-compose up -d). Here's the procedure I've come up with:

sudo docker-compose ps
sudo docker-compose stop grafana
sudo docker-compose rm --force grafana
sudo docker volume rm metricsmonitoring_grafana_data
sudo docker-compose up --force-recreate -d grafana

I don't know (without playing further) how best to determine the name of the docker volume to remove.

This is on docker-compose version 1.18.0

Solution 4 - Postgresql

Jan, 2022 Update:

This removes all the containers, networks, volumes and images defined in the docker-compose.

docker-compose down -v --rmi all

"-v" is for all the volumes

"--rmi all" is for all the images

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoshView Question on Stackoverflow
Solution 1 - PostgresqlhermView Answer on Stackoverflow
Solution 2 - PostgresqljamsinclairView Answer on Stackoverflow
Solution 3 - PostgresqlCameron KerrView Answer on Stackoverflow
Solution 4 - PostgresqlKai - Kazuya ItoView Answer on Stackoverflow