Python docker.APIClient() Examples

The following are 30 code examples of docker.APIClient(). 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: test_docker.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_execute_tls(self, client_class_mock, tls_class_mock):
        client_mock = mock.Mock(spec=APIClient)
        client_mock.create_container.return_value = {'Id': 'some_id'}
        client_mock.create_host_config.return_value = mock.Mock()
        client_mock.images.return_value = []
        client_mock.attach.return_value = []
        client_mock.pull.return_value = []
        client_mock.wait.return_value = {"StatusCode": 0}

        client_class_mock.return_value = client_mock
        tls_mock = mock.Mock()
        tls_class_mock.return_value = tls_mock

        operator = DockerOperator(docker_url='tcp://127.0.0.1:2376', image='ubuntu',
                                  owner='unittest', task_id='unittest', tls_client_cert='cert.pem',
                                  tls_ca_cert='ca.pem', tls_client_key='key.pem')
        operator.execute(None)

        tls_class_mock.assert_called_once_with(assert_hostname=None, ca_cert='ca.pem',
                                               client_cert=('cert.pem', 'key.pem'),
                                               ssl_version=None, verify=True)

        client_class_mock.assert_called_once_with(base_url='https://127.0.0.1:2376',
                                                  tls=tls_mock, version=None) 
Example #2
Source File: host.py    From blockade with Apache License 2.0 6 votes vote down vote up
def __init__(self, image=DEFAULT_IMAGE, container_timeout=3600,
                 container_expire=3000, container_prefix=None,
                 docker_client=None):
        self._image = image
        self._container_timeout = container_timeout
        self._container_expire = container_expire

        if container_prefix:
            self._container_prefix = container_prefix
        elif os.environ.get(CONTAINER_PREFIX_ENV):
            self._container_prefix = os.environ[CONTAINER_PREFIX_ENV]
        else:
            self._container_prefix = DEFAULT_CONTAINER_PREFIX

        self._docker_client = docker_client or docker.APIClient(
            **docker.utils.kwargs_from_env(assert_hostname=False)
        )
        self._lock = threading.RLock()
        self._reset_container() 
Example #3
Source File: test_rpmqa.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_rpmqa_plugin_skip(docker_tasker):  # noqa
    """
    Test skipping the plugin if workflow.image_components is already set
    """
    mock_docker()
    workflow = DockerBuildWorkflow(TEST_IMAGE, source=SOURCE)
    workflow.source = StubSource()
    workflow.builder = StubInsideBuilder().for_workflow(workflow)

    image_components = {
        'type': 'rpm',
        'name': 'something'
    }
    setattr(workflow, 'image_components', image_components)

    flexmock(docker.APIClient, logs=mock_logs_raise)
    runner = PostBuildPluginsRunner(docker_tasker, workflow,
                                    [{"name": PostBuildRPMqaPlugin.key,
                                      "args": {'image_id': TEST_IMAGE}}])
    results = runner.run()
    assert results[PostBuildRPMqaPlugin.key] is None
    assert workflow.image_components == image_components 
Example #4
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 #5
Source File: docker_backend.py    From sen with MIT License 6 votes vote down vote up
def __init__(self):
        self._containers = None
        self._images = None  # displayed images
        self._all_images = None  # docker images -a
        self._df = None

        kwargs = {"version": "auto"}
        kwargs.update(docker.utils.kwargs_from_env(assert_hostname=False))

        try:
            APIClientClass = docker.Client  # 1.x
        except AttributeError:
            APIClientClass = docker.APIClient  # 2.x

        try:
            self.client = APIClientClass(**kwargs)
        except docker.errors.DockerException as ex:
            raise TerminateApplication("can't establish connection to docker daemon: {0}".format(str(ex)))

        self.scratch_image = RootImage(self)

    # backend queries 
Example #6
Source File: test_docker_swarm.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_failed_service_raises_error(self, types_mock, client_class_mock):

        mock_obj = mock.Mock()

        client_mock = mock.Mock(spec=APIClient)
        client_mock.create_service.return_value = {'ID': 'some_id'}
        client_mock.images.return_value = []
        client_mock.pull.return_value = [b'{"status":"pull log"}']
        client_mock.tasks.return_value = [{'Status': {'State': 'failed'}}]
        types_mock.TaskTemplate.return_value = mock_obj
        types_mock.ContainerSpec.return_value = mock_obj
        types_mock.RestartPolicy.return_value = mock_obj
        types_mock.Resources.return_value = mock_obj

        client_class_mock.return_value = client_mock

        operator = DockerSwarmOperator(image='', auto_remove=False, task_id='unittest', enable_logging=False)
        msg = "Service failed: {'ID': 'some_id'}"
        with self.assertRaises(AirflowException) as error:
            operator.execute(None)
        self.assertEqual(str(error.exception), msg) 
