Python docker 模块,from_env() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用docker.from_env()

项目:girder_worker    作者:girder    | 项目源码 | 文件源码
def _run_container(image, container_args,  **kwargs):
    # TODO we could allow configuration of non default socket
    client = docker.from_env(version='auto')
    if nvidia.is_nvidia_image(client.api, image):
        client = nvidia.NvidiaDockerClient.from_env(version='auto')

    logger.info('Running container: image: %s args: %s kwargs: %s'
                % (image, container_args, kwargs))
    try:
        return client.containers.run(image, container_args, **kwargs)
    except nvidia.NvidiaConnectionError:
        try:
            logger.info('Running nvidia container without nvidia support: image: %s' % image)
            client = docker.from_env(version='auto')
            return client.containers.run(image, container_args, **kwargs)
        except DockerException:
            logger.exception('Exception when running docker container without nvidia support.')
            raise
    except DockerException:
        logger.exception('Exception when running docker container')
        raise
项目:Cloudroid    作者:cyberdb    | 项目源码 | 文件源码
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))
项目:Cloudroid    作者:cyberdb    | 项目源码 | 文件源码
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
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def test_stop_r():
    """ tests the restful endpoint: stop """
    # get web app
    test_app = start_web_app()

    # create some container and start it
    d = docker.from_env()
    d.images.pull('alpine')
    test_cont = d.containers.create('alpine')

    # test stop
    r = test_app.post('/stop', params={})
    assert r.status == 200
    r = test_app.post('/stop', params={'id': test_cont.attrs['Id']})
    assert r.status == 200
    r = test_app.post('/stop', params={'id': []})
    assert r.status == 200
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def test_start_r():
    """ tests the restful endpoint: start """
    # get web app
    test_app = start_web_app()

    # create some container
    d = docker.from_env()
    d.images.pull('alpine')
    test_cont = d.containers.create('alpine')

    # test start
    r = test_app.post('/start', params={})
    assert r.status == 200
    r = test_app.post('/start', params={'id': test_cont.attrs['Id']})
    assert r.status == 200
    r = test_app.post('/start', params={'id': []})
    assert r.status == 200
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def test_delete_r():
    """ tests the restful endpoint: delete """
    # get web app
    test_app = start_web_app()

    # create some container and start it
    d = docker.from_env()
    d.images.pull('alpine')
    test_cont = d.containers.create('alpine')

    # test delete
    r = test_app.post('/delete', params={})
    assert r.status == 200
    r = test_app.post('/delete', params={'id': test_cont.attrs['Id']})
    assert r.status == 200
    r = test_app.post('/delete', params={'id': []})
    assert r.status == 200
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def GET():
        web.header('Content-Type', 'text/html')

        # connect to docker
        try:
            d_client = docker.from_env()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to connect to docker because: ' + str(e))

        # start container to get network interfaces
        nics = ""
        try:
            nics = d_client.containers.run('cyberreboot/gonet',
                                           network_mode='host')
        except Exception as e:  # pragma: no cover
            return (False, "Failure: " + str(e))

        return (True, nics)
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def Images(vent=True):
    """ Get images that are build, by default limit to vent images """
    images = []
    # TODO needs to also check images in the manifest that couldn't have the
    #      label added
    try:
        d_client = docker.from_env()
        if vent:
            i = d_client.images.list(filters={'label': 'vent'})
        else:
            i = d_client.images.list()
        for image in i:
            images.append((image.tags[0], image.short_id))
    except Exception as e:  # pragma: no cover
        logger.error("Something with the Images went wrong " + str(e))

    return images
项目:zimfarm    作者:openzim    | 项目源码 | 文件源码
def run(self, token: str, config: {}):
        self.token = token
        client = docker.from_env()

        self.start_time = datetime.utcnow()
        self.status = 'PREPARING'
        self.put_status()

        self.run_redis(client)
        self.pull_mwoffliner(client)

        self.status = 'GENERATING'
        self.put_status()
        self.generate(client, config)

        # self.status = 'UPLOADING'
        # self.put_status()
        # self.upload_file()
        self.status = 'FINISHED'
        self.ended_time = datetime.utcnow()
        self.put_status()
