Bas*_*har 4 python docker dockerpy
docker-py 有没有办法在分离容器之前等待运行状况检查成功?我正在尝试执行以下操作,但问题是 .run() 在运行状况检查成功之前返回。如果我尝试在 run() 之后卷曲 elasticsearch 端点,则调用失败。
cls.es = client.containers.run("elasticsearch:7.5.0", auto_remove=True,
detach=True, publish_all_ports=True,
healthcheck='curl localhost:9200/_cat/health',
ports={'9200/tcp': 9200},
environment={'discovery.type': 'single-node'})
Run Code Online (Sandbox Code Playgroud)
这似乎完全没有记录,但通过研究源代码,我想出了一种在继续之前等待运行状况检查通过的方法。
由于这实际上与您正在检查哪个映像没有任何关系,因此我使用了一个我熟悉的映像,它有自己的运行状况检查工具。此方法也适用于其他图像和健康检查方法。
from time import sleep
from docker import APIClient
from docker.models.containers import Container
import docker
def get_health(container: Container):
api_client = APIClient()
inspect_results = api_client.inspect_container(container.name)
return inspect_results['State']['Health']['Status']
client = docker.from_env()
container = client.containers.run(
'postgres:12',
environment={'POSTGRES_HOST_AUTH_METHOD': 'trust'},
detach=True,
remove=True,
healthcheck={'test': ['CMD', 'pg_isready', '-U', 'postgres'], 'interval': 100000000},
)
while get_health(container) != 'healthy':
print(get_health(container))
sleep(0.1)
print('Container is healthy')
Run Code Online (Sandbox Code Playgroud)
免责声明:
py-docker此功能发生了什么。我在 Github 问题跟踪器中发现了这样的评论:
这个想法是为了避免属性激增,而这些属性又需要维护,并且可能根据 API 版本出现在属性字典中的不同位置。因此,我们将快速访问属性的数量限制为少数必需品。就这一点而言,我个人不同意健康属性是必不可少的这一观点。大多数容器根本不使用健康检查。
| 归档时间: |
|
| 查看次数: |
1559 次 |
| 最近记录: |