Python docker.from_env() Examples

The following are 30 code examples of docker.from_env(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module docker , or try the search function .
Example #1
Source File: crawl.py    From browsertrix with Apache License 2.0 6 votes vote down vote up
def print_logs(browsers, follow=False, wait=False, all_containers=False):
    docker_api = docker.from_env(version='auto')

    if follow is None:
        follow = False

    for reqid in browsers:
        if all_containers:
            print_container_log(
                docker_api, reqid, wait=False, follow=False, name='browser-'
            )

            print_container_log(
                docker_api, reqid, wait=False, follow=False, name='xserver-'
            )

        print_container_log(docker_api, reqid, wait=wait, follow=follow)


# ============================================================================ 
Example #2
Source File: installer.py    From INGInious with GNU Affero General Public License v3.0 6 votes vote down vote up
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 
Example #3
Source File: test3.py    From connect-docker with Mozilla Public License 2.0 6 votes vote down vote up
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 
Example #4
Source File: test1.py    From connect-docker with Mozilla Public License 2.0 6 votes vote down vote up
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 
Example #5
Source File: test2.py    From connect-docker with Mozilla Public License 2.0 6 votes vote down vote up
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 
Example #6
Source File: installer.py    From INGInious with GNU Affero General Public License v3.0 6 votes vote down vote up
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)) 
Example #7
Source File: test_cli.py    From naz with MIT License 6 votes vote down vote up
def setUp(self):
        self.parser = cli.cli.make_parser()

        self.docker_client = docker.from_env()
        smppSimulatorName = "nazTestSmppSimulator"
        running_containers = self.docker_client.containers.list()
        for container in running_containers:
            container.stop()

        self.smpp_server = self.docker_client.containers.run(
            "komuw/smpp_server:v0.3",
            name=smppSimulatorName,
            detach=True,
            auto_remove=True,
            labels={"name": "smpp_server", "use": "running_naz_tets"},
            ports={"2775/tcp": 2775, "8884/tcp": 8884},
            stdout=True,
            stderr=True,
        )
        self.naz_config = "tests.test_cli.NAZ_CLIENT"
        self.bad_naz_config = "tests.test_cli.BAD_NAZ_CLIENT" 
Example #8
Source File: test_service.py    From SwarmSpawner with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #9
Source File: test_client.py    From naz with MIT License 6 votes vote down vote up
def setUpClass(cls):
        docker_client = docker.from_env()
        smppSimulatorName = "nazTestSmppSimulator"
        running_containers = docker_client.containers.list()
        for container in running_containers:
            container.kill()
        cls.smpp_server = docker_client.containers.run(
            "komuw/smpp_server:v0.3",
            name=smppSimulatorName,
            detach=True,
            auto_remove=True,
            labels={"name": "smpp_server", "use": "running_naz_tets"},
            ports={"{0}/tcp".format(TestClient.smsc_port): TestClient.smsc_port, "8884/tcp": 8884},
            stdout=True,
            stderr=True,
        )
        # sleep to give enough time for the smpp_server container to have started properly
        time.sleep(1.0) 
Example #10
Source File: utils.py    From substra-backend with Apache License 2.0 6 votes vote down vote up
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) 
Example #11
Source File: docker_build.py    From biweeklybudget with GNU Affero General Public License v3.0 6 votes vote down vote up
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() 
Example #12
Source File: profile.py    From browsertrix with Apache License 2.0 6 votes vote down vote up
def get_profile_image(profile):
    try:
        global docker_api
        if not docker_api:
            docker_api = docker.from_env(version='auto')

        image_name = PROFILE_PREFIX + profile
        image = docker_api.images.get(image_name)
        assert image.labels.get(LABEL_BROWSERPROFILE) == profile
        return 'profile:' + profile

    except (docker.errors.ImageNotFound, AssertionError):
        if not is_quiet():
            print('Profile "{0}" not found'.format(profile))
        sys.exit(1)


# ============================================================================ 
Example #13
Source File: tasks.py    From substra-backend with Apache License 2.0 6 votes vote down vote up
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) 
Example #14
Source File: dockerops.py    From Cloudroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #15
Source File: tests_exception.py    From substra-backend with Apache License 2.0 6 votes vote down vote up
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) 
Example #16
Source File: docker.py    From pywren-ibm-cloud with Apache License 2.0 6 votes vote down vote up
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) 
Example #17
Source File: dockerops.py    From Cloudroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)) 
Example #18
Source File: bitcoind.py    From specter-desktop with MIT License 6 votes vote down vote up
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 
Example #19
Source File: instantboxManager.py    From instantbox with MIT License 6 votes vote down vote up
def __init__(self):
        self.client = docker.from_env()

        try:
            with open('manifest.json', 'r') as os_manifest:
                self.OS_LIST = json.load(os_manifest)
        except Exception:
            pass

        if self.OS_LIST is None:
            raise Exception(
                'Could not load manifest.json. ' +
                'Download it from https://get.instantbox.org/manifest.json'
            )

        self.AVAILABLE_OS_LIST = []
        for os in self.OS_LIST:
            for ver in os['subList']:
                self.AVAILABLE_OS_LIST.append(ver['osCode']) 