项目:SwarmSpawner    作者:cassinyio    | 项目源码 | 文件源码
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()
项目:Codado    作者:corydodt    | 项目源码 | 文件源码
def run(self):
        """
        Connect to the docker engine and begin listening for docker events
        """
        self.client = docker.from_env()

        now = time.time()
        nowNano = now * 1000000000
        startEvent = Event(status='init',
                id=None,
                time=int(now),
                timeNano=int(nowNano),
                actor=EventActor(image=None, name=None, signal=None, id=None),
                action='init',
                eventFrom=None,
                eventType='dockerish',
                engine=self,
                )
        self.callLater(0, self._callHandlers, 'dockerish.init', startEvent)
        self.callLater(PEEK_INTERVAL_SECONDS, self._genEvents, time.time())
项目:integration-prototype    作者:SKA-ScienceDataProcessor    | 项目源码 | 文件源码
def __init__(self):
        """ Constructor
        """
        Paas.__init__(self)

        log = logging.getLogger(__name__ + '.' + self.__class__.__name__)

        # Create a docker client
        self._client = docker.from_env()

        # Create the SIP overlay network if it does not exist.
        if not self._client.networks.list(names=['sip']):
            log.info('Creating sip overlay network.')
            self._client.networks.create('sip', driver='overlay')

        # Store a flag to show whether we are on a manager node or a worker.
        self._manager = self._client.info()['Swarm']['ControlAvailable']
项目:integration-prototype    作者:SKA-ScienceDataProcessor    | 项目源码 | 文件源码
def setUpClass(cls):
        """ Initialise the test class.
        """
        warnings.simplefilter('ignore', ResourceWarning)

        # Get a client to the local docker engine.
        client = docker.from_env()

        # Skip the tests in this class if not running from a manager node.
        if not client.info()['Swarm']['ControlAvailable']:
            raise unittest.SkipTest('This test must be run from a swarm '
                                    'manager node.')
            # client.swarm.init()

        # Create a logging server
        # FIXME(BM): This should not be needed to test heartbeat!
        paas = Paas()
        cls.logger = paas.run_service(
            'logging_server',
            'sip',
            [logging.handlers.DEFAULT_TCP_LOGGING_PORT],
            ['python3', 'sip/common/logging_server.py'])

        # Wait for the logging server to come online.
        time.sleep(3)
项目:integration-prototype    作者:SKA-ScienceDataProcessor    | 项目源码 | 文件源码
def repeat(times):
    """ Decorator that can be used to repeat a test.
    """
    # pylint: disable=missing-docstring
    def repeat_helper(method):
        def call_helper(*args):
            for i in range(times):
                # If running more than once reinitialise the swarm between
                # tests as this won't be done by setUp / tearDown.
                if i > 0:
                    client = docker.from_env()
                    client.swarm.leave(force=True)
                    client.networks.prune()
                    client.swarm.init()
                method(*args)
        return call_helper
    return repeat_helper
项目:slicer_cli_web    作者:girder    | 项目源码 | 文件源码
def testDockerDeleteFull(self):
        # attempt to delete docker image metadata and the image off the local
        # machine
        img_name = 'girder/slicer_cli_web:small'
        self.assertNoImages()
        self.addImage(img_name, JobStatus.SUCCESS)
        self.imageIsLoaded(img_name, True)
        self.deleteImage(img_name, True, True, JobStatus.SUCCESS)

        try:
            docker_client = docker.from_env(version='auto')
        except Exception as err:
            self.fail('could not create the docker client ' + str(err))

        try:
            docker_client.images.get(img_name)
            self.fail('If the image was deleted then an attempt to get it '
                      'should raise a docker exception')
        except Exception:
            pass

        self.imageIsLoaded(img_name, exists=False)
        self.assertNoImages()
项目:malmo-challenge    作者:Kaixhin    | 项目源码 | 文件源码
def __init__(self, rank):
    docker_client = docker.from_env()
    agent_port, partner_port = 10000 + rank, 20000 + rank
    clients = [('127.0.0.1', agent_port), ('127.0.0.1', partner_port)]
    self.agent_type = GlobalVar()

    # Assume Minecraft launched if port has listener, launch otherwise
    if not _port_has_listener(agent_port):
      self._launch_malmo(docker_client, agent_port)
    print('Malmo running on port ' + str(agent_port))
    if not _port_has_listener(partner_port):
      self._launch_malmo(docker_client, partner_port)
    print('Malmo running on port ' + str(partner_port))

    # Set up partner agent env in separate process
    p = mp.Process(target=self._run_partner, args=(clients, ))
    p.daemon = True
    p.start()
    time.sleep(3)

    # Set up agent env
    self.env = PigChaseEnvironment(clients, PigChaseTopDownStateBuilder(gray=False), role=1, randomize_positions=True)
