# Buggy code
def execute_dockerfile(args):
    """Execution path if given a dockerfile"""
    check_docker_daemon()
    logger.debug("Setting up...")
    report.setup(dockerfile=args.dockerfile)
    # attempt to build the image
    logger.debug("Building Docker image...")
    # placeholder to check if we can analyze the full image
    completed = True
    build, _ = dhelper.is_build()
    if build:
        # attempt to get built image metadata
        image_tag_string = dhelper.get_dockerfile_image_tag()
        full_image = report.load_full_image(image_tag_string)
        if full_image.origins.is_empty():
            # image loading was successful
            # Add an image origin here
            full_image.origins.add_notice_origin(
                formats.dockerfile_image.format(dockerfile=args.dockerfile)
            )
            # analyze image
            analyze(full_image, args, True)
        else:
            # we cannot load the full image
            logger.warning("Cannot retrieve full image metadata")
            completed = False
        # clean up image
        container.remove_image(full_image.repotag)
        if not args.keep_wd:
            report.clean_image_tars(full_image)
    else:
        # cannot build the image
        logger.warning("Cannot build image")
        completed = False
    # check if we have analyzed the full image or not
    if not completed:
        # get the base image
        logger.debug("Loading base image...")
        base_image = report.load_base_image()
        if base_image.origins.is_empty():
            # image loading was successful
            # add a notice stating failure to build image
            base_image.origins.add_notice_to_origins(
                args.dockerfile, Notice(formats.image_build_failure, "warning")
            )
            # analyze image
            analyze(base_image, args)
        else:
            # we cannot load the base image
            logger.warning("Cannot retrieve base image metadata")
        # run through commands in the Dockerfile
        logger.debug("Parsing Dockerfile to generate report...")
        stub_image = get_dockerfile_packages()
        if not args.keep_wd:
            report.clean_image_tars(base_image)
    # generate report based on what images were created
    if completed:
        report.report_out(args, full_image)
    else:
        report.report_out(args, base_image, stub_image)
    logger.debug("Teardown...")
    report.teardown()
    if not args.keep_wd:
        report.clean_working_dir(args.bind_mount)


# Traceback (FileNotFoundError)
(ternenv) browne@ubuntu18:~/ternenv/tern$ tern -l report -f output.txt -i debian:buster
2019-04-05 00:27:08,017 - DEBUG - __main__ - Starting...
2019-04-05 00:27:08,018 - DEBUG - report - Setting up...
2019-04-05 00:27:08,018 - DEBUG - container - Checking if image "debian:buster" is available on disk...
Traceback (most recent call last):
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/home/browne/ternenv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 964, in send
self.connect()
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, in connect
sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/browne/ternenv/bin/tern", line 11, in <module>
load_entry_point('tern==0.3.0', 'console_scripts', 'tern')()
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/__main__.py", line 111, in main
do_main(args)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/__main__.py", line 54, in do_main
report.execute_docker_image(args)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/report/report.py", line 422, in execute_docker_image
setup(image_tag_string=args.docker_image)
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/report/report.py", line 63, in setup
if not container.check_image(image_tag_string):
File "/home/browne/ternenv/lib/python3.6/site-packages/tern/utils/container.py", line 69, in check_image
client.images.get(image_tag_string)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/models/images.py", line 316, in get
return self.prepare_model(self.client.api.inspect_image(name))
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/api/image.py", line 245, in inspect_image
self._get(self._url("/images/{0}/json", image)), True
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/docker/api/client.py", line 225, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 546, in get
return self.request('GET', url, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/home/browne/ternenv/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
(ternenv) browne@ubuntu18:~/ternenv/tern$

# Fixed code
def execute_dockerfile(args):
    """Execution path if given a dockerfile"""
    container.check_docker_setup()
    logger.debug("Setting up...")
    report.setup(dockerfile=args.dockerfile)
    # attempt to build the image
    logger.debug("Building Docker image...")
    # placeholder to check if we can analyze the full image
    completed = True
    build, _ = dhelper.is_build()
    if build:
        # attempt to get built image metadata
        image_tag_string = dhelper.get_dockerfile_image_tag()
        full_image = report.load_full_image(image_tag_string)
        if full_image.origins.is_empty():
            # image loading was successful
            # Add an image origin here
            full_image.origins.add_notice_origin(
                formats.dockerfile_image.format(dockerfile=args.dockerfile)
            )
            # analyze image
            analyze(full_image, args, True)
        else:
            # we cannot load the full image
            logger.warning("Cannot retrieve full image metadata")
            completed = False
        # clean up image
        container.remove_image(full_image.repotag)
        if not args.keep_wd:
            report.clean_image_tars(full_image)
    else:
        # cannot build the image
        logger.warning("Cannot build image")
        completed = False
    # check if we have analyzed the full image or not
    if not completed:
        # get the base image
        logger.debug("Loading base image...")
        base_image = report.load_base_image()
        if base_image.origins.is_empty():
            # image loading was successful
            # add a notice stating failure to build image
            base_image.origins.add_notice_to_origins(
                args.dockerfile, Notice(formats.image_build_failure, "warning")
            )
            # analyze image
            analyze(base_image, args)
        else:
            # we cannot load the base image
            logger.warning("Cannot retrieve base image metadata")
        # run through commands in the Dockerfile
        logger.debug("Parsing Dockerfile to generate report...")
        stub_image = get_dockerfile_packages()
        if not args.keep_wd:
            report.clean_image_tars(base_image)
    # generate report based on what images were created
    if completed:
        report.report_out(args, full_image)
    else:
        report.report_out(args, base_image, stub_image)
    logger.debug("Teardown...")
    report.teardown()
    if not args.keep_wd:
        report.clean_working_dir(args.bind_mount)