gpt4 book ai didi

docker py : How to get exit code returned by process running inside container?

转载 作者:行者123 更新时间:2023-12-01 12:09:02 25 4
gpt4 key购买 nike

我的 python 脚本使用 docker-py启动一个 docker 容器,如下所示:

client = docker.from_env()
result = client.containers.run(
image="my-prog-image:latest",
command=["/etc/my-prog/configs.ini"],
auto_remove=True,
network_mode="host",
)

如文档所示 docker-py: containers , client.containers.run(...)方法返回容器。如何检索 my-prog 返回的退出代码在容器内运行?

最佳答案

result.wait() 应该等待容器运行完成,然后返回其退出代码。

但是,您可能会遇到一些麻烦,因为您指定了 auto_remove=True 但没有指定 detach=Truerun() 没有 detach=True 将运行容器完成,然后 auto_remove=True 选项将删除容器,此时状态代码不再存在。您可以明确地拆分这些步骤:

client = docker.from_env()
container = client.containers.run(
image="my-prog-image:latest",
command=["/etc/my-prog/configs.ini"],
detach=True,
)
result = container.wait()
container.remove()

(在Docker CLI术语中,您已经完成了 docker run --rm ... ,然后尝试使用 docker ps -a 找到容器的结果,但是容器已经消失了;我建议将其更改为 docker run -d ... 而不使用 --rm ,然后手动检查 07910475 和 0x10475 容器的输出。实际上,甚至还有一个 docker ps CLI command 但很少使用。)

关于 docker py : How to get exit code returned by process running inside container?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54000979/

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