项目:biweeklybudget    作者:jantman    | 项目源码 | 文件源码
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()
项目:ChRIS_ultron_backEnd    作者:FNNDSC    | 项目源码 | 文件源码
def get_plugin_app_representation(self, dock_image_name):
        """
        Get a plugin app representation given its docker image name.
        """
        client = docker.from_env()
        # first try to pull the latest image
        try:
            img = client.images.pull(dock_image_name)
        except docker.errors.APIError:
            # use local image ('remove' option automatically removes container when finished)
            byte_str = client.containers.run(dock_image_name, remove=True)
        else:
            byte_str = client.containers.run(img, remove=True)
        app_repr = json.loads(byte_str.decode())
        plugin_types = [plg_type[0] for plg_type in PLUGIN_TYPE_CHOICES]
        if app_repr['type'] not in plugin_types:
            raise ValueError("A plugin's TYPE can only be any of %s. Please fix it in %s"
                             % (plugin_types, dock_image_name))
        return app_repr
项目:dcos-e2e    作者:mesosphere    | 项目源码 | 文件源码
def _nodes(self, container_base_name: str) -> Set[Node]:
        """
        Args:
            container_base_name: The start of the container names.

        Returns: ``Node``s corresponding to containers with names starting
            with ``container_base_name``.
        """
        client = docker.from_env(version='auto')
        filters = {'name': container_base_name}
        containers = client.containers.list(filters=filters)

        return set(
            Node(
                ip_address=IPv4Address(
                    container.attrs['NetworkSettings']['IPAddress']
                ),
                ssh_key_path=self._path / 'include' / 'ssh' / 'id_rsa',
            ) for container in containers
        )
项目:do    作者:rapydo    | 项目源码 | 文件源码
def is_daemon_alive(self):

        if self.client is None:
            self.client = docker.from_env()
        else:
            pass

        # from requests.packages.urllib3 import exceptions as reqex
        # try:
        #     self.client.containers.list()
        # except (FileNotFoundError, reqex.ProtocolError, ConnectionError):
        #     return False
        # else:
        #     return True

        try:
            return self.client.ping()
        # this is the case of docker daemon not started
        except requests.exceptions.ConnectionError:
            return False
        # this is the case of docker daemon starting or not working properly
        except docker_errors:
            return False
项目:docker-service-registrator-kong    作者:mvanholsteijn    | 项目源码 | 文件源码
def __init__(self, admin_url, dns_name, hostname, verify_ssl):
        """
        constructor.
        """
        assert dns_name is not None
        assert hostname is not None
        assert admin_url is not None

        self.dockr = docker.from_env()
        self.hostname = hostname
        self.dns_name = dns_name
        self.admin_url = admin_url
        self.verify_ssl = verify_ssl
        self.upstreams = {}
        self.targets = {}
        self.apis = {}

        self.load()

        assert self.hostname == hostname
        assert self.dns_name == dns_name
项目:son-cli    作者:sonata-nfv    | 项目源码 | 文件源码
def _config_prometheus(remove=False):
        global prometheus_server_api
        global prometheus_config_path
        docker_cli = docker.from_env()
        # check if containers are already running
        c1 = docker_cli.containers.list(filters={'status': 'running', 'name': 'prometheus'})
        if len(c1) < 1:
            LOG.info('Prometheus is not running')
            return "Prometheus DB is not running"
        # make Prometheus scrape this server
        config_file = read_yaml(prometheus_config_path)
        targets = config_file.get('scrape_configs', [])
        SP_stream_config = next((target for target in targets if target.get('job_name') == 'SP_stream'), None)
        # the SP http server is not yet added to the config file
        config_dict = {'job_name': 'SP_stream', 'scrape_interval': '1s',
                       'static_configs': [{'targets': ['172.17.0.1:{}'.format(prometheus_stream_port)]}]}
        if not SP_stream_config and not remove:
            config_file['scrape_configs'].append(config_dict)
            LOG.info('added SP stream to Prometheus')
        elif remove and SP_stream_config:
            config_file['scrape_configs'].remove(config_dict)
            LOG.info('removed SP stream from Prometheus')

        write_yaml(prometheus_config_path, config_file)
        post(prometheus_server_api + '/-/reload')
