本文整理匯總了Python中docker.client.Client.host方法的典型用法代碼示例。如果您正苦於以下問題:Python Client.host方法的具體用法?Python Client.host怎麽用?Python Client.host使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類docker.client.Client
的用法示例。
在下文中一共展示了Client.host方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_docker_client
# 需要導入模塊: from docker.client import Client [as 別名]
# 或者: from docker.client.Client import host [as 別名]
def get_docker_client():
"""
Try to fire up boot2docker and set any environmental variables
"""
# For Mac
try:
# Get boot2docker info (will fail if not Mac)
process = ['boot2docker', 'info']
p = subprocess.Popen(process, stdout=PIPE)
boot2docker_info = json.loads(p.communicate()[0])
# Defaults
docker_host = ''
docker_cert_path = ''
docker_tls_verify = ''
# Start the boot2docker VM if it is not already running
if boot2docker_info['State'] != "running":
print('Starting Boot2Docker VM:')
# Start up the Docker VM
process = ['boot2docker', 'start']
subprocess.call(process)
if ('DOCKER_HOST' not in os.environ) or ('DOCKER_CERT_PATH' not in os.environ) or ('DOCKER_TLS_VERIFY' not in os.environ):
# Get environmental variable values
process = ['boot2docker', 'shellinit']
p = subprocess.Popen(process, stdout=PIPE)
boot2docker_envs = p.communicate()[0].split()
for env in boot2docker_envs:
if 'DOCKER_HOST' in env:
docker_host = env.split('=')[1]
elif 'DOCKER_CERT_PATH' in env:
docker_cert_path = env.split('=')[1]
elif 'DOCKER_TLS_VERIFY' in env:
docker_tls_verify = env.split('=')[1]
# Set environmental variables
os.environ['DOCKER_TLS_VERIFY'] = docker_tls_verify
os.environ['DOCKER_HOST'] = docker_host
os.environ['DOCKER_CERT_PATH'] = docker_cert_path
else:
# Handle case when boot2docker is already running
docker_host = os.environ['DOCKER_HOST'].split('=')[1]
# Get the arguments form the environment
client_kwargs = kwargs_from_env(assert_hostname=False)
client_kwargs['version'] = MINIMUM_API_VERSION
# Find the right version of the API by creating a DockerClient with the minimum working version
# Then test to see if the Docker is running a later version than the minimum
# See: https://github.com/docker/docker-py/issues/439
version_client = DockerClient(**client_kwargs)
client_kwargs['version'] = get_api_version(MAX_CLIENT_DOCKER_API_VERSION, version_client.version()['ApiVersion'])
# Create Real Docker client
docker_client = DockerClient(**client_kwargs)
# Derive the host address only from string formatted: "tcp://<host>:<port>"
docker_client.host = docker_host.split(':')[1].strip('//')
return docker_client
# For Linux
except OSError:
# Find the right version of the API by creating a DockerClient with the minimum working version
# Then test to see if the Docker is running a later version than the minimum
# See: https://github.com/docker/docker-py/issues/439
version_client = DockerClient(base_url='unix://var/run/docker.sock', version=MINIMUM_API_VERSION)
version = get_api_version(MAX_CLIENT_DOCKER_API_VERSION, version_client.version()['ApiVersion'])
docker_client = DockerClient(base_url='unix://var/run/docker.sock', version=version)
docker_client.host = DEFAULT_DOCKER_HOST
return docker_client
except:
raise