我正在尝试对扩展坞机器云提供商运行命令,因此我需要获取命令docker-machine env digitalocean的内容,通常如下所示:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://1.2.3.4:2376"
export DOCKER_CERT_PATH="/Users/danh/.docker/machine/machines/digitalocean"
export DOCKER_MACHINE_NAME="digitalocean"
# Run this command to configure your shell:
# eval "$(docker-machine env digitalocean)"并使用上面的内容作为shell前缀,例如:
print 'outside with:' + local('echo $DOCKER_HOST')
with prefix(local('docker-machine env digitalocean', capture=True)):
print 'inside with:' + local('echo $DOCKER_HOST')
with prefix('DOCKER_HOST="tcp://1.2.3.4:2376"'):
print 'inside with (manual):' + local('echo $DOCKER_HOST')但是,这将返回:
outside with:tcp://192.168.99.100:2376
inside with:
inside with (manual):tcp://1.2.3.4:2376我能看到的解决这个问题的唯一方法就是手动分解local('docker-machine env digitalocean')的结果。然而,肯定有一种更具面料风格的方式?
发布于 2015-12-08 17:33:40
好吧,这是我到目前为止使用的解决方案,尽管感觉有点老生常谈:
def dm_env(machine):
"""
Sets the environment to use a given docker machine.
"""
_env = local('docker-machine env {}'.format(machine), capture=True)
# Reorganize into a string that could be used with prefix().
_env = re.sub(r'^#.*$', '', _env, flags=re.MULTILINE) # Remove comments
_env = re.sub(r'^export ', '', _env, flags=re.MULTILINE) # Remove `export `
_env = re.sub(r'\n', ' ', _env, flags=re.MULTILINE) # Merge to a single line
return _env
@task
def blah():
print 'outside with: ' + local('echo $DOCKER_HOST')
with prefix(dm_env('digitalocean')):
print 'inside with: ' + local('echo $DOCKER_HOST')输出:
outside with: tcp://192.168.99.100:2376
inside with: tcp://1.2.3.4:2376发布于 2017-05-06 01:24:45
获取所需信息的另一种方法是像shown in the documentation一样,对docker-machine inspect命令的输出使用格式化模板。
请注意,Python字符串中的花括号需要通过将它们加倍来进行转义,因此每次都会有四个开始和结束括号。
machine_name = dev
machine_ip = local("docker-machine inspect --format='{{{{.Driver.IPAddress}}}}' {0}".format(machine_name), capture=True)
machine_port = local("docker-machine inspect --format='{{{{.Driver.EnginePort}}}}' {0}".format(machine_name), capture=True)
machine_cert_path = local("docker-machine inspect --format='{{{{.HostOptions.AuthOptions.StorePath}}}}' {0}".format(machine), capture=True)现在,您可以使用shell_env context manager,通过设置相应的环境变量,将Docker守护进程临时指向远程计算机:
from fabric.api import shell_env
with shell_env(DOCKER_TLS_VERIFY='1',
DOCKER_HOST='tcp://{0}:{1}'.format(machine_ip, machine_port),
DOCKER_CERT_PATH=machine_cert_path,
DOCKER_MACHINE_NAME=machine_name):
# will print containers on the remote machine
local('docker ps -a')
# will print containers on your local machine
# since environment switch is only valid within the context manager
local('docker ps -a') https://stackoverflow.com/questions/34138184
复制相似问题