项目:lain-sdk    作者:laincloud    | 项目源码 | 文件源码
def get_tag_list_in_docker_daemon(registry, appname):
    tag_list = []
    c = docker.from_env()
    imgs = c.images.list()
    for img in imgs:
        repo_tags = img.tags
        if not repo_tags:
            continue
        for repo_tag in repo_tags:
            try:
                s_list = repo_tag.split(":")
                tag = s_list[-1]
                repo = ":".join(s_list[:-1])
                if repo == "%s/%s" % (registry, appname) and tag not in tag_list:
                    tag_list.append(tag)
            except Exception as e:
                print(e)
    return tag_list
项目:simphony-remote    作者:simphony    | 项目源码 | 文件源码
def get_docker_client():
    """ Returns docker.client object using the local environment variables
    """
    # dependencies of docker-py is optional for this script
    try:
        import docker
    except ImportError:
        print_error('docker-py is not installed. '
                    'Try pip install docker-py')
        raise

    client = docker.from_env()
    try:
        client.info()
    except ConnectionError as exception:
        # ConnectionError occurs, say, if the docker machine is not running
        # or if the shell is not in a docker VM (for Mac/Windows)
        print_error('docker client fails to connect.')
        raise

    return client
项目:docker-tcp-switchboard    作者:OverTheWireOrg    | 项目源码 | 文件源码
def start(self):
        # get docker client
        client = docker.from_env()

        # start instance
        try:
            logger.debug("Starting instance {} of container {} with dockeroptions {}".format(self.getProfileName(), self.getContainerName(), pprint.pformat(self.getDockerOptions())))
            clientres = client.containers.run(self.getContainerName(), **self.getDockerOptions())
            self._instance = client.containers.get(clientres.id)
            logger.debug("Done starting instance {} of container {}".format(self.getProfileName(), self.getContainerName()))
        except Exception as e:
            logger.debug("Failed to start instance {} of container {}: {}".format(self.getProfileName(), self.getContainerName(), e))
            self.stop()
            return False

        # wait until container's checkupport is available
        logger.debug("Started instance on middleport {} with ID {}".format(self.getMiddlePort(), self.getInstanceID()))
        if self.__waitForOpenPort(self.getMiddleCheckupPort()):
            logger.debug("Started instance on middleport {} with ID {} has open port {}".format(self.getMiddlePort(), self.getInstanceID(), self.getMiddleCheckupPort()))
            return True
        else:
            logger.debug("Started instance on middleport {} with ID {} has closed port {}".format(self.getMiddlePort(), self.getInstanceID(), self.getMiddleCheckupPort()))
            self.stop()
            return False
项目:lcci-python    作者:librecores    | 项目源码 | 文件源码
def install(self, version):
        logging.info("Install {}:{}".format(self.name, version))
        client = docker.from_env(version='auto')
        logging.info("Pull the image")

        image = "lccitools/{}:{}".format(self.name, version)

        try:
            client.images.pull(image)
        except docker.errors.ImageNotFound:
            logging.error("Cannot find image {}".format(image))
            exit(1)

        logging.info("Start the installation")

        client.containers.run(
            image,
            detach = False,
            volumes = { self.basepath: { "bind": "/tools", "mode": "rw"} }
        )

        logging.info("Done")
项目:lcci-python    作者:librecores    | 项目源码 | 文件源码
def stop(self):
        logging.info("Stop agent {}".format(self.name))

        client = docker.from_env(version='auto')

        try:
            cont = client.containers.get("lcci-{}".format(self.name))
        except:
            logging.error("Cannot find container")
            exit(1)

        try:
            cont.stop()
        except docker.errors.APIError as e:
            if (e.status_code() == 137):
                logging.error("Container cannot be stopped" + e)
                exit(1)
        except requests.exceptions.ReadTimeout:
            cont.kill()

        try:
            cont.remove()
        except docker.errors.APIError as e:
            logging.error("Container cannot be removed")
            exit(1)
