Python docker.errors() Examples

The following are 30 code examples of docker.errors(). 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: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def __init__(self, docker_config: DockerHostConfig, mock_client=None) -> None:
        self.name = docker_config.name
        self.docker_config = docker_config
        if not docker_config.tls:
            tls = None
        else:
            tls = docker.tls.TLSConfig(client_cert=(docker_config.tls_cert, docker_config.tls_key), verify=docker_config.tls_ca)

        # Simplify testing
        if mock_client is not None:
            self.cli = mock_client
            return

        try:
            self.cli = docker.DockerClient(base_url=docker_config.address, version="auto", tls=tls)
        except docker.errors.DockerException as e:
            raise ZoeException("Cannot connect to Docker host {} at address {}: {}".format(docker_config.name, docker_config.address, str(e))) 
Example #2
Source File: dockerTest.py    From toil with Apache License 2.0 6 votes vote down vote up
def testDockerPipeChainErrorDetection(self, disableCaching=True):
        """
        By default, executing cmd1 | cmd2 | ... | cmdN, will only return an
        error if cmdN fails.  This can lead to all manor of errors being
        silently missed.  This tests to make sure that the piping API for
        dockerCall() throws an exception if non-last commands in the chain fail.
        """
        options = Job.Runner.getDefaultOptions(os.path.join(self.tempDir,
                                                            'jobstore'))
        options.logLevel = self.dockerTestLogLevel
        options.workDir = self.tempDir
        options.clean = 'always'
        options.caching = disableCaching
        A = Job.wrapJobFn(_testDockerPipeChainErrorFn)
        rv = Job.Runner.startToil(A, options)
        assert rv == True 
Example #3
Source File: conftest.py    From anchore-cli with Apache License 2.0 6 votes vote down vote up
def pytest_report_header(config):
    logger = get_logger('report_header')
    msg = []
    try:
        client = create_client()
        metadata = client.api.inspect_container('pytest_inline_scan')
    except docker.errors.NotFound:
        logger.info("No running container was found, can't add info to report header")
        metadata = {'Config': {'Labels': {}}}
        msg = ['Docker: Anchore inline_scan container not running yet']
    except DockerException as e:
        logger.exception('Unable to connect to a docker socket')
        msg = ['Anchore Version: Unable to connect to a docker socket']
        msg.append('Error: %s' % str(e))
        return msg

    labels = metadata['Config']['Labels']
    version = labels.get('version', 'unknown')
    commit = labels.get('anchore_commit', 'unknown')

    msg.extend([
       'Anchore Version: %s' % version,
       'Anchore Commit: %s' % commit
    ])
    return msg 
Example #4
Source File: local_container.py    From gigantum-client with MIT License 6 votes vote down vote up
def image_available(self) -> bool:
        """Do we currently see an up-to-date Docker image?

        Returns:
            True if we've gotten an Image ID stored.
        """
        if not self._image_id:
            try:
                self._image_id = self._client.images.get(name=self.image_tag).id
                return True
            except docker.errors.ImageNotFound:
                return False
        else:
            return True

    # Working with Containers 
Example #5
Source File: docker_driver.py    From dagda with Apache License 2.0 6 votes vote down vote up
def docker_logs(self, container_id, show_stdout, show_stderr, follow):
        try:
            return (self.cli.logs(container=container_id, stdout=show_stdout, stderr=show_stderr, follow=follow))\
                   .decode('utf-8')
        except docker.errors.APIError as ex:
            if "configured logging reader does not support reading" in str(ex):
                message = "Docker logging driver is not set to be 'json-file' or 'journald'"
                DagdaLogger.get_logger().error(message)
                raise DagdaError(message)
            else:
                message = "Unexpected exception of type {0} occurred: {1!r}" \
                    .format(type(ex).__name__, str(ex))
                DagdaLogger.get_logger().error(message)
                raise ex

    # Creates container and return the container id 
