Remove a named volume with docker-compose?

Deadly 提交于 2020-11-30 06:12:43

问题


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?


回答1:


docker-compose down -v

removes all volumes attached. See the docs




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/45511956/remove-a-named-volume-with-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!