项目:pydocktors    作者:Patouche    | 项目源码 | 文件源码
def start(self):
        """
        Start a containers and wait for it.
        """
        self._client = docker.from_env()

        image = self.p('image')
        logger.debug('[%s] image is starting ...', image)

        self._container = self._client.containers.run(
            image=image,
            detach=True,
            command=self.p('command'),
            volumes=self.p('volumes'),
            ports=self.p('ports'),
            environment=self.p('environment'),
        )

        logger.debug('[%s] container start with id : %s', image, self._container.id)
        self._wait_for_log()
        self._wait_for_port()
        logger.debug('[%s] reloading container %s', image, self._container.id)
        self._container.reload()
        logger.debug('[%s] container is ready (id=%s)', image, self._container.id)
        return self._container
项目:neurodocker    作者:kaczmarj    | 项目源码 | 文件源码
def docker_is_running(client):
    """Return true if Docker server is responsive.

    Parameters
    ----------
    client : docker.client.DockerClient
        The Docker client. E.g., `client = docker.from_env()`.

    Returns
    -------
    running : bool
        True if Docker server is responsive.
    """
    try:
        client.ping()
        return True
    except (requests.exceptions.ConnectionError, docker.errors.APIError):
        return False
项目:SoS    作者:vatlab    | 项目源码 | 文件源码
def __init__(self):
        try:
            self.client = docker.from_env()
            self.client.info()
            # mount the /Volumes folder under mac, please refer to
            #    http://vatlab.github.io/SOS/doc/tutorials/SoS_Docker_Guide.html
            # for details.
            self.has_volumes = False
            if platform.system() == 'Darwin':
                try:
                    # this command log in to the docker machine, check if /Volumes has been mounted,
                    # and try to mount it if possible. This requires users to configure
                    subprocess.call("""docker-machine ssh "{}" 'mount | grep /Volumes || {{ echo "mounting /Volumes"; sudo mount  -t vboxsf Volumes /Volumes; }}' """.format(os.environ['DOCKER_MACHINE_NAME']),
                        shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
                    env.logger.trace('Sucessfully mount /Volumes to virtual machine')
                    self.has_volumes = True
                except Exception as e:
                    env.logger.trace('Failed to mount /Volumes to virtual machine: {}'.format(e))
        except Exception as e:
            env.logger.debug('Docker client init fail: {}'.format(e))
            self.client = None
项目:python-hydra-sdk    作者:OSSystems    | 项目源码 | 文件源码
def hydra_fixture():
    if os.environ.get('PYTHON_HYDRA_LOCAL'):
        yield
        return
    client = docker.from_env()
    env = {
        'FORCE_ROOT_CLIENT_CREDENTIALS': 'client:secret',
        'DATABASE_URL': 'memory',
    }
    ports = {'4444/tcp': 4444}
    entrypoint = '/go/bin/hydra host --dangerous-force-http'
    container = client.containers.run(
        'oryd/hydra',
        detach=True,
        environment=env,
        ports=ports,
        entrypoint=entrypoint,
    )
    for line in container.logs(stream=True):
        if b'Setting up http server on :4444' in line:
            break
    yield
    container.kill()
    container.remove()
项目:pocketinternet    作者:inognet    | 项目源码 | 文件源码
def create_backbone_network(name="backbone", v4_subnet="172.16.0.0/12", v6_subnet="fd00:bb::/48"):
    docker_client = docker.from_env()

    # Does the backbone network already exist?
    print("[BB] Checking for existing Backbone network")
    existing_backbone = docker_client.networks.list(names=[name])

    existing_backbone = [bb for bb in existing_backbone if bb.name == name]

    if existing_backbone:
        print("[BB] Backbone network already exists.")
    else:
        print("[BB] Backbone does not exist, Creating Backbone")
        v4_pool = docker.types.IPAMPool(subnet=v4_subnet)
        v6_pool = docker.types.IPAMPool(subnet=v6_subnet)

        ipam_config = docker.types.IPAMConfig(
            pool_configs=[v4_pool, v6_pool]
        )
        try:
            docker_client.networks.create(
                name=name,
                enable_ipv6=True,
                ipam=ipam_config
            )

            print("[BB] Created Backbone")
        except docker.errors.APIError as apierror:
            print("\r\n[BB] -!-!- UNABLE TO CREATE BACKBONE -!-!-")
            print("[BB] Docker Encountered an Error: {}".format(apierror))
            print("[BB] You will need to run pocketinternet on a machine which has no Docker containers or networks outside of the defaults.")
            print("[BB] At this point, you should then run 'sudo ./pocketinternet configure-docker' before re-running setup")
            print("[BB] -!-!- UNABLE TO CREATE BACKBONE -!-!-")

            sys.exit(1)
项目:girder_worker    作者:girder    | 项目源码 | 文件源码
def chmod_writable(host_paths):
    """
    Since files written by docker containers are owned by root, we can't
    clean them up in the worker process since that typically doesn't run
    as root. So, we run a lightweight container to make the temp dir cleanable.
    """
    if not isinstance(host_paths, (list, tuple)):
        host_paths = (host_paths,)

    client = docker.from_env(version='auto')
    config = {
        'tty': True,
        'volumes': {},
        'detach': False,
        'remove': True
    }

    container_paths = []
    for host_path in host_paths:
        container_path = os.path.join(CONTAINER_PATH, uuid.uuid4().hex)
        container_paths.append(container_path)
        config['volumes'][host_path] = {
            'bind': container_path,
            'mode': 'rw'
        }

    args = ['chmod', '-R', 'a+rw'] + container_paths

    try:
        client.containers.run('busybox:latest', args, **config)
    except DockerException:
        logger.exception('Error setting perms on docker volumes %s.' % host_paths)
        raise
项目:girder_worker    作者:girder    | 项目源码 | 文件源码
def _pull_image(image):
    """
    Pulls the specified Docker image onto this worker.
    """
    client = docker.from_env(version='auto')
    try:
        client.images.pull(image)
    except DockerException:
        logger.exception('Error pulling Docker image %s:' % image)
        raise
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def test_run_containers():
    """ Run some containers for testing purposes """
    d_client = docker.from_env()
    d_client.containers.run("alpine:latest", "tail -f /etc/passwd", detach=True, labels=["vent", "vent-plugins"])
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def __init__(self, port=8080, host='0.0.0.0'):  # pragma: no cover
        d_client = docker.from_env()
        d_client.images.pull('cyberreboot/vent-ncapture', tag='master')
        nf_inst = NControl()
        urls = nf_inst.urls()
        app = web.application(urls, globals())
        web.httpserver.runsimple(app.wsgifunc(), (host, port))
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def POST():
        """
        Send a POST request with a docker container ID and it will be stopped.

        Example input: {'id': "12345"}, {'id': ["123", "456"]
        """
        web.header('Content-Type', 'application/json')

        # verify user input
        data = web.data()
        payload = {}
        try:
            payload = ast.literal_eval(data)
        except Exception as e:  # pragma: no cover
            return (False, 'malformed payload : ' + str(e))

        # verify payload has a container ID
        if 'id' not in payload:
            return (False, 'payload missing container id')

        # connect to docker and stop the given container
        c = None
        try:
            c = docker.from_env()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to connect to docker because: ' + str(e))

        # stop containers chosen from CLI
        try:
            for container_id in payload['id']:
                c.containers.get(container_id).stop()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to stop list of containers because: ' +
                    str(e))

        return (True, 'container successfully stopped: ' + str(payload['id']))
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def POST():
        """
        Send a POST request with a docker container ID and it will be deleted.

        Example input: {'id': "12345"}, {'id': ["123", "456"]}
        """
        web.header('Content-Type', 'application/json')

        # verify user input
        data = web.data()
        payload = {}
        try:
            payload = ast.literal_eval(data)
        except Exception as e:  # pragma: no cover
            return (False, 'malformed payload : ' + str(e))

        # verify payload has a container ID
        if 'id' not in payload:
            return (False, 'payload missing container id')

        # connect to docker and stop the given container
        c = None
        try:
            c = docker.from_env()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to connect to docker because: ' + str(e))

        # delete containers chosen from CLI
        try:
            for container_id in payload['id']:
                c.containers.get(container_id).remove()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to delete containers because: '
                    + str(e))

        return (True, 'container successfully deleted: ' + str(payload['id']))
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def POST():
        """
        Send a POST request with a docker container ID and it will be started.

        Example input: {'id': "12345"}, {'id': ["123", "456"]}
        """
        web.header('Content-Type', 'application/json')

        # verify user input
        data = web.data()
        payload = {}
        try:
            payload = ast.literal_eval(data)
        except Exception as e:  # pragma: no cover
            return (False, 'malformed payload : ' + str(e))

        # verify payload has a container ID
        if 'id' not in payload:
            return (False, 'payload missing container id')

        # connect to docker and stop the given container
        c = None
        try:
            c = docker.from_env()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to connect to docker because: ' + str(e))

        # start containers chosen from CLI
        try:
            for container_id in payload['id']:
                c.containers.get(container_id).start()
        except Exception as e:  # pragma: no cover
            return (False, 'unable to start list of containers because: ' +
                    str(e))

        return (True, 'container successfully started: ' + str(payload['id']))
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def Docker():
    """ Get Docker setup information """
    docker_info = {'server': {}, 'env': '', 'type': '', 'os': ''}

    # get docker server version
    try:
        d_client = docker.from_env()
        docker_info['server'] = d_client.version()
    except Exception as e:  # pragma: no cover
        logger.error("Can't get docker info " + str(e))

    # get operating system
    system = System()
    docker_info['os'] = system

    # check if native or using docker-machine
    if 'DOCKER_MACHINE_NAME' in os.environ:
        # using docker-machine
        docker_info['env'] = os.environ['DOCKER_MACHINE_NAME']
        docker_info['type'] = 'docker-machine'
    elif 'DOCKER_HOST' in os.environ:
        # not native
        docker_info['env'] = os.environ['DOCKER_HOST']
        docker_info['type'] = 'remote'
    else:
        # using "local" server
        docker_info['type'] = 'native'
    return docker_info
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def Gpu(pull=False):
    """ Check for support of GPUs, and return what's available """
    gpu = (False, "")

    try:
        image = 'nvidia/cuda:8.0-runtime'
        image_name, tag = image.split(":")
        d_client = docker.from_env()
        nvidia_image = d_client.images.list(name=image)

        if pull and len(nvidia_image) == 0:
            try:
                d_client.images.pull(image_name, tag=tag)
                nvidia_image = d_client.images.list(name=image)
            except Exception as e:  # pragma: no cover
                logger.error("Something with the GPU went wrong " + str(e))

        if len(nvidia_image) > 0:
            cmd = 'nvidia-docker run --rm ' + image + ' nvidia-smi -L'
            proc = Popen([cmd],
                         stdout=PIPE,
                         stderr=PIPE,
                         shell=True,
                         close_fds=True)
            gpus = proc.stdout.read()
            err = proc.stderr.read()
            if gpus:
                gpu_str = ""
                for line in gpus.strip().split("\n"):
                    gpu_str += line.split(" (UUID: ")[0] + ", "
                gpu = (True, gpu_str[:-2])
            else:
                if err:
                    gpu = (False, "Unknown", str(err))
                else:
                    gpu = (False, "None")
        else:
            gpu = (False, "None")
    except Exception as e:  # pragma: no cover
        gpu = (False, "Unknown", str(e))
    return gpu
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def __init__(self, **kargs):
        self.path_dirs = PathDirs(**kargs)
        self.manifest = join(self.path_dirs.meta_dir,
                             "plugin_manifest.cfg")
        self.p_helper = PluginHelper(**kargs)
        self.d_client = docker.from_env()
        self.logger = Logger(__name__)
        self.plugin_config_file = self.path_dirs.plugin_config_file
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def __init__(self, **kargs):
        self.d_client = docker.from_env()
        self.path_dirs = PathDirs(**kargs)
        self.manifest = join(self.path_dirs.meta_dir,
                             "plugin_manifest.cfg")
        self.logger = Logger(__name__)
