本文整理汇总了Python中docker.APIClient.exec_create方法的典型用法代码示例。如果您正苦于以下问题:Python APIClient.exec_create方法的具体用法?Python APIClient.exec_create怎么用?Python APIClient.exec_create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.APIClient
的用法示例。
在下文中一共展示了APIClient.exec_create方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: docker_abs_net_io
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import exec_create [as 别名]
def docker_abs_net_io(container_id):
"""
Network traffic of all network interfaces within the controller.
:param container_id: The full ID of the docker container.
:type container_id: ``str``
:return: Returns the absolute network I/O till container startup, in bytes. The return dict also contains the
system time.
:rtype: ``dict``
"""
c = APIClient()
command = c.exec_create(container_id, 'ifconfig')
ifconfig = c.exec_start(command['Id'])
sys_time = int(time.time() * 1000000000)
in_bytes = 0
m = re.findall('RX bytes:(\d+)', str(ifconfig))
if m:
for number in m:
in_bytes += int(number)
else:
in_bytes = None
out_bytes = 0
m = re.findall('TX bytes:(\d+)', str(ifconfig))
if m:
for number in m:
out_bytes += int(number)
else:
out_bytes = None
return {'NET_in': in_bytes, 'NET_out': out_bytes, 'NET_systime': sys_time}
示例2: ImageBuildException
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import exec_create [as 别名]
#.........这里部分代码省略.........
try:
ret_val = str(self.client.inspect_container(container_id).get('State').get('Status'))
if ret_val.startswith("running"):
status = ProviderBase.STATUS_RUNNING
else:
status = ProviderBase.STATUS_STOPPED
except NotFound:
pass
return status
def start_containers(self, container_ids):
"""Starts each container in given list of container IDs."""
for container_id in container_ids:
self.start_container(container_id)
def start_container(self, container_id):
""" Start the container with given ID."""
logging.info(DockerProxy.LOG_TAG + " Starting container " + container_id)
try:
self.client.start(container=container_id)
except (NotFound, NullResource) as e:
print (DockerProxy.LOG_TAG + "Something went wrong while starting container.", e)
return False
return True
def execute_command(self, container_id, command):
"""Executes given command as a shell command in the given container. Returns None is anything goes wrong."""
run_command = "/bin/bash -c \"" + command + "\""
# print("CONTAINER: {0} COMMAND: {1}".format(container_id, run_command))
if self.start_container(container_id) is False:
print (DockerProxy.LOG_TAG + "Could not start container.")
return None
try:
exec_instance = self.client.exec_create(container_id, run_command)
response = self.client.exec_start(exec_instance)
return [self.client.exec_inspect(exec_instance), response]
except (NotFound, APIError) as e:
print (DockerProxy.LOG_TAG + " Could not execute command.", e)
return None
def build_image(self, dockerfile):
""" Build image from given Dockerfile object and return ID of the image created. """
import uuid
logging.info("Building image...")
random_string = str(uuid.uuid4())
image_tag = Constants.DOCKER_IMAGE_PREFIX + "{0}".format(random_string[:])
last_line = ""
try:
for line in self.client.build(fileobj=dockerfile, rm=True, tag=image_tag):
print(DockerProxy._decorate(line))
if "errorDetail" in line:
raise DockerProxy.ImageBuildException()
last_line = line
# Return image ID. It's a hack around the fact that docker-py's build image command doesn't return an image
# id.
image_id = get_docker_image_id_from_string(str(last_line))
logging.info("Image ID: {0}".format(image_id))
return str(DockerImage(image_id, image_tag))
except (DockerProxy.ImageBuildException, IndexError) as e:
raise DockerProxy.ImageBuildException(e)
@staticmethod
def _decorate(some_line):
return some_line[11:-4].rstrip()