Example #6
Source File: test_features.py    From DockerMake with Apache License 2.0 6 votes vote down vote up
def test_handle_missing_squash_cache(experimental_daemon, squashcache):
    run_docker_make("-f data/secret-squash.yml cache-test invisible-secret")
    client = helpers.get_client()
    cachelayer = client.images.get("invisible-secret")
    firstimg = client.images.get("cache-test")
    for _id in ("cache-test", firstimg.id, "invisible_secret", cachelayer.id):
        try:
            client.images.remove(_id)
        except docker.errors.ImageNotFound:
            pass

    # Make sure the image can rebuild even if original layers are missing
    run_docker_make("-f data/secret-squash.yml cache-test")

    # Sanity check - makes sure that the first image was in fact removed and not used for cache
    assert client.images.get("cache-test").id != firstimg.id 
Example #7
Source File: engine.py    From ansible-playbook-bundle with GNU General Public License v2.0 6 votes vote down vote up
def build_apb(project, dockerfile=None, tag=None):
    if dockerfile is None:
        dockerfile = "Dockerfile"
    spec = get_spec(project)
    if 'version' not in spec:
        print("APB spec does not have a listed version. Please update apb.yml")
        exit(1)

    if not tag:
        tag = spec['name']

    update_dockerfile(project, dockerfile)

    print("Building APB using tag: [%s]" % tag)

    try:
        client = create_docker_client()
        client.images.build(path=project, tag=tag, dockerfile=dockerfile)
    except docker.errors.DockerException:
        print("Error accessing the docker API. Is the daemon running?")
        raise

    print("Successfully built APB image: %s" % tag)
    return tag 
Example #8
Source File: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def update(self, docker_id, cpu_quota=None, mem_reservation=None, mem_limit=None):
        """Update the resource reservation for a container."""
        kwargs = {}
        if cpu_quota is not None:
            kwargs['cpu_quota'] = cpu_quota
        if mem_reservation is not None:
            kwargs['mem_reservation'] = mem_reservation
        if mem_limit is not None:
            kwargs['mem_limit'] = mem_limit

        try:
            cont = self.cli.containers.get(docker_id)
        except (docker.errors.NotFound, docker.errors.APIError):
            return

        try:
            cont.update(**kwargs)
        except docker.errors.APIError:
            pass 
Example #9
Source File: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def logs(self, docker_id: str, stream: bool, follow=None):
        """
        Retrieves the logs of the selected container.

        :param docker_id:
        :param stream:
        :param follow:
        :return:
        """
        try:
            cont = self.cli.containers.get(docker_id)
        except (docker.errors.NotFound, docker.errors.APIError):
            return None

        try:
            return cont.logs(stdout=True, stderr=True, follow=follow, stream=stream, timestamps=True, tail='all')
        except docker.errors.APIError:
            return None 
Example #10
Source File: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def stats(self, docker_id: str, stream: bool):
        """Retrieves container stats based on resource usage."""
        try:
            cont = self.cli.containers.get(docker_id)
        except docker.errors.NotFound:
            raise ZoeException('Container not found')
        except docker.errors.APIError as e:
            raise ZoeException('Docker API error: {}'.format(e))

        try:
            return cont.stats(stream=stream)
        except docker.errors.APIError as e:
            raise ZoeException('Docker API error: {}'.format(e))
        except requests.exceptions.ReadTimeout:
            raise ZoeException('Read timeout')
        except ValueError:
            raise ZoeException('Docker API decoding error') 
Example #11
Source File: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def list(self, only_label=None, status=None) -> List[dict]:
        """
        List running or defined containers.

        :param only_label: filter containers with only a certain label
        :param status: filter containers with only a certain status (one of restarting, running, paused, exited)
        :return: a list of containers
        """
        filters = {}
        if only_label is not None:
            filters['label'] = only_label
        if status is not None:
            filters['status'] = status
        try:
            ret = self.cli.containers.list(all=True, filters=filters)
        except docker.errors.APIError as ex:
            raise ZoeException(str(ex))
        except requests.exceptions.RequestException as ex:
            raise ZoeException(str(ex))
        conts = []
        for cont_info in ret:
            conts.append(self._container_summary(cont_info))

        return conts 