项目:vent    作者:CyberReboot    | 项目源码 | 文件源码
def get_vent_tool_url(tool_name):
        """
        Iterate through all containers and grab the port number
        corresponding to the given tool name. Works for only CORE tools
        since it specifically looks for core

        Args:
            tool_name(str): tool name to search for. Eg: network-tap

        Returns:
            A tuple of success status and the url corresponding to the given
            tool name or a failure mesage. An example return url is
            http://0.0.0.0:37728. Works well with send_request and get_request.
        """
        try:
            d = docker.from_env()
            containers = d.containers.list(filters={'label': 'vent'}, all=True)
        except Exception as e:  # pragma no cover
            return (False, "docker failed with error " + str(e))

        url = ''
        found = False
        for c in containers:
            if tool_name in c.attrs['Name'] and \
                    'core' in c.attrs['Config']['Labels']['vent.groups']:
                # get a dictionary of ports
                url = c.attrs['NetworkSettings']['Ports']

                # iterate through the dict to avoid hard coding anything
                # is it safe to assume only 1 entry in the dict will exist?
                for port in url:
                    h_port = url[port][0]['HostPort']
                    h_ip = url[port][0]['HostIp']
                    url = "http://" + str(h_ip) + ":" + str(h_port)
                    found = True
                    break
            # no need to cycle every single container if we found our ports
            if found:
                break
        return (True, str(url))
