Python源码示例:docker.Client()

示例1
def _delete_image_on_docker_host(self, base_url, image_name, image_version):
        """
        Delete image from docker host if exists image called
        image_name:image_version.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)
        client = Client(base_url=base_url)
        try:
            client.remove_image(image=image_complete_name, force=True)
        except Exception:
            logger.info('There is no image called %s on docker host %s' %
                (image_complete_name, base_url))
            return None

        logger.info('Image %s on docker host %s has been deleted.' %
                (image_complete_name, base_url)) 
示例2
def _delete_image_on_docker_host(self, base_url, image_name, image_version):
        """
        Delete image from docker host if exists image called
        image_name:image_version.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)
        client = Client(base_url=base_url)
        try:
            client.remove_image(image=image_complete_name, force=True)
        except Exception:
            logger.info('There is no image called %s on docker host %s' %
                (image_complete_name, base_url))
            return None

        logger.info('Image %s on docker host %s has been deleted.' %
                (image_complete_name, base_url)) 
示例3
def _delete_image_on_docker_host(self, base_url, image_name, image_version):
        """
        Delete image from docker host if exists image called
        image_name:image_version.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)
        client = Client(base_url=base_url)
        try:
            client.remove_image(image=image_complete_name, force=True)
        except Exception:
            logger.info('There is no image called %s on docker host %s' %
                (image_complete_name, base_url))
            return None

        logger.info('Image %s on docker host %s has been deleted.' %
                (image_complete_name, base_url)) 
示例4
def _tag_image_with_new_name(self, base_url, old_image_name,
            old_image_version, image_name, image_version):
        """
        Docker tag old_image_name:old_image_version image_name:image_version.
        """
        client = Client(base_url=base_url)
        old_image = "{}:{}".format(old_image_name, old_image_version)
        try:
            response = client.tag(image=old_image, repository=image_name,
                tag=image_version)
        except Exception as e:
            logger.debug(e)
            response = False
        if not response:
            logger.info("Tag image {} to {}:{} failed.".format(old_image,
                image_name, image_version))
            return None

        image_token = self._get_image_token_on_docker_host(base_url,
            image_name, image_version)

        self._delete_image_on_docker_host(base_url, old_image_name,
            old_image_version)

        return image_token 
示例5
def _push_image_to_registry(self, base_url, image_name, image_version,
        image_token):
        """
        Push image from docker host to private registry.

        Returns the sha256 digest of the image.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        client = Client(base_url=base_url)
        try:
            response = [res for res in client.push(image_complete_name,
                stream=True)]
        except Exception:
            logger.error('Push image %s to registry failed.' %
                image_complete_name)
            return None

        try:
            digest = fetch_digest_from_response(response[-1])
        except Exception:
            logger.error('Parse the digest response error.')
            return None

        return digest 
示例6
def exec_dockerps():
    """
    Returns a list of docker inspect jsons, one for each container.

    This call executes the `docker inspect` command every time it is invoked.
    """
    try:
        client = docker.Client(
            base_url='unix://var/run/docker.sock', version='auto')
        containers = client.containers()
        inspect_arr = []
        for container in containers:
            inspect = exec_dockerinspect(container['Id'])
            inspect_arr.append(inspect)
    except docker.errors.DockerException as e:
        logger.warning(str(e))
        raise DockerutilsException('Failed to exec dockerps')

    return inspect_arr 
示例7
def setUp(self):
        self.docker = docker.Client(
            base_url='unix://var/run/docker.sock', version='auto')
        
        os.mkdir('/etc/cos-secrets', 0755 )
        f=open("/etc/cos-secrets/access_key", "w+")
        f.write("test")
        f.close()
        f=open("/etc/cos-secrets/secret_key", "w+")
        f.write("testforall")
        f.close()
        f=open("/etc/cos-secrets/location", "w+")
        f.write("test")
        f.close()
        self.start_minio_container()
        self.start_crawled_container() 
示例8
def setUp(self):
        self.docker = docker.Client(
            base_url='unix://var/run/docker.sock', version='auto')
        try:
            if len(self.docker.containers()) != 0:
                raise Exception(
                    "Sorry, this test requires a machine with no docker"
                    "containers running.")
        except requests.exceptions.ConnectionError:
            print ("Error connecting to docker daemon, are you in the docker"
                   "group? You need to be in the docker group.")

        self.docker.pull(repository='alpine', tag='latest')
        self.container = self.docker.create_container(
            image=self.image_name, command='/bin/sleep 60')
        self.tempd = tempfile.mkdtemp(prefix='crawlertest.')
        self.docker.start(container=self.container['Id']) 
