简体   繁体   English

Docker SDK Python 运行容器,如果它不是aeardy运行

[英]Docker SDK Python run container if it isn't aleardy running

My goal is to only run the container if it isn't already running.我的目标是仅在容器尚未运行时才运行它。

What I tried is using client.containers.get(containername) and checking if it is None but that won't work because it returns an error message and not a bool.我尝试使用 client.containers.get(containername) 并检查它是否为 None 但这不起作用,因为它返回错误消息而不是布尔值。

if client.containers.get('containername') is not None:
   client.containeres.run("redis:latest",name="containerename", detach=True, ports={'6379/tcp': ('localhost', 6379)})

I get:我得到:

docker.error.notFound: 404 Client Error: Not Found("No such container: containername")

Is there any way I can manage to check if the redis container is already running and only run the container if it doesn't?有什么办法可以检查 redis 容器是否已经在运行,并且只有在没有运行的情况下才运行容器?

What about to use containers.list ?如何使用containers.list

if len(client.containers.list(filters = {'name' : 'containername'})) == 0:
  ...

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

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