简体   繁体   English

列出同一个 docker-compose 中的容器

[英]List containers in the same docker-compose

I am working on an application that takes advantage of docker-compose .我正在开发一个利用docker-compose的应用程序。 One of the containers is a watchdog component, that runs a small python script periodically to get the states of the sibling containers, and takes action if one of the containers stopped.其中一个容器是一个看门狗组件,它定期运行一个小的 python 脚本来获取兄弟容器的状态,并在其中一个容器停止时采取行动。

To this point I used a list of all container names that should be watched, but now I was wondering it I could just watch all containers created by the same docker-compose command.到目前为止,我使用了应该监视的所有容器名称的列表,但现在我想知道我是否可以监视由相同的docker-compose命令创建的所有容器。

Previously, the code looked something like this:以前,代码如下所示:

watched_containers = ["component-a", "component-b"]
def inspect_containers():
    containers = {
        (container["Names"][0].lstrip("/"), container)
        for container in docker.Client().containers(all=True)
    }

    for container_name in watched_containers:
        if containers.get(container_name, {}).get("State") != "running":
            alert_container_stopped(container_name)

I found that dockerpy does not know about docker-compose , so I created a function that filters the containers based on docker-compose project name label ( com.docker.compose.project ), but now I have a hardcoded project name instead of container names.我发现dockerpy不知道dockerpy docker-compose ,所以我创建了一个基于com.docker.compose.project -compose 项目名称标签( com.docker.compose.project )过滤容器的函数,但现在我有一个硬编码的项目名称而不是容器名称。 Is there a way to get the current project name, or are there other ways to obtain the list of containers in the same compose?有没有办法获取当前项目名称,或者是否有其他方法可以获取同一组合中的容器列表?

Thanks谢谢

This is just a quick idea off the top of my head.这只是我脑海中的一个快速想法。

As you're using the Docker client library in the watchdog container, I'm assuming that you have access to the host's Docker API from within the watchdog container.当您在看门狗容器中使用 Docker 客户端库时,我假设您可以从看门狗容器中访问主机的 Docker API。 As each container's hostname is the container ID by default, you can use this to have the watchdog container look up its own labels from the Docker API.由于默认情况下每个容器的主机名是容器 ID,您可以使用它来让看门狗容器从 Docker API 中查找自己的标签。

Below is an example using the Docker CLI;以下是使用 Docker CLI 的示例; the Python SDK should work exactly the same way: Python SDK 应该以完全相同的方式工作:

$ COMPOSE_PROJECT=$(docker inspect -f '{{ (index .Config.Labels "com.docker.compose.project") }}' $HOSTNAME)

Then use the project name to get the remaining containers:然后使用项目名称获取剩余的容器:

$ docker ps -aq -f label=com.docker.compose.project=$COMPOSE_PROJECT

The new (June 2021) docker compose V2 could simplify this query with:新的(2021 年 6 月) docker compose V2可以通过以下方式简化此查询:
docker compose ls

List running compose projects列出正在运行的 compose 项目

Since compose is part of Docker commands, you can call it from your python program.由于 compose 是 Docker 命令的一部分,因此您可以从 Python 程序中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM