title: List containers in the same docker-compose
body: <p>I am working on an application that takes advantage of <code>docker-compose</code>. 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.</p>
  
  <p>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 <code>docker-compose</code> command.</p>
  
  <p>Previously, the code looked something like this:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I found that <code>dockerpy</code> does not know about <code>docker-compose</code>, so I created a function that filters the containers based on docker-compose project name label (<code>com.docker.compose.project</code>), but now I have a hardcoded project name instead of container names. 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?</p>
  
  <p>Thanks</p>