Example #7
Source File: test_tasker.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_timeout(timeout, expected_timeout):
    if not hasattr(docker, 'APIClient'):
        setattr(docker, 'APIClient', docker.Client)

    expected_kwargs = {
        'timeout': expected_timeout
    }
    if hasattr(docker, 'AutoVersionClient'):
        expected_kwargs['version'] = 'auto'

    (flexmock(docker.APIClient)
        .should_receive('__init__')
        .with_args(**expected_kwargs)
        .once())

    kwargs = {}
    if timeout is not None:
        kwargs['timeout'] = timeout

    DockerTasker(**kwargs) 
Example #8
Source File: test_docker_swarm.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_no_auto_remove(self, types_mock, client_class_mock):

        mock_obj = mock.Mock()

        client_mock = mock.Mock(spec=APIClient)
        client_mock.create_service.return_value = {'ID': 'some_id'}
        client_mock.images.return_value = []
        client_mock.pull.return_value = [b'{"status":"pull log"}']
        client_mock.tasks.return_value = [{'Status': {'State': 'complete'}}]
        types_mock.TaskTemplate.return_value = mock_obj
        types_mock.ContainerSpec.return_value = mock_obj
        types_mock.RestartPolicy.return_value = mock_obj
        types_mock.Resources.return_value = mock_obj

        client_class_mock.return_value = client_mock

        operator = DockerSwarmOperator(image='', auto_remove=False, task_id='unittest', enable_logging=False)
        operator.execute(None)

        self.assertEqual(
            client_mock.remove_service.call_count, 0,
            'Docker service being removed even when `auto_remove` set to `False`'
        ) 
Example #9
Source File: test_tasker.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_docker_tasker(autoversion, base_url_arg):
    mock_docker()
    base_url = 'unix://var/run/docker.sock'
    kwargs = {}
    if base_url_arg:
        kwargs['base_url'] = base_url
    else:
        os.environ['DOCKER_CONNECTION'] = base_url

    expected_kwargs = {'base_url': base_url, 'timeout': 120}
    if autoversion:
        setattr(docker, 'AutoVersionClient', 'auto')
        expected_kwargs['version'] = 'auto'

    (flexmock(docker.APIClient)
        .should_receive('__init__')
        .with_args(**expected_kwargs)
        .once())

    DockerTasker(**kwargs)

    os.environ.pop('DOCKER_CONNECTION', None)
    if autoversion:
        delattr(docker, 'AutoVersionClient') 
Example #10
Source File: utils.py    From blockade with Apache License 2.0 5 votes vote down vote up
def check_docker():
    client = docker.APIClient(
        **docker.utils.kwargs_from_env(assert_hostname=False)
    )
    try:
        client.ping()
    except Exception as e:
        raise BlockadeError(("Unable to connect to Docker: %s\n\n" +
            "Blockade requires access to a Docker API.\nEnsure that Docker " +
            "is running and your user has the correct privileges to access " +
            "it.\nOr set the DOCKER_HOST env to point to an external Docker.")
        % (str(e),)) 
Example #11
Source File: fake_api_client.py    From runway with Apache License 2.0 5 votes vote down vote up
def make_fake_client(overrides=None):
    """Return a Client with a fake APIClient."""
    client = docker.DockerClient()
    client.api = make_fake_api_client(overrides)
    return client 
Example #12
Source File: docker_device.py    From android-emulator-container-scripts with Apache License 2.0 5 votes vote down vote up
def get_api_client(self):
        try:
            api_client = docker.APIClient()
            logging.info(api_client.version())
            return api_client
        except:
            logging.exception("Failed to create default client, trying domain socket.", exc_info=True)

        api_client = docker.APIClient(base_url="unix://var/run/docker.sock")
        logging.info(api_client.version())
        return api_client 
Example #13
Source File: core.py    From rocker with Apache License 2.0 5 votes vote down vote up
def get_docker_client():
    """Simple helper function for pre 2.0 imports"""
    try:
        docker_client = docker.APIClient()
    except AttributeError:
        # docker-py pre 2.0
        docker_client = docker.Client()
    return docker_client 
Example #14
Source File: build.py    From kolla with Apache License 2.0 5 votes vote down vote up
def dc(self):
        if self._dc is not None:
            return self._dc
        docker_kwargs = self.docker_kwargs.copy()
        self._dc = docker.APIClient(version='auto', **docker_kwargs)
        return self._dc 
