本文整理汇总了Python中docker.Client方法的典型用法代码示例。如果您正苦于以下问题:Python docker.Client方法的具体用法?Python docker.Client怎么用?Python docker.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker
的用法示例。
在下文中一共展示了docker.Client方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _delete_image_on_docker_host
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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: _tag_image_with_new_name
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例3: _push_image_to_registry
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例4: exec_dockerps
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例5: setUp
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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()
示例6: setUp
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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'])
示例7: setUp
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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'])
示例8: setUp
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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()
示例9: test_timeout
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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)
示例10: test_docker2
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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()
示例11: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例12: init_callback
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例13: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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"))
示例14: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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
示例15: find_endpoint
# 需要导入模块: import docker [as 别名]
# 或者: from docker import Client [as 别名]
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)