如何查看哪个容器正在使用或正在使用哪个Docker卷 [英] How to see which docker volume is or was being used by which container

查看:605
本文介绍了如何查看哪个容器正在使用或正在使用哪个Docker卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以列出所有具有 docker ps(-a)的容器,我可以列出所有具有 docker volume ls ,当我检查时,可以看到名称驱动程序 mountpoint ,但不是使用它的容器。

I can list all containers with docker ps (-a), I can list all volumes with docker volumes ls, when I inspect a volume, I can see the name, driver and mountpoint, but not the container(s) it is being used by.

当我使用 docker inspect< container> ,例如,我可以看到Mount数据:

When I use docker inspect <container>, I can see the Mount data like this for example:

"Mounts": [
    {
        "Name": "centos_db_symfony",
        "Source": "/var/lib/docker/volumes/centos_db_symfony/_data",
        "Destination": "/var/lib/mysql",
        "Driver": "local",
        "Mode": "rw",
        "RW": true,
        "Propagation": "rprivate"
    },

所以从理论上讲,我可以编写一个脚本来循环遍历所有容器,以按名称匹配特定的。但是我通过 docker-compose 运行了一些容器,并且没有将名称绑定(例如现在可以在v2中使用)到某些卷,因此它们在sha256中显示为sha256 docker volume ls 列表,像这样:

so in theory, I could write a script that loops through all containers to match a specific volume by name. But I ran some containers through docker-compose, and didn't bind a name (like now is possible in v2) to some volumes, so they show up like a sha256 in the docker volume ls list, like this:

DRIVER              VOLUME NAME
local               34009871ded5936bae81197247746b708c3ec9e9b9832b702c09736a90...etc
local               centos_data
local               centos_db_symfony

在这种情况下,在我在 docker-compose 34009871ded5 (示例) > centos_db_symfony 之后。

In this case 34009871ded5 (example) was created before I named the volume in docker-compose and centos_db_symfony after.

docker-compose.yml 时卷信息会像在这种情况下那样进行命名,并更新 docker inspect< container> 中的信息,是永远丢失的历史记录还是我能找出哪个容器使用了多少卷?如果是这样,是否还可以还原这样的旧卷?

When docker-compose.yml volume information is updated like in this case making the volume named and the information in docker inspect <container> is updated, is the history forever lost or can I find out which container a volume was used by? If so, is it also possible to restore an old volume like this?

docker-compose version 1.6.0, build d99cad6
Docker version 1.10.2, build c3959b1


推荐答案

Docker v1.11 ,您可以按 volume s过滤 ps !不幸的是,这在以前的版本中不可用。

As of Docker v1.11 you can filter ps by volumes! Unfortunately this is not available in previous versions.

使用方法如下:

docker ps -f "volume=/var/lib/mysql" 

docker ps -f "volume=centos_db_symfony"

这篇关于如何查看哪个容器正在使用或正在使用哪个Docker卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