本文整理汇总了Python中docker.client.Client.create_container方法的典型用法代码示例。如果您正苦于以下问题:Python Client.create_container方法的具体用法?Python Client.create_container怎么用?Python Client.create_container使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.client.Client
的用法示例。
在下文中一共展示了Client.create_container方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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: __init__
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [as 别名]
class Docker:
dockerconf = KOOPLEX.get('docker', {})
def __init__(self):
base_url = self.dockerconf.get('base_url', '')
self.client = Client(base_url = base_url)
logger.debug("Client init")
self.check = None
def list_imagenames(self):
logger.debug("Listing image names")
pattern_imagenamefilter = KOOPLEX.get('docker', {}).get('pattern_imagename_filter', r'^image-%(\w+):\w$')
for image in self.client.images(all = True):
if image['RepoTags'] is None:
continue
for tag in image['RepoTags']:
if re.match(pattern_imagenamefilter, tag):
_, imagename, _ = re.split(pattern_imagenamefilter, tag)
logger.debug("Found image: %s" % imagename)
yield imagename
def list_volumenames(self):
logger.debug("Listing volume names")
volumes = self.client.volumes()
for volume in volumes['Volumes']:
yield volume['Name']
def get_container(self, container):
for item in self.client.containers(all = True):
# docker API prepends '/' in front of container names
if '/' + container.name in item['Names']:
logger.debug("Get container %s" % container.name)
return item
return None
def create_container(self, container):
volumes = [] # the list of mount points in the container
binds = {} # a mapping dictionary of the container mounts
for volume in container.volumes:
logger.debug("container %s, volume %s" % (container, volume))
mp = volume.mountpoint
volumes.append(mp)
binds[volume.name] = { 'bind': mp, 'mode': volume.mode(container.user) }
logger.debug("container %s binds %s" % (container, binds))
host_config = self.client.create_host_config(
binds = binds,
privileged = True,
mem_limit = '2g',
memswap_limit = '170m',
mem_swappiness = 0,
# oom_kill_disable = True,
cpu_shares = 2,
)
network = self.dockerconf.get('network', 'host')
networking_config = { 'EndpointsConfig': { network: {} } }
ports = self.dockerconf.get('container_ports', [ 8000, 9000 ])
imagename = container.image.imagename if container.image else self.dockerconf.get('default_image', 'basic')
args = {
'name': container.name,
'image': imagename,
'detach': True,
'hostname': container.name,
'host_config': host_config,
'networking_config': networking_config,
'environment': container.environment,
'volumes': volumes,
'ports': ports,
}
self.client.create_container(**args)
logger.debug("Container created")
self.managemount(container) #FIXME: check if not called twice
return self.get_container(container)
def _writefile(self, container_name, path, filename, content):
import tarfile
import time
from io import BytesIO
tarstream = BytesIO()
tar = tarfile.TarFile(fileobj = tarstream, mode = 'w')
tarinfo = tarfile.TarInfo(name = filename)
tarinfo.size = len(content)
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
#.........这里部分代码省略.........
示例3: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [as 别名]
#.........这里部分代码省略.........
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:
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,
)
short_id = None
try:
id_ = container.get('Id')
short_id = id_[:12]
res = self.cli.start(container=id_,
port_bindings={self.VNC_PORT: None, self.CMD_PORT: None},
volumes_from=['netcapsule_shared_data_1'],
network_mode='netcapsule',
)
info = self.cli.inspect_container(id_)
ip = info['NetworkSettings']['IPAddress']
if not ip:
ip = info['NetworkSettings']['Networks']['netcapsule']['IPAddress']
#self.redis.hset('all_containers', short_id, ip)
self.redis.incr('num_containers')
self.redis.setex('c:' + short_id, self.C_EXPIRE_TIME, 1)
return {'vnc_host': self._get_host_port(info, self.VNC_PORT, default_host),
'cmd_host': self._get_host_port(info, self.CMD_PORT, default_host),
}
except Exception as e:
if short_id:
self.remove_container(short_id)
traceback.print_exc(e)
return {}
示例4: Client
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [as 别名]
#!/usr/bin/env python
import dockerpty
import os
from docker.client import Client
from docker.utils import kwargs_from_env
client = Client(base_url=os.environ['DOCKER_HOST'], version=os.environ['DOCKER_SERVER_API_VERSION'])
container = client.create_container(image="debian:wheezy", stdin_open=True, tty=True, command='echo "passed"')
dockerpty.start(client, container)
示例5: DockerPyClient
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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 = {}
#.........这里部分代码省略.........
示例6: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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')
#.........这里部分代码省略.........
示例7: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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
#.........这里部分代码省略.........
示例8: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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_,
#.........这里部分代码省略.........
示例9: DockerController
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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)
示例10: _run_build
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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()
示例11: Client
# 需要导入模块: from docker.client import Client [as 别名]
# 或者: from docker.client.Client import create_container [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())