zoukankan      html  css  js  c++  java
  • docker-api

    __author__ = 'zxp'
    import docker
    import sys
    class DockerManager_Slave(object):
        def __init__(self):
            self.idict={}
            self.rinfo={}
            try:
                self.c = docker.Client(base_url='unix://var/run/docker.sock',version='1.0.1',timeout=15)
            except Exception,e:
                print "Connection docker server error:"+str(e)
                sys.exit()
        def Create_Container(self,image,command=None,mem_limit=0,ports=None,name=None,cpu_shares=None):
            """
            :param image (str): The image to run
            :param command (str or list): The command to be run in the container
            :param mem_limit (float or str): Memory limit (format: [number][optional unit], where unit = b, k, m, or g)
            :param ports (list of ints): A list of port numbers
            :param name (str): A name for the container
            :param cpu_shares (int or float): CPU shares (relative weight)
            Returns (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
            return container id
            """
            try:
                self.container = self.c.create_container(image=image, command=command,mem_limit=mem_limit,ports=ports,
                                                     name=name,cpu_shares=cpu_shares)
            except Exception,e:
                print "Create Container error:"+str(e)
                sys.exit()
            return self.container['Id']
        def Inspect_Container(self,containerid):
            """
            :param containerid:The container to inspect
            :return:Nearly the same output as docker inspect, just as a single dict
            """
            try:
                self.container_info=self.c.inspect_container(containerid)
            except Exception,e:
                print "Inspect Container"+containerid+"error:"+str(e)
                sys.exit()
        def Pull(self,repository,tag=None):
            """
            :param repository (str): The repository to pull
            :param tag (str): The tag to pull
            :return (generator or str): The output
            """
            try:
                self.pull_detail=self.c.pull(repository,tag)
            except Exception,e:
                print "Pull images"+repository+":"+tag+"error:"+str(e)
                sys.exit()
        def Start(self,containerid):
            try:
                self.c.start(containerid)
            except Exception,e:
                print "Start Container"+containerid+"error:"+str(e)
        def Stop(self,containerid,timeout=10):
            """
            :param container (str): The container to stop
            :param timeout (int): Timeout in seconds to wait for the container to stop before sending a SIGKILL
            :return:
            """
            try:
                self.c.stop(containerid,timeout=timeout)
            except Exception,e:
                print "Stop"+containerid+"error:"+str(e)
                sys.exit()
        def Remove_Container(self,containerid):
            """
            container (str): The container to remove
            v (bool): Remove the volumes associated with the container
            link (bool): Remove the specified link and not the underlying container
            force (bool): Force the removal of a running container (uses SIGKILL)
            """
            try:
                self.c.remove_container(containerid)
            except Exception,e:
                print "Remove container"+containerid+"error:"+str(e)
                sys.exit()
    
  • 相关阅读:
    dedecms5.7百度主动推送(实时) 开发
    胆囊结石食物选择
    读《遥远的救世主》与观看电视剧天道
    cnn健康增胖和调理好身体
    Machine-wide Progress Telerik Fiddler installation has been found at ...Please, use that one or uninstall it ...
    鼻炎治疗之路(转载)
    学医后才知道的小知识...
    一点浩然气,千里快哉风(修炼孟子浩然之气)
    孟尝君的逆袭
    张小龙和张一鸣的对立统一
  • 原文地址:https://www.cnblogs.com/zxpgo/p/4167808.html
Copyright © 2011-2022 走看看