python docker如何打印脚本的返回码以了解测试脚本是否通过或失败 [英] python docker how to print return code of the script to know if the test script passed or failed

查看:36
本文介绍了python docker如何打印脚本的返回码以了解测试脚本是否通过或失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有2个问题:

  1. 当我尝试使用python docker apis运行时它不打印日志,但是使用docker run来打印日志吗?
  2. 如何获取我在容器中运行的脚本的返回码?

这是我尝试过的:

import docker
dockerClient = docker.from_env()
print dockerClient.containers.run(image='centos7.5', command='./pmml2luastatic')

推荐答案

1)根据

1) According to documentation function run does not print, but returns stdout and stderr

根据stdout和stderr参数的值,容器记录STDOUT和/或STDERR,或同时记录两者.
仅当使用json文件或日记日志驱动程序时,才可以读取STDOUT和STDERR.因此,如果您不使用这些驱动程序,则将返回None对象.
有关详细信息,请参阅Engine API 文档.

对于显示日志,我建议考虑在子进程中运行容器,而不是通过API运行.

For show log, I suggest considering run container in subprocess instead of running via API.

import subprocess

subprocess.run("docker run --rm -i centos7.5 ./pmml2luastatic", shell=True)

2)这样,您还可以通过CompletedProcess对象捕获返回代码

2) This way also will allow you to catch return code via CompletedProcess object

import subprocess

result = subprocess.run("docker run --rm -i centos7.5 ./pmml2luastatic", shell=True)

if result.returncode != 0:
   print("Error")

我将考虑使用API​​与远程计算机进行通信.无论如何,如果您将使用API​​来运行容器,请使用 stream 参数.

I will consider using API for communication with a remote machine. If you anyway will use API for running container then use stream param.

stream(布尔)–如果true和detach为false,则返回日志生成器而不是字符串.如果detach为true,则忽略它.默认值:False.

stream (bool) – If true and detach is false, return a log generator instead of a string. Ignored if detach is true. Default: False.

这篇关于python docker如何打印脚本的返回码以了解测试脚本是否通过或失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