本文整理汇总了Python中docker.from_env方法的典型用法代码示例。如果您正苦于以下问题:Python docker.from_env方法的具体用法?Python docker.from_env怎么用?Python docker.from_env使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker
的用法示例。
在下文中一共展示了docker.from_env方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_creates_service
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def test_creates_service(hub_service):
"""Test that logging in as a new user creates a new docker service."""
client = docker.from_env()
services_before_login = client.services.list()
# This request should create a new docker service to run the server for a-new-user
response = requests.post("http://127.0.0.1:8000/hub/login?next=", data={"username": "a-new-user", "password": "just magnets"})
assert response.status_code == 200
services_after_login = client.services.list()
assert len(services_after_login) - len(services_before_login) == 1
# Remove the service we just created, or we'll get errors when tearing down the fixtures
(set(services_after_login) - set(services_before_login)).pop().remove()
示例2: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def __init__(self, docker_config):
self.log_level = os.getenv('PYWREN_LOGLEVEL')
self.config = docker_config
self.name = 'docker'
self.host = docker_config['host']
self.queue = multiprocessing.Queue()
self.docker_client = None
self._is_localhost = self.host in ['127.0.0.1', 'localhost']
if self._is_localhost:
try:
self.docker_client = docker.from_env()
except Exception:
pass
log_msg = 'PyWren v{} init for Docker - Host: {}'.format(__version__, self.host)
logger.info(log_msg)
if not self.log_level:
print(log_msg)
示例3: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def __init__(self):
""" Connnects to the docker daemon"""
# will be used as the tag on the docker image
self.problem_name = sanitize_name(self.name)
# use an explicit remote docker daemon per the configuration
try:
tls_config = docker.tls.TLSConfig(
ca_cert=self.docker_ca_cert,
client_cert=(self.docker_client_cert, self.docker_client_key),
verify=True)
self.client = docker.DockerClient(base_url=self.docker_host, tls=tls_config)
self.api_client = docker.APIClient(base_url=self.docker_host, tls=tls_config)
logger.debug("Connecting to docker daemon with config")
# Docker options not set in configuration so use the environment to
# configure (could be local or remote)
except AttributeError:
logger.debug("Connecting to docker daemon with env")
self.client = docker.from_env()
# throws an exception if the server returns an error: docker.errors.APIError
self.client.ping()
示例4: _start_bitcoind
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def _start_bitcoind(self, cleanup_at_exit):
bitcoind_path = self.construct_bitcoind_cmd(self.rpcconn )
dclient = docker.from_env()
logger.debug("Running (in docker): {}".format(bitcoind_path))
ports={
'{}/tcp'.format(self.rpcconn.rpcport-1): self.rpcconn.rpcport-1,
'{}/tcp'.format(self.rpcconn.rpcport): self.rpcconn.rpcport
}
logger.debug("portmapping: {}".format(ports))
image = dclient.images.get("registry.gitlab.com/cryptoadvance/specter-desktop/python-bitcoind:{}".format(self.docker_tag))
self.btcd_container = dclient.containers.run("registry.gitlab.com/cryptoadvance/specter-desktop/python-bitcoind:{}".format(self.docker_tag), bitcoind_path, ports=ports, detach=True)
def cleanup_docker_bitcoind():
self.btcd_container.stop()
self.btcd_container.remove()
if cleanup_at_exit:
atexit.register(cleanup_docker_bitcoind)
logger.debug("Waiting for container {} to come up".format(self.btcd_container.id))
self.wait_for_container()
rpcconn, _ = self.detect_bitcoind_container(self.rpcconn.rpcport)
if rpcconn == None:
raise Exception("Couldn't find container or it died already. Check the logs!")
else:
self.rpcconn = rpcconn
return
示例5: __init__
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def __init__(self, toxinidir, distdir):
"""
:param toxinidir: directory containing tox.ini
:type toxinidir: str
:param distdir: tox dist directory
:type distdir: str
"""
self._toxinidir = toxinidir
self._distdir = distdir
self._gitdir = os.path.join(self._toxinidir, '.git')
logger.debug('Initializing DockerImageBuilder; toxinidir=%s gitdir=%s '
'distdir=%s',
self._toxinidir, self._gitdir, self._distdir)
if not os.path.exists(self._gitdir) or not os.path.isdir(self._gitdir):
raise RuntimeError(
'Error: %s does not exist or is not a directory' % self._gitdir
)
logger.debug('Connecting to Docker')
self._docker = docker.from_env()
示例6: docker_memory_limit
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def docker_memory_limit():
docker_client = docker.from_env()
# Get memory limit from docker container through the API
# Because the docker execution may be remote
cmd = f'python3 -u -c "import os; print(int(os.sysconf("SC_PAGE_SIZE") '\
f'* os.sysconf("SC_PHYS_PAGES") / (1024. ** 2)) // {CELERY_WORKER_CONCURRENCY}), flush=True, end=\'\')"'
task_args = {
'image': CELERYWORKER_IMAGE,
'command': cmd,
'detach': False,
'stdout': True,
'stderr': True,
'auto_remove': False,
'remove': True,
'network_disabled': True,
'network_mode': 'none',
'privileged': False,
'cap_drop': ['ALL'],
}
memory_limit_bytes = docker_client.containers.run(**task_args)
return int(memory_limit_bytes)
示例7: remove_local_folders
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def remove_local_folders(compute_plan_id):
if not settings.ENABLE_REMOVE_LOCAL_CP_FOLDERS:
logger.info(f'Skipping remove local volume of compute plan {compute_plan_id}')
return
logger.info(f'Remove local volume of compute plan {compute_plan_id}')
client = docker.from_env()
volume_id = get_volume_id(compute_plan_id)
try:
local_volume = client.volumes.get(volume_id=volume_id)
local_volume.remove(force=True)
except docker.errors.NotFound:
pass
except Exception:
logger.error(f'Cannot remove volume {volume_id}', exc_info=True)
if settings.TASK['CHAINKEYS_ENABLED']:
chainkeys_directory = get_chainkeys_directory(compute_plan_id)
try:
shutil.rmtree(chainkeys_directory)
except Exception:
logger.error(f'Cannot remove volume {chainkeys_directory}', exc_info=True)
示例8: test_exception_handler
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def test_exception_handler(self):
# Python exception in system
try:
1 / 0
except Exception as e:
error_code = compute_error_code(e)
value_error_code, _ = get_exception_code(ZeroDivisionError)
self.assertIn(f'00-01-{value_error_code}', error_code)
# Python exception in docker
try:
client = docker.from_env()
client.containers.run("python:3.6", ['python3', '-c', 'print(KO)'], remove=True)
except Exception as e:
error_code = compute_error_code(e)
container_error_code, _ = get_exception_code(NameError)
self.assertIn(f'01-01-{container_error_code}', error_code)
示例9: removeServices
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def removeServices(serviceid):
logging.info('Remove the service %s', serviceid)
try:
docker_client = docker.from_env()
docker_remove = docker_client.services.get(serviceid)
docker_remove.remove()
remove_ser = models.Service.query.all()
for i in remove_ser:
if (i.serviceid == serviceid):
db.session.delete(i)
db.session.commit()
break
except docker.errors.APIError as e:
if e.status_code == 404:
remove_ser = models.Service.query.all()
for i in remove_ser:
if (i.serviceid == serviceid):
db.session.delete(i)
db.session.commit()
break
else:
logging.error('Unable to remove the service %s. \nReason: %s', serviceid, str(e))
示例10: deleteImage
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def deleteImage(image_name):
logging.info('Delete the image %s', image_name)
try:
docker_client = docker.from_env()
registry_imagename = registry + '/' + image_name
docker_client.images.remove(image=registry_imagename,force=True)
image = models.Image.query.filter_by(imagename=image_name).first()
db.session.delete(image)
db.session.commit()
except docker.errors.APIError as e:
image = models.Image.query.filter_by(imagename = image_name).first()
db.session.delete(image)
db.session.commit()
error_string = 'Unable to delete the image {}. \nReason: {}. Delete the record'.format(registry_imagename, str(e))
logging.error(error_string)
return error_string
return None
示例11: setUpClass
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def setUpClass(cls):
print(' \n >>>> ==== Running Test 2 - Verify Mounted Volume')
print(' >>>> ==== using IMAGE = ' + cls.docker_image + ' ===== ')
DockerUtil.empty_test_folder("tmp")
# Setup test dir as volume to by mounted by container
appdata = DockerUtil.create_test_dir("tmp/appdata")
exts = DockerUtil.create_test_dir("tmp/extensions")
mount={}
mount[appdata]= { 'bind':'/opt/connect/appdata', 'mode':'rw'}
mount[exts]= {'bind':'/opt/connect/custom-extensions','mode':'ro'}
# run docker image with -v option
client = docker.from_env()
cls.container = client.containers.run(cls.docker_image,volumes=mount,detach=True,name="mctest2")
# wait for MC to come up
try:
DockerUtil.wait_for_containers([cls.container], 60)
except Exception, e:
print(">>>> MC server failed to start")
cls.tearDownClass()
raise e
示例12: setUpClass
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def setUpClass(cls):
# run docker image with 2 environment variables
print(' \n >>>> ==== Running Test 1 - Verify Environment Variables')
print(' >>>> ==== using IMAGE = ' + cls.docker_image + ' ===== ')
client = docker.from_env()
cls.container = client.containers.run(cls.docker_image,
environment=[
"SESSION_STORE=true",
"VMOPTIONS=-Xmx768m"
],
detach=True,
name="mctest1")
# wait for MC to come up
try:
DockerUtil.wait_for_containers([cls.container], 60)
except Exception, e:
print(">>>> MC server failed to start")
cls.tearDownClass()
raise e
# retrieve container mirth.properties file as a map
示例13: setUpClass
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def setUpClass(cls):
print(' \n >>>> ==== Running Test 3 - Verify Compose with secret, postgres, custom-extensions')
print( ' >>>> ==== using IMAGE = ' + cls.docker_image + ' ===== ')
DockerUtil.empty_test_folder("tmp")
# Setup test dir as volume to by mounted by container
exts = DockerUtil.create_test_dir("tmp/exts")
os.system('cp ./testdata/*.zip ./tmp/exts/')
os.system('cp ./testdata/secret.properties ./tmp/')
DockerUtil.generate_compose_yml('./tmp/test.yml',cls.docker_image)
# Run docker compose
os.system(cls.composeCmd + " up -d")
client = docker.from_env()
cls.container = client.containers.get("mctest3_mc_1")
# wait for MC to come up
try:
DockerUtil.wait_for_containers([cls.container], 60)
except Exception, e:
print(">>>> MC server failed to start")
cls.tearDownClass()
raise e
# retrieve container mirth.properties file as a map
示例14: test_local_docker_conf
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def test_local_docker_conf(self):
""" Test to connect to a local Docker daemon """
try:
docker_connection = docker.from_env()
except Exception as e:
self._display_error("- Unable to connect to Docker. Error was %s" % str(e))
return False
try:
self._display_info("- Asking Docker some info")
if docker.utils.compare_version('1.24', docker_connection.version()['ApiVersion']) < 0:
self._display_error("- Docker version >= 1.12.0 is required.")
return False
except Exception as e:
self._display_error("- Unable to contact Docker. Error was %s" % str(e))
return False
self._display_info("- Successfully got info from Docker. Docker connection works.")
return True
示例15: download_containers
# 需要导入模块: import docker [as 别名]
# 或者: from docker import from_env [as 别名]
def download_containers(self, to_download, current_options):
""" Download the chosen containers on all the agents """
if current_options["backend"] == "local":
self._display_info("Connecting to the local Docker daemon...")
try:
docker_connection = docker.from_env()
except:
self._display_error("Cannot connect to local Docker daemon. Skipping download.")
return
for image in to_download:
try:
self._display_info("Downloading image %s. This can take some time." % image)
docker_connection.images.pull(image + ":latest")
except Exception as e:
self._display_error("An error occurred while pulling the image: %s." % str(e))
else:
self._display_warning(
"This installation tool does not support the backend configuration directly, if it's not local. You will have to "
"pull the images by yourself. Here is the list: %s" % str(to_download))