Example #15
Source File: docker_gc.py    From docker-custodian with Apache License 2.0 5 votes vote down vote up
def main():
    logging.basicConfig(
        level=logging.INFO,
        format="%(message)s",
        stream=sys.stdout)

    args = get_args()
    client = docker.APIClient(version='auto',
                              timeout=args.timeout,
                              **kwargs_from_env())

    exclude_container_labels = format_exclude_labels(
        args.exclude_container_label
    )

    if args.max_container_age:
        cleanup_containers(
            client,
            args.max_container_age,
            args.dry_run,
            exclude_container_labels,
        )

    if args.max_image_age:
        exclude_set = build_exclude_set(
            args.exclude_image,
            args.exclude_image_file)
        cleanup_images(client, args.max_image_age, args.dry_run, exclude_set)

    if args.dangling_volumes:
        cleanup_volumes(client, args.dry_run) 
Example #16
Source File: docker_autostop.py    From docker-custodian with Apache License 2.0 5 votes vote down vote up
def main():
    logging.basicConfig(
        level=logging.INFO,
        format="%(message)s",
        stream=sys.stdout)

    opts = get_opts()
    client = docker.APIClient(version='auto',
                              timeout=opts.timeout,
                              **kwargs_from_env())

    matcher = build_container_matcher(opts.prefix)
    stop_containers(client, opts.max_run_time, matcher, opts.dry_run) 
Example #17
Source File: conftest.py    From docker-custodian with Apache License 2.0 5 votes vote down vote up
def mock_client():
    client = mock.create_autospec(docker.APIClient)
    client._version = '1.21'
    return client 
Example #18
Source File: docker_v1.py    From senlin with Apache License 2.0 5 votes vote down vote up
def __init__(self, url):
        self._dockerclient = docker.APIClient(base_url=url, version='auto') 
Example #19
Source File: test_host.py    From blockade with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.helper = HostExecHelper()

        self.docker = docker.APIClient(
            **docker.utils.kwargs_from_env(assert_hostname=False)) 
Example #20
Source File: fake_api_client.py    From runway with Apache License 2.0 5 votes vote down vote up
def make_fake_api_client(overrides=None):
    """Return non-complete fake APIClient.

    This returns most of the default cases correctly, but most arguments that
    change behavior will not work.

    """
    if overrides is None:
        overrides = {}
    api_client = docker.APIClient()
    mock_attrs = {
        'build.return_value': fake_api.FAKE_CONTAINER_ID,
        'commit.return_value': fake_api.post_fake_commit()[1],
        'containers.return_value': fake_api.get_fake_containers()[1],
        'create_container.return_value':
            fake_api.post_fake_create_container()[1],
        'create_host_config.side_effect': api_client.create_host_config,
        'create_network.return_value': fake_api.post_fake_network()[1],
        'exec_create.return_value': fake_api.post_fake_exec_create()[1],
        'exec_start.return_value': fake_api.post_fake_exec_start()[1],
        'images.return_value': fake_api.get_fake_images()[1],
        'inspect_container.return_value':
            fake_api.get_fake_inspect_container()[1],
        'inspect_image.return_value': fake_api.get_fake_inspect_image()[1],
        'inspect_network.return_value': fake_api.get_fake_network()[1],
        'logs.return_value': [b'hello world\n'],
        'networks.return_value': fake_api.get_fake_network_list()[1],
        'start.return_value': None,
        'wait.return_value': {'StatusCode': 0},
    }
    mock_attrs.update(overrides)
    mock_client = CopyReturnMagicMock(**mock_attrs)

    mock_client._version = docker.constants.DEFAULT_DOCKER_API_VERSION
    return mock_client 
Example #21
Source File: builder.py    From polyaxon with Apache License 2.0 5 votes vote down vote up
def __init__(
        self, context, destination, credstore_env=None, registries=None, docker=None
    ):
        self.destination = destination

        self.context = context
        self._validate_registries(registries)
        self.registries = registries
        self.docker = docker or APIClient(version="auto", credstore_env=credstore_env)
        self.is_pushing = False 
Example #22
Source File: docker_engine.py    From worker with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.cli = docker.APIClient(base_url='unix://var/run/docker.sock', version="auto")

    # check network exists: 
Example #23
Source File: kuryr.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def run(self, container_create_args=None):
        """Start and stop container on docker network.

        Measure the "docker run" , "docker stop", "docker rm"
        command performance.
        """
        self.docker_client = docker.APIClient(base_url='tcp://0.0.0.0:2375')
        container_id = self._start_container(container_create_args or {})
        self._stop_container(container_id)
        # TODO(yedongcan) We will hit the Docker bug:
        # "Unable to remove filesystem - device or resource busy"
        # Temporary workaround is disable remove_container here.
        # self._remove_container(container_id) 
Example #24
Source File: kuryr.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def run(self, network_create_args=None):
        """Create and delete a network without kuryr.

        Measure the "docker network create" and "docker network rm" command
        performance with default driver.

        :param network_create_args: dict as options to create the network
        """
        self.docker_client = docker.APIClient(base_url='tcp://0.0.0.0:2375')
        network = self._create_network(is_kuryr=False,
                       network_create_args=network_create_args or {})
        self._delete_network(network) 