示例9
def setUp(self):
        self.docker = docker.Client(
            base_url='unix://var/run/docker.sock', version='auto')
        try:
            if len(self.docker.containers()) != 0:
                raise Exception(
                    "Sorry, this test requires a machine with no docker"
                    "containers running.")
        except requests.exceptions.ConnectionError:
            print ("Error connecting to docker daemon, are you in the docker"
                   "group? You need to be in the docker group.")

        self.docker.pull(repository='node', tag='11.0')
        self.container = self.docker.create_container(
            image=self.image_name, command='sleep 60')
        self.docker.start(container=self.container['Id']) 
示例10
def setUp(self):
        root = logging.getLogger()
        root.setLevel(logging.INFO)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        ch.setFormatter(formatter)
        root.addHandler(ch)

        self.docker = docker.Client(base_url='unix://var/run/docker.sock',
                                    version='auto')
        try:
            if len(self.docker.containers()) != 0:
                raise Exception(
                    "Sorry, this test requires a machine with no docker"
                    "containers running.")
        except requests.exceptions.ConnectionError:
            print ("Error connecting to docker daemon, are you in the docker"
                   "group? You need to be in the docker group.")

        self.start_crawled_container() 
示例11
def setUp(self):
        self.docker = docker.Client(
            base_url='unix://var/run/docker.sock', version='auto')
        try:
            if len(self.docker.containers()) != 0:
                raise Exception(
                    "Sorry, this test requires a machine with no docker"
                    "containers running.")
        except requests.exceptions.ConnectionError:
            print ("Error connecting to docker daemon, are you in the docker"
                   "group? You need to be in the docker group.")

        self.docker.pull(repository='alpine', tag='latest')
        self.container = self.docker.create_container(
            image=self.image_name, command='/bin/sleep 60')
        self.tempd = tempfile.mkdtemp(prefix='crawlertest.')
        self.docker.start(container=self.container['Id']) 
示例12
def test_timeout(timeout, expected_timeout):
    if not hasattr(docker, 'APIClient'):
        setattr(docker, 'APIClient', docker.Client)

    expected_kwargs = {
        'timeout': expected_timeout
    }
    if hasattr(docker, 'AutoVersionClient'):
        expected_kwargs['version'] = 'auto'

    (flexmock(docker.APIClient)
        .should_receive('__init__')
        .with_args(**expected_kwargs)
        .once())

    kwargs = {}
    if timeout is not None:
        kwargs['timeout'] = timeout

    DockerTasker(**kwargs) 
示例13
def test_docker2():
    class MockClient(object):
        def __init__(self, **kwargs):
            pass

        def version(self):
            return {}

    for client in ['APIClient', 'Client']:
        if not hasattr(docker, client):
            setattr(docker, client, MockClient)

    (flexmock(docker)
        .should_receive('APIClient')
        .once()
        .and_raise(AttributeError))

    (flexmock(docker)
        .should_receive('Client')
        .once())

    DockerTasker() 
示例14
def __init__(self, client, container, interactive=True, stdout=None, stderr=None, stdin=None, logs=None):
        """
        Initialize the PTY using the docker.Client instance and container dict.
        """

        if logs is None:
            warnings.warn("The default behaviour of dockerpty is changing. Please add logs=1 to your dockerpty.start call to maintain existing behaviour. See https://github.com/d11wtq/dockerpty/issues/51 for details.", DeprecationWarning)
            logs = 1

        self.client = client
        self.container = container
        self.raw = None
        self.interactive = interactive
        self.stdout = stdout or sys.stdout
        self.stderr = stderr or sys.stderr
        self.stdin = stdin or sys.stdin
        self.logs = logs 
示例15
def init_callback(self):
        self.client = docker.Client(
            base_url=self.docker_url,
            version=DockerPlugin.MIN_DOCKER_API_VERSION)
        self.client.timeout = self.timeout

        # Check API version for stats endpoint support.
        try:
            version = self.client.version()['ApiVersion']
            if StrictVersion(version) < \
                    StrictVersion(DockerPlugin.MIN_DOCKER_API_VERSION):
                raise Exception
        except:
            collectd.warning(('Docker daemon at {url} does not '
                              'support container statistics!')
                             .format(url=self.docker_url))
            return False

        collectd.register_read(self.read_callback)
        collectd.info(('Collecting stats about Docker containers from {url} '
                       '(API version {version}; timeout: {timeout}s).')
                      .format(url=self.docker_url,
                              version=version,
                              timeout=self.timeout))
        return True 
