本文整理汇总了Python中docker.client.Client.start方法的典型用法代码示例。如果您正苦于以下问题:Python Client.start方法的具体用法?Python Client.start怎么用?Python Client.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.client.Client
的用法示例。
在下文中一共展示了Client.start方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class Runner:
"""
This class is in charge of loading test suites and runs them on different environments
"""
STOP_TIMEOUT = 3
def __init__(self, cfg):
from docker.client import Client
from docker.utils import kwargs_from_env
self.config = cfg
docker_kwargs = kwargs_from_env()
docker_kwargs['tls'].assert_hostname = False
self.docker = Client(**docker_kwargs)
def run(self, build, *tests):
"""
Run all the test suites passed in as parameters on the given build
This method will start a container of the build, run the tests and stop it
"""
from docker.utils import create_host_config
print("Running tests on {}".format(build.name))
ports = self.config['environment']['ports']
host = self.config['global'].get('docker_host', os.getenv('DOCKER_HOST').split('/')[-1].split(':')[0])
container = self.docker.create_container(
image=build.docker_tag,
command='/bin/bash -c "nc -l 8080"',
ports=ports,
host_config=create_host_config(port_bindings=dict(zip(ports, [None] * len(ports))))
).get('Id')
self.docker.start(container)
info = self.docker.inspect_container(container)
port_bindings = {port: bind[0]['HostPort'] for port, bind in info['NetworkSettings']['Ports'].items()}
for test in tests:
test.run(host, port_bindings, build.context)
self.docker.stop(container, timeout=self.STOP_TIMEOUT)
log_file_path = os.path.join(self.config['global'].get('logs_dir', '/tmp'), '{}.log'.format(build.name))
with open(log_file_path, 'wb') as logs:
logs.write(self.docker.logs(container, stdout=True, stderr=True, stream=False))
print("Container logs wrote to {}".format(log_file_path))
示例2: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerController(object):
def _load_config(self):
with open('./config.yaml') as fh:
config = yaml.load(fh)
return config
def __init__(self):
config = self._load_config()
self.LOCAL_REDIS_HOST = 'netcapsule_redis_1'
self.REDIS_HOST = os.environ.get('REDIS_HOST', self.LOCAL_REDIS_HOST)
self.PYWB_HOST = os.environ.get('PYWB_HOST', 'netcapsule_pywb_1')
self.C_EXPIRE_TIME = config['init_container_expire_secs']
self.Q_EXPIRE_TIME = config['queue_expire_secs']
self.REMOVE_EXP_TIME = config['remove_expired_secs']
self.VERSION = config['api_version']
self.VNC_PORT = config['vnc_port']
self.CMD_PORT = config['cmd_port']
self.MAX_CONT = config['max_containers']
self.image_prefix = config['image_prefix']
self.browser_list = config['browsers']
self.browser_paths = {}
for browser in self.browser_list:
path = browser['path']
if path in self.browser_paths:
raise Exception('Already a browser for path {0}'.format(path))
self.browser_paths[path] = browser
self.default_browser = config['default_browser']
self.redirect_paths = config['redirect_paths']
self.randompages = []
try:
with open(config['random_page_file']) as fh:
self.randompages = list([line.rstrip() for line in fh])
except Exception as e:
print(e)
self.redis = redis.StrictRedis(host=self.REDIS_HOST)
self.redis.setnx('next_client', '1')
self.redis.setnx('max_containers', self.MAX_CONT)
self.redis.setnx('num_containers', '0')
self.redis.setnx('cpu_auto_adjust', 5.5)
throttle_samples = config['throttle_samples']
self.redis.setnx('throttle_samples', throttle_samples)
throttle_max_avg = config['throttle_max_avg']
self.redis.setnx('throttle_max_avg', throttle_max_avg)
self.redis.setnx('container_expire_secs',
config['full_container_expire_secs'])
self.T_EXPIRE_TIME = config['throttle_expire_secs']
if os.path.exists('/var/run/docker.sock'):
self.cli = Client(base_url='unix://var/run/docker.sock',
version=self.VERSION)
else:
kwargs = kwargs_from_env(assert_hostname=False)
kwargs['version'] = self.VERSION
self.cli = Client(**kwargs)
def _get_host_port(self, info, port, default_host):
info = info['NetworkSettings']['Ports'][str(port) + '/tcp']
info = info[0]
host = info['HostIp']
if host == '0.0.0.0' and default_host:
host = default_host
return host + ':' + info['HostPort']
def timed_new_container(self, browser, env, host, client_id):
start = time.time()
info = self.new_container(browser, env, host)
end = time.time()
dur = end - start
time_key = 't:' + client_id
self.redis.setex(time_key, self.T_EXPIRE_TIME, dur)
throttle_samples = int(self.redis.get('throttle_samples'))
print('INIT DUR: ' + str(dur))
self.redis.lpush('init_timings', time_key)
self.redis.ltrim('init_timings', 0, throttle_samples - 1)
return info
def new_container(self, browser_id, env=None, default_host=None):
browser = self.browser_paths.get(browser_id)
# get default browser
if not browser:
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
#.........这里部分代码省略.........
tarinfo.mtime = time.time()
tar.addfile(tarinfo, BytesIO(content))
tar.close()
tarstream.seek(0)
try:
status = self.client.put_archive(container = container_name, path = path, data = tarstream)
logger.info("container %s put_archive %s/%s returns %s" % (container_name, path, filename, status))
except Exception as e:
logger.error("container %s put_archive %s/%s fails -- %s" % (container_name, path, filename, e))
def managemount(self, container):
from kooplex.lib.fs_dirname import Dirname
path, filename = os.path.split(self.dockerconf.get('mountconf', '/tmp/mount.conf'))
mapper = []
for v in container.volumes:
mapper.extend([ "%s:%s" % (v.volumetype, d) for d in Dirname.containervolume_listfolders(container, v) ])
#NOTE: mounter uses read to process the mapper configuration, thus we need to make sure '\n' terminates the config mapper file
mapper.append('')
logger.debug("container %s map %s" % (container, mapper))
file_data = "\n".join(mapper).encode('utf8')
self._writefile(container.name, path, filename, file_data)
def trigger_impersonator(self, vcproject): #FIXME: dont call it 1-by-1
from kooplex.lib.fs_dirname import Dirname
container_name = self.dockerconf.get('impersonator', 'impersonator')
path, filename = os.path.split(self.dockerconf.get('gitcommandconf', '/tmp/gitcommand.conf'))
cmdmaps = []
token = vcproject.token
fn_clonesh = os.path.join(Dirname.vcpcache(vcproject), "clone.sh")
fn_key = os.path.join(Dirname.userhome(vcproject.token.user), '.ssh', token.fn_rsa)
cmdmaps.append("%s:%s:%s:%s" % (token.user.username, fn_key, token.repository.domain, fn_clonesh))
cmdmaps.append('')
file_data = "\n".join(cmdmaps).encode('utf8')
self._writefile(container_name, path, filename, file_data)
def run_container(self, container):
docker_container_info = self.get_container(container)
if docker_container_info is None:
logger.debug("Container did not exist, Creating new one")
docker_container_info = self.create_container(container)
container_state = docker_container_info['Status']
if container_state == 'Created' or container_state.startswith('Exited'):
logger.debug("Starting container")
self.start_container(container)
def refresh_container_state(self, container):
docker_container_info = self.get_container(container)
container_state = docker_container_info['State']
logger.debug("Container state %s" % container_state)
container.last_message = str(container_state)
container.last_message_at = now()
container.save()
def start_container(self, container):
self.client.start(container.name)
# we need to retrieve the container state after starting it
docker_container_info = self.get_container(container)
container_state = docker_container_info['State']
logger.debug("Container state %s" % container_state)
container.last_message = str(container_state)
container.last_message_at = now()
assert container_state == 'running', "Container failed to start: %s" % docker_container_info
def stop_container(self, container):
try:
self.client.stop(container.name)
container.last_message = 'Container stopped'
except Exception as e:
logger.warn("docker container not found by API -- %s" % e)
container.last_message = str(e)
def remove_container(self, container):
try:
self.client.remove_container(container.name)
container.last_message = 'Container removed'
container.last_message_at = now()
except Exception as e:
logger.warn("docker container not found by API -- %s" % e)
container.last_message = str(e)
container.last_message_at = now()
logger.debug("Container removed %s" % container.name)
#FIXME: az execute2 lesz az igazi...
def execute(self, container, command):
logger.info("execution: %s in %s" % (command, container))
execution = self.client.exec_create(container = container.name, cmd = shlex.split(command))
return self.client.exec_start(execution, detach = False)
def execute2(self, container, command):
logger.info("execution: %s in %s" % (command, container))
execution = self.client.exec_create(container = container.name, cmd = shlex.split(command))
response = self.client.exec_start(exec_id = execution['Id'], stream = False)
check = self.client.exec_inspect(exec_id = execution['Id'])
self.check = check
if check['ExitCode'] != 0:
logger.error('Execution %s in %s failed -- %s' % (command, container, check))
return response.decode()
示例4: DockerPyClient
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerPyClient(DockerClient):
def __init__(self, remote, username=None, password=None, email=None):
super(DockerPyClient,self).__init__()
self.client = Client(base_url=remote, version='1.15')
self.log = logging.getLogger(__name__)
self.log.debug('password %s, remote = %s, username=%s', password, remote, username)
if username:
self.client.login(username=username, password=password, email=email)
def docker_images(self, filters=None):
return self.client.images(filters=filters)
def __id(self, ioc):
if ioc and 'Id' in ioc:
return ioc['Id']
return None
def docker_containers(self):
return [{
'Id': cont['Id'],
'Tag': cont['Image'],
'Image': self.__id(self.image(cont['Image'])),
'Names': cont['Names'],
'Ports': cont['Ports'],
'Created': cont['Created'],
'Command': cont['Command'],
'Status': cont['Status'],
'Running': cont['Status'].startswith('Up ') or cont['Status'].startswith('Restarting ')
} for cont in self.client.containers(all=True)]
def docker_pull(self, image):
(repository, tag) = self.tag(image)
existing = self.image(image)
for line in self.client.pull(repository=repository, stream=True, insecure_registry=True):
parsed = json.loads(line)
self.log.debug('parsed %s' % parsed)
if 'error' in parsed:
raise Exception(parsed['error'])
# Check if image updated
self.flush_images()
newer = self.image(image)
if not existing or (newer['Id'] != existing['Id']):
return True
return False
def docker_run(self, entry):
volumes = ['/var/log/ext']
kwargs = {
'image': entry['image'],
'volumes': volumes,
'detach': True,
'environment': {
'DOCKER_IMAGE': entry['image']
}
}
if 'name' in entry:
kwargs['name'] = entry['name']
if 'env' in entry:
kwargs['environment'].update(entry['env'])
if 'cpu' in entry:
kwargs['cpu_shares'] = entry['cpu']
if 'memory' in entry:
kwargs['mem_limit'] = entry['memory']
if 'entrypoint' in entry:
kwargs['entrypoint'] = entry['entrypoint']
if 'command' in entry:
kwargs['command'] = entry['command']
if 'volumes' in entry:
volumes.extend([vol['containerPath'] for vol in entry['volumes'] if 'containerPath' in vol])
volsFrom = [vol['from'] for vol in entry['volumes'] if 'from' in vol]
if len(volsFrom):
kwargs['volumes_from'] = volsFrom
if 'portMappings' in entry:
kwargs['ports'] = [p['containerPort'] for p in entry['portMappings']]
container = self.client.create_container(**kwargs)
self.docker_start(container['Id'], entry)
return container['Id']
def docker_start(self, container, entry=None):
logsBound = False
binds = {}
#.........这里部分代码省略.........
示例5: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerController(object):
def _load_config(self):
with open('./config.yaml') as fh:
config = yaml.load(fh)
return config
def __init__(self):
config = self._load_config()
self.REDIS_HOST = config['redis_host']
self.PYWB_HOST = config['pywb_host']
self.C_EXPIRE_TIME = config['container_expire_secs']
self.Q_EXPIRE_TIME = config['queue_expire_secs']
self.REMOVE_EXP_TIME = config['remove_expired_secs']
self.VERSION = config['api_version']
self.VNC_PORT = config['vnc_port']
self.CMD_PORT = config['cmd_port']
self.image_prefix = config['image_prefix']
self.browsers = config['browsers']
self.redis = redis.StrictRedis(host=self.REDIS_HOST)
self.redis.setnx('next_client', '1')
if os.path.exists('/var/run/docker.sock'):
self.cli = Client(base_url='unix://var/run/docker.sock',
version=self.VERSION)
else:
kwargs = kwargs_from_env()
kwargs['tls'].assert_hostname = False
kwargs['version'] = self.VERSION
self.cli = Client(**kwargs)
def new_container(self, browser, env=None):
tag = self.browsers.get(browser)
# get default browser
if not tag:
tag = self.browsers['']
container = self.cli.create_container(image=self.image_prefix + '/' + tag,
ports=[self.VNC_PORT, self.CMD_PORT],
environment=env,
)
id_ = container.get('Id')
res = self.cli.start(container=id_,
port_bindings={self.VNC_PORT: None, self.CMD_PORT: None},
links={self.PYWB_HOST: self.PYWB_HOST,
self.REDIS_HOST: self.REDIS_HOST},
volumes_from=['netcapsule_shared_data_1'],
)
vnc_port = self.cli.port(id_, self.VNC_PORT)
vnc_port = vnc_port[0]['HostPort']
cmd_port = self.cli.port(id_, self.CMD_PORT)
cmd_port = cmd_port[0]['HostPort']
info = self.cli.inspect_container(id_)
ip = info['NetworkSettings']['IPAddress']
short_id = id_[:12]
self.redis.hset('all_containers', short_id, ip)
self.redis.setex('c:' + short_id, self.C_EXPIRE_TIME, 1)
return vnc_port, cmd_port
def remove_container(self, short_id, ip):
print('REMOVING ' + short_id)
try:
self.cli.remove_container(short_id, force=True)
except Exception as e:
print(e)
self.redis.hdel('all_containers', short_id)
self.redis.delete('c:' + short_id)
ip_keys = self.redis.keys(ip +':*')
for key in ip_keys:
self.redis.delete(key)
def remove_all(self, check_expired=False):
all_containers = self.redis.hgetall('all_containers')
for short_id, ip in all_containers.iteritems():
if check_expired:
remove = not self.redis.get('c:' + short_id)
else:
remove = True
if remove:
self.remove_container(short_id, ip)
def add_new_client(self):
#client_id = base64.b64encode(os.urandom(27))
#self.redis.rpush('q:clients', client_id)
client_id = self.redis.incr('clients')
#.........这里部分代码省略.........
示例6: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerController(object):
def _load_config(self):
with open("./config.yaml") as fh:
config = yaml.load(fh)
return config
def __init__(self):
config = self._load_config()
self.LOCAL_REDIS_HOST = "netcapsule_redis_1"
self.REDIS_HOST = os.environ.get("REDIS_HOST", self.LOCAL_REDIS_HOST)
self.PYWB_HOST = os.environ.get("PYWB_HOST", "netcapsule_pywb_1")
self.C_EXPIRE_TIME = config["init_container_expire_secs"]
self.Q_EXPIRE_TIME = config["queue_expire_secs"]
self.REMOVE_EXP_TIME = config["remove_expired_secs"]
self.VERSION = config["api_version"]
self.VNC_PORT = config["vnc_port"]
self.CMD_PORT = config["cmd_port"]
self.MAX_CONT = config["max_containers"]
self.image_prefix = config["image_prefix"]
self.browser_list = config["browsers"]
self.browser_paths = {}
for browser in self.browser_list:
path = browser["path"]
if path in self.browser_paths:
raise Exception("Already a browser for path {0}".format(path))
self.browser_paths[path] = browser
self.default_browser = config["default_browser"]
self.redirect_paths = config["redirect_paths"]
self.randompages = []
try:
with open(config["random_page_file"]) as fh:
self.randompages = list([line.rstrip() for line in fh])
except Exception as e:
print(e)
self.redis = redis.StrictRedis(host=self.REDIS_HOST)
self.redis.setnx("next_client", "1")
self.redis.setnx("max_containers", self.MAX_CONT)
throttle_samples = config["throttle_samples"]
self.redis.setnx("throttle_samples", throttle_samples)
throttle_max_avg = config["throttle_max_avg"]
self.redis.setnx("throttle_max_avg", throttle_max_avg)
self.redis.setnx("container_expire_secs", config["full_container_expire_secs"])
self.T_EXPIRE_TIME = config["throttle_expire_secs"]
if os.path.exists("/var/run/docker.sock"):
self.cli = Client(base_url="unix://var/run/docker.sock", version=self.VERSION)
else:
kwargs = kwargs_from_env(assert_hostname=False)
kwargs["version"] = self.VERSION
self.cli = Client(**kwargs)
def _get_host_port(self, info, port, default_host):
info = info["NetworkSettings"]["Ports"][str(port) + "/tcp"]
info = info[0]
host = info["HostIp"]
if host == "0.0.0.0" and default_host:
host = default_host
return host + ":" + info["HostPort"]
def timed_new_container(self, browser, env, host, client_id):
start = time.time()
info = dc.new_container(browser, env, host)
end = time.time()
dur = end - start
time_key = "t:" + client_id
self.redis.setex(time_key, self.T_EXPIRE_TIME, dur)
throttle_samples = int(self.redis.get("throttle_samples"))
print("INIT DUR: " + str(dur))
self.redis.lpush("init_timings", time_key)
self.redis.ltrim("init_timings", 0, throttle_samples - 1)
return info
def new_container(self, browser_id, env=None, default_host=None):
browser = self.browser_paths.get(browser_id)
# get default browser
if not browser:
browser = self.browser_paths.get(self.default_browser)
container = self.cli.create_container(
image=self.image_prefix + "/" + browser["id"], ports=[self.VNC_PORT, self.CMD_PORT], environment=env
#.........这里部分代码省略.........
示例7: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerController(object):
def _load_config(self):
with open('./config.yaml') as fh:
config = yaml.load(fh)
return config
def __init__(self):
config = self._load_config()
self.LOCAL_REDIS_HOST = 'netcapsule_redis_1'
self.REDIS_HOST = os.environ.get('REDIS_HOST', self.LOCAL_REDIS_HOST)
self.PYWB_HOST = os.environ.get('PYWB_HOST', 'netcapsule_pywb_1')
self.C_EXPIRE_TIME = config['init_container_expire_secs']
self.Q_EXPIRE_TIME = config['queue_expire_secs']
self.REMOVE_EXP_TIME = config['remove_expired_secs']
self.VERSION = config['api_version']
self.VNC_PORT = config['vnc_port']
self.CMD_PORT = config['cmd_port']
self.MAX_CONT = config['max_containers']
self.image_prefix = config['image_prefix']
self.browser_list = config['browsers']
self.browser_paths = {}
for browser in self.browser_list:
path = browser['path']
if path in self.browser_paths:
raise Exception('Already a browser for path {0}'.format(path))
self.browser_paths[path] = browser
self.default_browser = config['default_browser']
self.redirect_paths = config['redirect_paths']
self.redis = redis.StrictRedis(host=self.REDIS_HOST)
self.redis.setnx('next_client', '1')
self.redis.setnx('max_containers', self.MAX_CONT)
throttle_samples = config['throttle_samples']
self.redis.setnx('throttle_samples', throttle_samples)
throttle_max_avg = config['throttle_max_avg']
self.redis.setnx('throttle_max_avg', throttle_max_avg)
self.redis.setnx('container_expire_secs',
config['full_container_expire_secs'])
self.T_EXPIRE_TIME = config['throttle_expire_secs']
if os.path.exists('/var/run/docker.sock'):
self.cli = Client(base_url='unix://var/run/docker.sock',
version=self.VERSION)
else:
kwargs = kwargs_from_env(assert_hostname=False)
kwargs['version'] = self.VERSION
self.cli = Client(**kwargs)
def _get_host_port(self, info, port, default_host):
info = info['NetworkSettings']['Ports'][str(port) + '/tcp']
info = info[0]
host = info['HostIp']
if host == '0.0.0.0' and default_host:
host = default_host
return host + ':' + info['HostPort']
def timed_new_container(self, browser, env, host, client_id):
start = time.time()
info = dc.new_container(browser, env, host)
end = time.time()
dur = end - start
time_key = 't:' + client_id
self.redis.setex(time_key, self.T_EXPIRE_TIME, dur)
throttle_samples = int(self.redis.get('throttle_samples'))
print('INIT DUR: ' + str(dur))
self.redis.lpush('init_timings', time_key)
self.redis.ltrim('init_timings', 0, throttle_samples - 1)
return info
def new_container(self, browser_id, env=None, default_host=None):
browser = self.browser_paths.get(browser_id)
# get default browser
if not browser:
browser = self.browser_paths.get(self.default_browser)
container = self.cli.create_container(image=self.image_prefix + '/' + browser['id'],
ports=[self.VNC_PORT, self.CMD_PORT],
environment=env,
)
id_ = container.get('Id')
res = self.cli.start(container=id_,
#.........这里部分代码省略.........
示例8: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
class DockerController(object):
def _load_config(self):
with open('./config.yaml') as fh:
config = yaml.load(fh)
return config
def __init__(self):
config = self._load_config()
self.REDIS_HOST = config['redis_host']
self.PYWB_HOST = config['pywb_host']
self.EXPIRE_TIME = config['expire_secs']
self.REMOVE_EXP_TIME = config['remove_expired_secs']
self.VERSION = config['api_version']
self.VNC_PORT = config['vnc_port']
self.CMD_PORT = config['cmd_port']
self.image_prefix = config['image_prefix']
self.browsers = config['browsers']
self.redis = redis.StrictRedis(host=self.REDIS_HOST)
if os.path.exists('/var/run/docker.sock'):
self.cli = Client(base_url='unix://var/run/docker.sock',
version=self.VERSION)
else:
kwargs = kwargs_from_env()
kwargs['tls'].assert_hostname = False
kwargs['version'] = self.VERSION
self.cli = Client(**kwargs)
def new_container(self, browser, env=None):
tag = self.browsers.get(browser)
# get default browser
if not tag:
tag = self.browsers['']
container = self.cli.create_container(image=self.image_prefix + '/' + tag,
ports=[self.VNC_PORT, self.CMD_PORT],
environment=env)
id_ = container.get('Id')
res = self.cli.start(container=id_,
port_bindings={self.VNC_PORT: None, self.CMD_PORT: None},
links={self.PYWB_HOST: self.PYWB_HOST,
self.REDIS_HOST: self.REDIS_HOST})
vnc_port = self.cli.port(id_, self.VNC_PORT)
vnc_port = vnc_port[0]['HostPort']
cmd_port = self.cli.port(id_, self.CMD_PORT)
cmd_port = cmd_port[0]['HostPort']
info = self.cli.inspect_container(id_)
ip = info['NetworkSettings']['IPAddress']
short_id = id_[:12]
self.redis.hset('all_containers', short_id, ip)
self.redis.setex('c:' + short_id, self.EXPIRE_TIME, 1)
return vnc_port, cmd_port
def remove_container(self, short_id, ip):
print('REMOVING ' + short_id)
try:
self.cli.remove_container(short_id, force=True)
except Exception as e:
print(e)
self.redis.hdel('all_containers', short_id)
self.redis.delete('c:' + short_id)
ip_keys = self.redis.keys(ip +':*')
for key in ip_keys:
self.redis.delete(key)
def remove_all(self, check_expired=False):
all_containers = self.redis.hgetall('all_containers')
for short_id, ip in all_containers.iteritems():
if check_expired:
remove = not self.redis.get('c:' + short_id)
else:
remove = True
if remove:
self.remove_container(short_id, ip)
示例9: _run_build
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
def _run_build(build):
"""
The REPOSITORY_DIR will contain a directory for each Project
instance.
Each Project instance directory will have a directory called "clone"
which is the master clone. Then, each time a build is queued, we
create a local clone from that repository named after the current
build's commit sha.
REPOSITORY_DIR/
django/
clone/
a18bc/
.../
Once the local clone is created, we build the docker image, create
the container and run the tests.
"""
project = build.project
# docker client
c = Client(build.host)
project_dir = os.path.join(REPOSITORY_DIR, project.slug)
main_repo_path = os.path.join(project_dir, 'clone')
if not os.path.exists(main_repo_path):
# assert project repo present
os.makedirs(project_dir)
# clone repo
repo = Repo.clone_from(project.repository, main_repo_path)
else:
repo = Repo(main_repo_path)
remote = repo.remote()
remote.pull()
# build_path is a local clone of the project and it's named after the
# current build's commit sha
build_path = os.path.join(project_dir, build.commit_sha)
if not os.path.exists(build_path):
repo = Repo.clone_from(main_repo_path, build_path)
else:
repo = Repo(build_path)
g = repo.git
g.checkout(build.commit_sha)
image_name = ':'.join([project.slug, build.commit_sha])
img_id, res = c.build(build_path)
# create image
c.tag(img_id, repository=project.slug, tag=build.commit_sha)
# run build command from new image and report output
container = c.create_container(image_name, project.build_command,
stdin_open=True, tty=True)
container_id = container.get('Id')
c.start(container_id)
return_code = c.wait(container_id)
out = c.logs(container_id)
build.result = out
build.return_code = return_code
build.save()
示例10: Client
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import start [as 别名]
from docker.client import Client
from docker.utils import kwargs_from_env
cli = Client(**kwargs_from_env())
container = cli.create_container(image='busybox:latest', command='/bin/echo 2342', rm=True)
print(container)
response = cli.start(container=container.get('Id'))
print(container)
print(response)
print(container.logs())
print(cli.version())