Example #12
Source File: api_client.py    From zoe with Apache License 2.0 6 votes vote down vote up
def terminate_container(self, docker_id: str, delete=False) -> None:
        """
        Terminate a container.

        :param docker_id: The container to terminate
        :type docker_id: str
        :param delete: If True, also delete the container files
        :type delete: bool
        :return: None
        """
        try:
            cont = self.cli.containers.get(docker_id)
        except docker.errors.NotFound:
            return

        try:
            if delete:
                cont.remove(force=True)
            else:
                cont.stop(timeout=5)
        except docker.errors.NotFound:
            pass
        except docker.errors.APIError as e:
            log.warning(str(e)) 
Example #13
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 #14
Source File: test_tasker.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_export(docker_tasker, no_container):
    if MOCK:
        mock_docker()

    container_dict = docker_tasker.create_container(INPUT_IMAGE, command=["/bin/bash"])
    container_id = container_dict['Id']

    try:
        if no_container:
            with pytest.raises(docker.errors.APIError):
                docker_tasker.export_container('NOT_THERE')
        else:
            export_generator = docker_tasker.export_container(container_id)
            for _ in export_generator:
                pass
    finally:
        docker_tasker.remove_container(container_id) 
Example #15
Source File: docker_utils.py    From sc-docker with MIT License 6 votes vote down vote up
def ensure_local_net(
        network_name: str = DOCKER_STARCRAFT_NETWORK,
        subnet_cidr: str = SUBNET_CIDR
) -> None:
    """
    Create docker local net if not found.

    :raises docker.errors.APIError
    """
    logger.info(f"checking whether docker has network {network_name}")
    ipam_pool = docker.types.IPAMPool(subnet=subnet_cidr)
    ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
    networks = docker_client.networks.list(names=DOCKER_STARCRAFT_NETWORK)
    output = networks[0].short_id if networks else None
    if not output:
        logger.info("network not found, creating ...")
        output = docker_client.networks.create(DOCKER_STARCRAFT_NETWORK, ipam=ipam_config).short_id
    logger.debug(f"docker network id: {output}") 
Example #16
Source File: test_tasker.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_retry_method(retry_times):
    my_args = ('some', 'new')
    my_kwargs = {'one': 'first', 'two': 'second'}

    (flexmock(sys.modules[__name__])
        .should_call('my_func')
        .with_args(*my_args, **my_kwargs)
        .times(retry_times + 1))
    (flexmock(time)
        .should_receive('sleep')
        .and_return(None))

    if retry_times >= 0:
        with pytest.raises(docker.errors.APIError):
            retry(my_func, *my_args, retry=retry_times, **my_kwargs)
    else:
        retry(my_func, *my_args, retry=retry_times, **my_kwargs) 
Example #17
Source File: docker_utils.py    From sc-docker with MIT License 5 votes vote down vote up
def container_exit_code(container_id: str) -> Optional[int]:
    """
    :raises docker.errors.NotFound
    :raises docker.errors.APIError
    """
    container = docker_client.containers.get(container_id)
    return container.wait()["StatusCode"] 
Example #18
Source File: app.py    From repo2docker with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def initialize(self):
        """Init repo2docker configuration before start"""
        # FIXME: Remove this function, move it to setters / traitlet reactors
        if self.json_logs:
            # register JSON excepthook to avoid non-JSON output on errors
            sys.excepthook = self.json_excepthook
            # Need to reset existing handlers, or we repeat messages
            logHandler = logging.StreamHandler()
            formatter = jsonlogger.JsonFormatter()
            logHandler.setFormatter(formatter)
            self.log = logging.getLogger("repo2docker")
            self.log.handlers = []
            self.log.addHandler(logHandler)
            self.log.setLevel(self.log_level)
        else:
            # due to json logger stuff above,
            # our log messages include carriage returns, newlines, etc.
            # remove the additional newline from the stream handler
            self.log.handlers[0].terminator = ""
            # We don't want a [Repo2Docker] on all messages
            self.log.handlers[0].formatter = logging.Formatter(fmt="%(message)s")

        if self.dry_run and (self.run or self.push):
            raise ValueError("Cannot push or run image if we are not building it")

        if self.volumes and not self.run:
            raise ValueError("Cannot mount volumes if container is not run") 