Example #25
Source File: kuryr.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def run(self, network_create_args=None):
        """Create and delete a network with kuryr.

        Measure the "docker network create" and "docker network rm" command
        performance with kuryr driver.

        :param network_create_args: dict as options to create the network
        """
        self.docker_client = docker.APIClient(base_url='tcp://0.0.0.0:2375')
        network = self._create_network(is_kuryr=True,
                       network_create_args=network_create_args or {})
        self._delete_network(network) 
Example #26
Source File: kuryr.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def run(self, network_list_args=None):
        """List the networks.

        Measure the "docker network ls" command performance under kuryr.

        This will call the docker client API to list networks

        TODO (baohua):
        1. may support tenant/user in future.
        2. validation.required_services add KURYR support

        :param network_list_args: dict: names, ids
        """
        self.docker_client = docker.APIClient(base_url='tcp://0.0.0.0:2375')
        self._list_networks(network_list_args or {}) 
Example #27
Source File: kuryr_base.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(KuryrBaseTest, self).setUp()
        self.docker_client = docker.APIClient(
            base_url='tcp://0.0.0.0:2375')
        try:
            self.neutron_client = get_neutron_client_from_env()
        except Exception as e:
            # We may missing or didn't source configured openrc file.
            message = ("Missing environment variable %s in your local."
                       "Please add it and also check other missing "
                       "environment variables. After that please source "
                       "the openrc file. "
                       "Trying credentials from DevStack cloud.yaml ...")
            LOG.warning(message, e.args[0])
            self.neutron_client = get_neutron_client_from_creds() 
Example #28
Source File: __init__.py    From ahab with ISC License 5 votes vote down vote up
def listen(self):
        client = docker.APIClient(base_url=self.url, version=docker_client_version)

        # the 'since' flag is to start reading from a particular event.
        # see the docker SDK docs:
        # https://docker-py.readthedocs.io/en/stable/client.html#docker.client.DockerClient.events
        for event in client.events(decode=True, since=self.since):
            for k in ['time', 'Time']:
                if k in event:
                    event[k] = datetime.fromtimestamp(event[k])
            log.debug('Event: %s', event)
            data = {}
            i = get_id(event)
            if i is not None:
                try:
                    if 'from' in event or 'From' in event:
                        data = client.inspect_container(i)
                    else:
                        data = client.inspect_image(i)
                    self.data[i] = data
                except docker.errors.NotFound:
                    data = self.data[i]
            self.handle(event, data)
            # mark the last event seen so we can restart this listener
            # without dropping events. the caller must be responsible for
            # ensuring that handlers do not drop events, because they are
            # fire-and-forget from this point.
            self.since = get_time_nano(event) 
Example #29
Source File: _docker.py    From tcconfig with MIT License 5 votes vote down vote up
def __init__(self, tc_command_output=TcCommandOutput.NOT_SET):
        self.__client = APIClient(version="auto")
        self.__host_name = os.uname()[1]
        self.__tc_command_output = tc_command_output

        self.__con = connect_memdb()
        IfIndex.attach(self.__con) 
Example #30
Source File: docker_monitor.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def __init__(
        self,
        docker_api_socket,
        docker_api_version,
        logger,
        cache_expiration_secs=300,
        cache_clean_secs=5,
    ):
        """
        Initializes one instance.

        @param docker_api_socket: The path to the UNIX socket exporting the Docker API by the daemon.
        @param docker_api_version: The API version to use, typically 'auto'.
        @param cache_expiration_secs: The number of seconds to cache a mapping from container id to container name.  If
            the mapping is not used for this number of seconds, the mapping will be evicted.  (The actual eviction
            is performed lazily).
        @param cache_clean_secs:  The number of seconds between sweeps to clean the cache.
        @param logger: The logger to use.  This MUST be supplied.
        @type docker_api_socket: six.text_type
        @type docker_api_version: six.text_type
        @type cache_expiration_secs: double
        @type cache_clean_secs: double
        @type logger: Logger
        """
        # Guards all variables except for __logger and __docker_client.
        self.__lock = threading.Lock()
        self.__cache = dict()
        # The walltime of when the cache was last cleaned.
        self.__last_cache_clean = time.time()
        self.__cache_expiration_secs = cache_expiration_secs
        self.__cache_clean_secs = cache_clean_secs
        self.__docker_client = docker.APIClient(  # pylint: disable=no-member
            base_url=("unix:/%s" % docker_api_socket), version=docker_api_version
        )
        # The set of container ids that have not been used since the last cleaning.  These are eviction candidates.
        self.__untouched_ids = dict()
        self.__logger = logger