gpt4 book ai didi

Python Docker API 需要知道 attach(**kwargs) 到容器的语法

转载 作者:行者123 更新时间:2023-11-28 18:02:45 25 4
gpt4 key购买 nike

我正在尝试在如下容器中运行命令:

# container started
container = PythonDockerAPIs.runContainerWithoutLogs(alpineImage, 'tail -f /dev/null', detach=True)
#execute the command
cmd1 = container.exec_run('ls -ltr',stream=True, detach=True)

有没有一种方法可以使用 attach(**kwargs) 将日志附加到此容器并稍后打印日志?

https://docker-py.readthedocs.io/en/stable/containers.html

最佳答案

**kwargs 只是表示列出的参数是“关键字参数”,所以对于 attach function ,它使用 keyword=value 的形式以任何顺序接收这些参数(或“参数”):

  • stdout (bool) – 包括标准输出。
  • stderr (bool) – 包括 stderr。
  • stream (bool) – 作为迭代器逐步返回容器输出字符串,而不是单个字符串。
  • logs (bool) – 包括容器之前的输出。

要调用此函数,您需要:

logs = container.attach(stdout=True, stderr=True, stream=True, logs=True)

请注意,文档还说明了 logs function 是 attach 函数的包装器,因此您可以使用该函数而不是 attach

编辑:

完整的用法示例:

import docker
client = docker.from_env()
container = client.containers.run('hello-world', detach=True)

# method 1
print(container.logs())

# method 2
logs = container.attach(stdout=True, stderr=True, stream=False, logs=True)
print(logs)

关于Python Docker API 需要知道 attach(**kwargs) 到容器的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55054336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com