本文整理汇总了Python中docker.APIClient.containers方法的典型用法代码示例。如果您正苦于以下问题:Python APIClient.containers方法的具体用法?Python APIClient.containers怎么用?Python APIClient.containers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.APIClient
的用法示例。
在下文中一共展示了APIClient.containers方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_server_containers
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def get_server_containers(server: Server, client: docker.APIClient) -> tp.List[dict]:
containers = client.containers()
server_containers = []
for container in containers:
container = {
'command': filter_printable(container['Command']),
'containerId': container['Id'],
'image': container['Image'],
'labels': sorted([{
'containerId': container['Id'],
'name': l[0],
'value': l[1]} for l in container['Labels'].items()],
key=lambda label: label['name']),
'name': container['Names'][0],
'network': container['HostConfig']['NetworkMode'],
'ports': sorted([{
'destination': str(p['PrivatePort']),
'hostIp': p['IP'] if 'IP' in p else None,
'protocol': p['Type'],
'source': str(p['PublicPort']) if 'PublicPort' in p else None} for p in container['Ports']],
key=lambda port: (str(port['destination']), str(port['source']))),
'privileged': client.inspect_container(container['Id'])['HostConfig']['Privileged'],
'serverId': server.id,
'volumes': sorted([{
'containerId': container['Id'],
'destination': filter_printable(v['Destination']),
'source': filter_printable(v['Source'])} for v in container['Mounts']],
key=lambda volume: volume['destination'])
}
server_containers.append(container)
return server_containers
示例2: modify_random_containers
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def modify_random_containers(client: docker.APIClient, amount: int, action: str = 'stop') -> tp.List[dict]:
server_containers = client.containers()
stopped_containers = []
for _ in range(amount):
container = random.choice(server_containers)
if action == 'delete':
client.remove_container(container, force=True)
elif action == 'stop':
client.stop(container)
stopped_containers.append(container)
server_containers.remove(container)
return stopped_containers
示例3: start_containers
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def start_containers(worker_api, name_prefix, timeout=5):
"""Start containers with given prefix
The chaincode container usually has name with `name_prefix-` as prefix
:param worker_api: Docker daemon url
:param name_prefix: image name prefix
:param timeout: Time to wait for the response
:return: None
"""
logger.debug("Get containers, worker_api={}, prefix={}".format(
worker_api, name_prefix))
client = Client(base_url=worker_api, version="auto", timeout=timeout)
containers = client.containers(all=True)
id_cc = [e['Id'] for e in containers if
e['Names'][0].split("/")[-1].startswith(name_prefix)]
logger.info(id_cc)
for _ in id_cc:
client.start(_)
示例4: _clean_project_containers
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def _clean_project_containers(worker_api, name_prefix, timeout=5):
"""
Clean cluster node containers and chaincode containers
All containers with the name prefix will be removed.
:param worker_api: Docker daemon url
:param name_prefix: image name prefix
:param timeout: Time to wait for the response
:return: None
"""
logger.debug("Clean project containers, worker_api={}, prefix={}".format(
worker_api, name_prefix))
client = Client(base_url=worker_api, version="auto", timeout=timeout)
containers = client.containers(all=True)
id_removes = [e['Id'] for e in containers if
e['Names'][0].split("/")[-1].startswith(name_prefix)]
for _ in id_removes:
client.remove_container(_, force=True)
logger.debug("Remove container {}".format(_))
示例5: _clean_exited_containers
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def _clean_exited_containers(worker_api):
""" Clean those containers with exited status
This is dangerous, as it may delete temporary containers.
Only trigger this when no one else uses the system.
:param worker_api: Docker daemon url
:return: None
"""
logger.debug("Clean exited containers")
client = Client(base_url=worker_api, version="auto")
containers = client.containers(quiet=True, all=True,
filters={"status": "exited"})
id_removes = [e['Id'] for e in containers]
for _ in id_removes:
logger.debug("exited container to remove, id={}", _)
try:
client.remove_container(_)
except Exception as e:
logger.error("Exception in clean_exited_containers {}".format(e))
示例6: reset_container_host
# 需要导入模块: from docker import APIClient [as 别名]
# 或者: from docker.APIClient import containers [as 别名]
def reset_container_host(host_type, worker_api, timeout=15):
""" Try to detect the daemon type
Only wait for timeout seconds.
:param host_type: Type of host: single or swarm
:param worker_api: Docker daemon url
:param timeout: Time to wait for the response
:return: host type info
"""
try:
client = Client(base_url=worker_api, version="auto", timeout=timeout)
containers = client.containers(quiet=True, all=True)
logger.debug(containers)
for c in containers:
client.remove_container(c['Id'], force=True)
logger.debug("cleaning all containers")
except Exception as e:
logger.error("Exception happens when reset host!")
logger.error(e)
return False
try:
images = client.images(all=True)
logger.debug(images)
for i in images:
if i["RepoTags"][0] == "<none>:<none>":
logger.debug(i)
try:
client.remove_image(i['Id'])
except Exception as e:
logger.error(e)
continue
logger.debug("cleaning <none> images")
except Exception as e:
logger.error("Exception happens when reset host!")
logger.error(e)
return False
return setup_container_host(host_type=host_type, worker_api=worker_api)