项目:client-python    作者:bblfsh    | 项目源码 | 文件源码
def tearDownClass(cls):
        if not cls.BBLFSH_SERVER_EXISTED:
            client = docker.from_env(version="auto")
            client.containers.get("bblfshd").remove(force=True)
            client.api.close()
项目:son-mano-framework    作者:sonata-nfv    | 项目源码 | 文件源码
def connect(self):
        """
        Connect to a Docker service on which FSMs/SSMs shall be executed.
        The connection information for this service should be specified with the following
        environment variables (example for a docker machine installation):

            export DOCKER_TLS_VERIFY="1"
            export DOCKER_HOST="tcp://192.168.99.100:2376"
            export DOCKER_CERT_PATH="/Users/<user>/.docker/machine/machines/default"
            export DOCKER_MACHINE_NAME="default"

            Docker machine hint: eval $(docker-machine env default) sets all needed ENV variables.

        If DOCKER_HOST is not set, the default local Docker socket will be tried.
        :return: client object
        """
        # lets check if Docker ENV information is set and use local socket as fallback
        if os.environ.get("DOCKER_HOST") is None:
            os.environ["DOCKER_HOST"] = "unix://var/run/docker.sock"
            LOG.warning("ENV variable 'DOCKER_HOST' not set. Using {0} as fallback.".format(os.environ["DOCKER_HOST"]))

        # lets connect to the Docker instance specified in current ENV
        # cf.: http://docker-py.readthedocs.io/en/stable/machine/
        dc = docker.from_env(assert_hostname=False)
        # do a call to ensure that we are connected
        dc.info()
        LOG.info("Connected to Docker host: {0}".format(dc.base_url))
        return dc
