Docker SDK Python运行容器(如果它未运行)

2024-10-01 22:30:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是仅在容器尚未运行时运行它

我尝试的是使用client.containers.get(containername)并检查它是否为None,但这不起作用,因为它返回错误消息而不是bool

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

我得到:

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

我有没有办法检查redis容器是否已经运行,如果没有,就只运行容器


Tags: redisclientnone消息目标getifis

热门问题