示例16
def __init__(self, config):
        self.pod_name = None
        self.namespace = None
        self.docker_id = None
        self.policy_parser = None

        # Get configuration from the given dictionary.
        logger.debug("Plugin running with config: %s", config)
        self.auth_token = config[KUBE_AUTH_TOKEN_VAR]
        self.api_root = config[KUBE_API_ROOT_VAR]
        self.client_certificate = config[KUBE_CLIENT_CERTIFICATE_VAR]
        self.client_certificate_key = config[KUBE_CLIENT_CERTIFICATE_KEY_VAR]
        self.ca_certificate = config[KUBE_CA_CERTIFICATE_VAR]
        self.calico_ipam = config[CALICO_IPAM_VAR].lower()
        self.default_policy = config[DEFAULT_POLICY_VAR].lower()

        self._datastore_client = IPAMClient()
        self._docker_client = Client(
            version=DOCKER_VERSION,
            base_url=os.getenv("DOCKER_HOST", "unix://var/run/docker.sock")) 
示例17
def __init__(self):
        self._containers = None
        self._images = None  # displayed images
        self._all_images = None  # docker images -a
        self._df = None

        kwargs = {"version": "auto"}
        kwargs.update(docker.utils.kwargs_from_env(assert_hostname=False))

        try:
            APIClientClass = docker.Client  # 1.x
        except AttributeError:
            APIClientClass = docker.APIClient  # 2.x

        try:
            self.client = APIClientClass(**kwargs)
        except docker.errors.DockerException as ex:
            raise TerminateApplication("can't establish connection to docker daemon: {0}".format(str(ex)))

        self.scratch_image = RootImage(self)

    # backend queries 
示例18
def find_endpoint():
    if os.environ.get("NO_DOCKER"):
        yield ('localhost', 9200)
    else:
        es_tag = os.environ.get("ES_VERSION", '2.4')
        cl = docker.Client(version='auto')
        cl.pull('elasticsearch:{}'.format(es_tag))
        container = cl.create_container(
            image='elasticsearch:{}'.format(es_tag),
            name='aioes-test-server',
            ports=[9200],
            detach=True)
        cid = container['Id']
        cl.start(container=cid)
        ins = cl.inspect_container(cid)
        try:
            yield (ins['NetworkSettings']['IPAddress'], 9200)
        finally:
            cl.kill(container=cid)
            cl.remove_container(cid) 
示例19
def __init__(self):
        timeout = os.environ.get('DOCKER_CLIENT_TIMEOUT', docker.constants.DEFAULT_TIMEOUT_SECONDS)
        self.client = docker.Client(version='auto', timeout=timeout)
        self.registry = settings.REGISTRY_HOST + ':' + str(settings.REGISTRY_PORT) 
示例20
def _load_image_on_docker_host(self, base_url, build_file, image_name,
                image_version='latest'):
        """
        Import container snapshot on the selected docker host.

        'base_url': the url of docker host.
        'build_file': the name of the build file in absolute path.
        'image_name': the name of the image, containing registry address, user
        name and image name.
        'image_version': the version of the image.

        Returns:
        'token': the image token
        """
        self._delete_image_on_docker_host(base_url, self.old_image_name,
            self.old_image_version)
        self._delete_image_on_docker_host(base_url, image_name, image_version)

        client = Client(base_url=base_url)
        try:
            with open(build_file, 'rb') as fileobj:
                client.load_image(fileobj)
        except Exception:
            logger.error('load image file on docker host %s failed.' % base_url)
            return None

        return self._tag_image_with_new_name(base_url, self.old_image_name,
            self.old_image_version, image_name, image_version) 
示例21
def _import_snapshot_on_docker_host(self, base_url, build_file, image_name,
                image_version='latest'):
        """
        Import container snapshot on the selected docker host.

        'base_url': the url of docker host.
        'build_file': the name of the build file in absolute path.
        'image_name': the name of the image, containing registry address, user
        name and image name.
        'image_version': the version of the image.

        Returns:
        'token': the image token
        """
        self._delete_image_on_docker_host(base_url, image_name, image_version)

        client = Client(base_url=base_url)
        try:
            res_json = client.import_image_from_file(build_file, image_name,
                image_version)
            res = json.loads(res_json)
        except Exception:
            logger.error('import snapshot on docker host %s failed.' % base_url)
            return None

        return res.get('status', None) 
