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()
    
  • 相关阅读:
    编程官方文档中常见的参数格式说明
    console.dir()和console.log()的区别
    JS中逗号运算符的用法
    Image 对象事件
    git已经删除了远程分支,本地仍然能看到
    Nginx初入
    WebApi设置SessionState为Required
    WebAPI2使用AutoFac依赖注入完整解决方案。
    CodeFirst时使用T4模板
    mysql5.7 java读取乱码
  • 原文地址:https://www.cnblogs.com/zxpgo/p/4167808.html
Copyright © 2011-2022 走看看