Bosh
Bosh

Reputation: 8738

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?

Upvotes: 164

Views: 128551

Answers (4)

jamsinclair
jamsinclair

Reputation: 1534

There's no way to target the removal of a specific named volume with the docker compose command. Instead this can be achieved using the docker volume command. 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:

# The "-sf" flags will stop the container and skip confirmation of the removal
docker compose rm -sf NAME_OF_SERVICE

# Next find your volume name in the following list
docker volume ls

# Finally remove the volume
docker volume rm VOLUME_NAME

Edit: The answer is updated to reference the docker compose command as the docker-compose cli is no longer maintained since July 2023.

Upvotes: 83

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

Upvotes: 49

herm
herm

Reputation: 16315

docker-compose down -v

removes all volumes attached. See the docs

Upvotes: 228

Cameron Kerr
Cameron Kerr

Reputation: 1875

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

Upvotes: 9

Related Questions