示例22
def _push_image_to_registry(self, base_url, image_name, image_version,
        image_token):
        """
        Push image from docker host to private registry.

        Returns the sha256 digest of the image.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        if not self._is_image_on_docker_host(base_url, image_token):
            logger.error('There is no image called %s on docker host %s' %
                (image_complete_name, base_url))
            return None

        client = Client(base_url=base_url)
        try:
            response = [res for res in client.push(image_complete_name,
                stream=True)]
        except Exception:
            logger.error('Communicate with %s failed.' % base_url)
            return None

        try:
            digest = fetch_digest_from_response(response[-1])
        except Exception:
            logger.error('Parse the digest response error.')
            return None

        return digest 
示例23
def _is_image_on_docker_host(self, base_url, image_token):
        """
        Check the image whether or not on docker host.
        """
        client = Client(base_url=base_url)
        try:
            response = client.images(quiet=True)
        except Exception:
            logger.error("Connected %s failed." % base_url)
            return False
        if image_token not in response:
            return False
        return True 
示例24
def _get_image_token_on_docker_host(self, base_url, image_name,
        image_version):
        """
        Given the image name and version, return the token of the image on the
        docker host.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        logger.debug(image_complete_name)

        client = Client(base_url=base_url)
        try:
            images = client.images()
        except Exception as e:
            logger.debug(e)
            logger.debug("Communicate with docker host {} failed.".format(
                base_url))
            return None

        tokens = [image['Id'] for image in images
            if image_complete_name in image['RepoTags']]

        if not tokens:
            logger.info("The docker host {} has no image {}:{}".format(base_url,
                image_name, image_version))
            return None
        return tokens[0] 
示例25
def _build_image_on_docker_host(self, base_url, build_file, dockerfile,
            image_name, image_version):
        """
        Build image on the selected docker host by Dockerfile.

        'base_url': the url of docker host.
        'build_file': the name of the build file in absolute path.
        'dockerfile': Dockerfile path in build_file.
        'image_name': the name of the image, containing registry address, user
        name and image name.
        'image_version': the version of the image.

        Returns:
        'token': the image token
        """
        self._delete_image_on_docker_host(base_url, image_name, image_version)

        client = Client(base_url=base_url)
        fileobj = open(build_file, 'rb')
        image_complete_name = '%s:%s' % (image_name, image_version)
        try:
            response = [line for line in client.build(
                fileobj=fileobj,
                custom_context=True,
                dockerfile=dockerfile,
                rm=True,
                tag=image_complete_name)]
        except APIError as error:
            logger.debug(error)
            logger.error('Cannot locate specified Dockerfile: %s.' %
                (self.dockerfile))
            fileobj.close()
            return None
        except Exception as error:
            logger.debug(error)
            logger.error('Build image %s failed.' % image_complete_name)
            fileobj.close()
            return None
        fileobj.close()

        return self._get_image_token(base_url, image_complete_name) 
示例26
def _get_image_token(self, base_url, image_complete_name):
        """
        """
        client = Client(base_url=base_url)
        try:
            token = client.inspect_image(image_complete_name).get('Id', None)
        except Exception:
            logger.error('Can\'t get the token of image %s on docker host %s' %
                (image_complete_name, base_url))
            return None
        return token 
示例27
def _get_image_token_on_docker_host(self, base_url, image_name,
        image_version):
        """
        Given the image name and version, return the token of the image on the
        docker host.
        """
        image_complete_name = '%s:%s' %(image_name, image_version)

        logger.debug(image_complete_name)

        client = Client(base_url=base_url)
        try:
            images = client.images()
        except Exception as e:
            logger.debug(e)
            logger.debug("Communicate with docker host {} failed.".format(
                base_url))
            return None

        tokens = [image['Id'] for image in images
            if image_complete_name in image['RepoTags']]

        if not tokens:
            logger.info("The docker host {} has no image {}:{}".format(base_url,
                image_name, image_version))
            return None
        return tokens[0] 
示例28
def docker_client():
    client = Client(base_url='unix://var/run/docker.sock', timeout=180)
    return client 
示例29
def docker_client():
    client_cfg = docker_utils.kwargs_from_env()
    return docker.Client(version='1.21', **client_cfg) 
示例30
def exec_docker_history(long_id):
    try:
        client = docker.Client(base_url='unix://var/run/docker.sock',
                               version='auto')
        image = client.inspect_container(long_id)['Image']
        history = client.history(image)
        return history
    except docker.errors.DockerException as e:
        logger.warning(str(e))
        raise DockerutilsException('Failed to exec dockerhistory')