Example #20
Source File: docker.py    From picoCTF with MIT License 6 votes vote down vote up
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() 
Example #21
Source File: utils.py    From ebonite with Apache License 2.0 6 votes vote down vote up
def create_docker_client(docker_host: str = '', check=True) -> docker.DockerClient:
    """
    Context manager for DockerClient creation

    :param docker_host: DOCKER_HOST arg for DockerClient
    :param check: check if docker is available
    :return: DockerClient instance
    """
    with _docker_host_lock:
        os.environ["DOCKER_HOST"] = docker_host  # The env var DOCKER_HOST is used to configure docker.from_env()
        client = docker.from_env()
    if check and not _is_docker_running(client):
        raise RuntimeError("Docker daemon is unavailable")
    try:
        yield client
    finally:
        client.close() 
Example #22
Source File: conftest.py    From model_server with Apache License 2.0 5 votes vote down vote up
def get_docker_context():
    return docker.from_env() 
Example #23
Source File: model.py    From kryptoflow with GNU General Public License v3.0 5 votes vote down vote up
def serve(self):
        client = docker.from_env()
        serving = client.containers.get('tf.txt-serving')
        execute = serving.exec_run(cmd=['tensorflow_model_server',
                                        '--port=9000',
                                        '--model_base_path=%s' % os.path.join('/serving/', self.model_type),
                                        '&>',
                                        '%s_%s.log' % (self.model_type, str(self.number)),
                                        '&'],
                                   socket=False,
                                   stdout=True,
                                   stream=True)
        for s in execute[1]:
            _logger.debug(s) 
Example #24
Source File: deploy.py    From margipose with Apache License 2.0 5 votes vote down vote up
def build_and_push_image(image_tag):
    docker_client = docker.from_env()
    log.info('Building Docker image...')
    docker_client.images.build(path='.', tag=image_tag)
    log.info('Pushing Docker image...')
    docker_client.images.push(image_tag) 
Example #25
Source File: docker_client.py    From cross_compile with Apache License 2.0 5 votes vote down vote up
def __init__(
        self,
        disable_cache: bool = False,
        default_docker_dir: Optional[Path] = None,
        colcon_defaults_file: Optional[Path] = None,
    ):
        """
        Construct the DockerClient.

        :param disable_cache: If True, disable the Docker cache when building images.
        """
        self._client = docker.from_env()
        self._disable_cache = disable_cache
        self._default_docker_dir = str(default_docker_dir or Path(__file__).parent / 'docker')
        self._colcon_defaults_file = str(colcon_defaults_file or DEFAULT_COLCON_DEFAULTS_FILE) 
Example #26
Source File: cleanup.py    From model_server with Apache License 2.0 5 votes vote down vote up
def get_docker_client():
    return docker.from_env() 
Example #27
Source File: gallery.py    From tljh-voila-gallery with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _get_docker_images():
    client = docker.from_env()
    image_tags = [
        tag for image in client.images.list(filters={"dangling": False})
        for tag in image.tags
    ]
    return set(image_tags)


# Read gallery.yaml on each spawn. If this gets too expensive, cache it here
# Only keep the examples that have a corresponding Docker image 
Example #28
Source File: utils.py    From neurodocker with Apache License 2.0 5 votes vote down vote up
def get_docker_client(version='auto', **kwargs):
    try:
        import docker
    except ImportError:
        raise ImportError("the docker python package is required for this")
    return docker.from_env(version='auto', **kwargs) 
Example #29
Source File: smr_engine.py    From son-mano-framework with Apache License 2.0 5 votes vote down vote up
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 
Example #30
Source File: eris.py    From platform-resource-manager with Apache License 2.0 5 votes vote down vote up
def detect_cgroup_driver():
    """
    Detect docker cgroup parent dir based on cgroup driver type
    """
    client = docker.from_env()
    dockinfo = client.info()
    cgroup_driver = dockinfo['CgroupDriver']
    return cgroup_driver