Example #19
Source File: docker_gc.py    From docker-custodian with Apache License 2.0 5 votes vote down vote up
def api_call(func, **kwargs):
    try:
        return func(**kwargs)
    except requests.exceptions.Timeout as e:
        params = ','.join('%s=%s' % item for item in kwargs.items())
        log.warn("Failed to call %s %s %s" % (func.__name__, params, e))
    except docker.errors.APIError as ae:
        params = ','.join('%s=%s' % item for item in kwargs.items())
        log.warn("Error calling %s %s %s" % (func.__name__, params, ae)) 
Example #20
Source File: app.py    From repo2docker with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json_excepthook(self, etype, evalue, traceback):
        """Called on an uncaught exception when using json logging

        Avoids non-JSON output on errors when using --json-logs
        """
        self.log.error(
            "Error during build: %s",
            evalue,
            exc_info=(etype, evalue, traceback),
            extra=dict(phase="failed"),
        ) 
Example #21
Source File: docker_autostop.py    From docker-custodian with Apache License 2.0 5 votes vote down vote up
def stop_container(client, id):
    try:
        client.stop(id)
    except requests.exceptions.Timeout as e:
        log.warn("Failed to stop container %s: %s" % (id, e))
    except docker.errors.APIError as ae:
        log.warn("Error stopping %s: %s" % (id, ae)) 
Example #22
Source File: docker_driver.py    From dagda with Apache License 2.0 5 votes vote down vote up
def docker_exec(self, container_id, cmd, show_stdout, show_stderr):
        dict = self.cli.exec_create(container=container_id, cmd=cmd, stdout=show_stdout, stderr=show_stderr)
        return (self.cli.exec_start(exec_id=dict.get('Id'))).decode("utf-8", errors="ignore")

    # Gets logs from docker container 
Example #23
Source File: test_features.py    From DockerMake with Apache License 2.0 5 votes vote down vote up
def test_squashing_error_without_experimental_daemon(non_experimental_daemon):
    with pytest.raises(dockermake.errors.ExperimentalDaemonRequiredError):
        run_docker_make("-f data/secret-squash.yml invisible-secret visible-secret") 
Example #24
Source File: container.py    From freight_forwarder with MIT License 5 votes vote down vote up
def dump_logs(self):
        """dump entirety of the container logs to stdout

            :returns None
        """
        msg = "log dump: \n"
        if self._transcribe:
            if self._transcribe_queue:
                while not self._transcribe_queue.empty():
                    logs = self._transcribe_queue.get()

                    if isinstance(logs, six.binary_type):
                        logs = logs.decode(encoding='UTF-8', errors="ignore")

                    msg = '{0} {1}'.format(msg, logs)
        else:
            logs = self.client.logs(self.id, stdout=True, stderr=True, stream=False, timestamps=False, tail='all')
            if isinstance(logs, six.binary_type):
                logs = logs.decode(encoding='UTF-8', errors="ignore")

            msg = '{0}{1}'.format(msg, logs)

        logger.error(msg)

    ###
    # Static methods
    ### 
Example #25
Source File: dockerspawner.py    From dockerspawner with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def remove_object(self):
        self.log.info("Removing %s %s", self.object_type, self.object_id)
        # remove the container, as well as any associated volumes
        try:
            yield self.docker("remove_" + self.object_type, self.object_id, v=True)
        except docker.errors.APIError as e:
            if e.status_code == 409:
                self.log.debug("Already removing %s: %s", self.object_type, self.object_id)
            else:
                raise 
Example #26
Source File: docker_utils.py    From sc-docker with MIT License 5 votes vote down vote up
def remove_game_image(image_name: str) -> None:
    try:
        docker_client.images.get(image_name)
    except docker.errors.ImageNotFound:
        pass
    except docker.errors.APIError:
        logger.error(f"there occurred an error trying to find image {image_name}")
    else:
        docker_client.images.remove(image_name, force=True)
    logger.info(f"docker image {image_name} removed.") 