项目:arxiv-vanity    作者:arxiv-vanity    | 项目源码 | 文件源码
def create_client():
    """
    Create a client to either a local Docker instance or Hyper.sh.
    """
    client = docker.from_env()
    if settings.ENGRAFO_USE_HYPER_SH:
        client.api = hyper_sh.Client({
            'clouds': {
                settings.HYPER_ENDPOINT: {
                    'accesskey': settings.HYPER_ACCESS_KEY,
                    'secretkey': settings.HYPER_SECRET_KEY,
                }
            }
        })
    return client
项目:SwarmSpawner    作者:cassinyio    | 项目源码 | 文件源码
def swarm():
    """Initialize the docker swarm that's going to run the servers
    as services.
    """
    client = docker.from_env()
    client.swarm.init(advertise_addr="192.168.99.100")
    yield client.swarm.attrs
    client.swarm.leave(force=True)
项目:SwarmSpawner    作者:cassinyio    | 项目源码 | 文件源码
def hub_image():
    """Build the image for the jupyterhub. We'll run this as a service
    that's going to then spawn the notebook server services.
    """
    client = docker.from_env()

    # Build the image from the root of the package
    parent_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    image = client.images.build(path=parent_dir, tag=HUB_IMAGE_TAG, rm=True)
    yield image
    client.images.remove(image.tags[0])
项目:SwarmSpawner    作者:cassinyio    | 项目源码 | 文件源码
def network():
    """Create the overlay network that the hub and server services will
    use to communicate.
    """
    client = docker.from_env()
    network = client.networks.create(
        name=NETWORK_NAME,
        driver="overlay",
        options={"subnet": "192.168.0.0/20"},
        attachable=True)
    yield network
    network.remove()