Example #27
Source File: test_features.py    From DockerMake with Apache License 2.0 5 votes vote down vote up
def test_build_fails_if_secrets_already_exist(experimental_daemon, secretfail):
    with pytest.raises(dockermake.errors.BuildError):
        run_docker_make("-f data/secret-squash.yml secretfail") 
Example #28
Source File: docker_utils.py    From sc-docker with MIT License 5 votes vote down vote up
def ensure_local_image(
        local_image: str,
        parent_image: str = SC_PARENT_IMAGE,
        java_image: str = SC_JAVA_IMAGE,
        starcraft_base_dir: str = SCBW_BASE_DIR,
        starcraft_binary_link: str = SC_BINARY_LINK,
) -> None:
    """
    Check if `local_image` is present locally. If it is not, pull parent images and build.
    This includes pulling starcraft binary.

    :raises docker.errors.ImageNotFound
    :raises docker.errors.APIError
    """
    logger.info(f"checking if there is local image {local_image}")
    docker_images = docker_client.images.list(local_image)
    if len(docker_images) and docker_images[0].short_id is not None:
        logger.info(f"image {local_image} found locally.")
        return

    logger.info("image not found locally, creating...")
    pkg_docker_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "local_docker")
    base_dir = os.path.join(starcraft_base_dir, "docker")
    logger.info(f"copying files from {pkg_docker_dir} to {base_dir}.")
    distutils.dir_util.copy_tree(pkg_docker_dir, base_dir)

    starcraft_zip_file = f"{base_dir}/starcraft.zip"
    if not os.path.exists(starcraft_zip_file):
        logger.info(f"downloading starcraft.zip to {starcraft_zip_file}")
        download_file(starcraft_binary_link, starcraft_zip_file)

    logger.info(f"pulling image {parent_image}, this may take a while...")
    pulled_image = docker_client.images.pull(parent_image)
    pulled_image.tag(java_image)

    logger.info(f"building local image {local_image}, this may take a while...")
    docker_client.images.build(path=base_dir, dockerfile="game.dockerfile", tag=local_image)
    logger.info(f"successfully built image {local_image}") 
Example #29
Source File: docker_utils.py    From sc-docker with MIT License 5 votes vote down vote up
def ensure_docker_can_run() -> None:
    """
    :raises docker.errors.ContainerError
    :raises docker.errors.ImageNotFound
    :raises docker.errors.APIError
    """
    logger.info("checking docker can run")
    version = docker_client.version()["ApiVersion"]
    docker_client.containers.run("hello-world")
    logger.debug(f"using docker API version {version}") 
Example #30
Source File: dockerspawner.py    From dockerspawner with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def pull_image(self, image):
        """Pull the image, if needed

        - pulls it unconditionally if pull_policy == 'always'
        - otherwise, checks if it exists, and
          - raises if pull_policy == 'never'
          - pulls if pull_policy == 'ifnotpresent'
        """
        # docker wants to split repo:tag
        # the part split("/")[-1] allows having an image from a custom repo
        # with port but without tag. For example: my.docker.repo:51150/foo would not 
        # pass this test, resulting in image=my.docker.repo:51150/foo and tag=latest
        if ':' in image.split("/")[-1]:
            # rsplit splits from right to left, allowing to have a custom image repo with port
            repo, tag = image.rsplit(':', 1)
        else:
            repo = image
            tag = 'latest'

        if self.pull_policy.lower() == 'always':
            # always pull
            self.log.info("pulling %s", image)
            yield self.docker('pull', repo, tag)
            # done
            return
        try:
            # check if the image is present
            yield self.docker('inspect_image', image)
        except docker.errors.NotFound:
            if self.pull_policy == "never":
                # never pull, raise because there is no such image
                raise
            elif self.pull_policy == "ifnotpresent":
                # not present, pull it for the first time
                self.log.info("pulling image %s", image)
                yield self.docker('pull', repo, tag)