# Buggy 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)


# Traceback (subprocess.CalledProcessError)
docker build -t ternd .
./docker_run.sh workdir ternd "report -i golang:alpine"
-------------------------OUTPUT-------------------------
Traceback (most recent call last):
File "/usr/bin/tern", line 8, in <module>
sys.exit(main())
File "/usr/lib/python3.7/site-packages/tern/__main__.py", line 167, in main
do_main(args)
File "/usr/lib/python3.7/site-packages/tern/__main__.py", line 90, in do_main
run.execute_docker_image(args)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/run.py", line 88, in execute_docker_image
analyze(full_image, args)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/run.py", line 67, in analyze
analyze_docker_image(image_obj, args.redo, is_dockerfile)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 39, in analyze_docker_image
analyze_subsequent_layers(image_obj, shell, master_list, redo)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 133, in analyze_subsequent_layers
target = mount_overlay_fs(image_obj, curr_layer)
File "/usr/lib/python3.7/site-packages/tern/analyze/docker/analyze.py", line 178, in mount_overlay_fs
target = rootfs.mount_diff_layers(tar_layers)
File "/usr/lib/python3.7/site-packages/tern/utils/rootfs.py", line 196, in mount_diff_layers
root_command(union_mount, args, merge_dir_path)
File "/usr/lib/python3.7/site-packages/tern/utils/rootfs.py", line 66, in root_command
1, cmd=full_cmd, output=error)
subprocess.CalledProcessError: Command '['mount', '-t', 'overlay', 'overlay', '-o', 'lowerdir=/root/.tern/temp/8e635d6264340a45901f63d2a18ea5bc8c680919e07191e4ef276860952d0399/contents,upperdir=/root/.tern/temp/1332c5c5bbaa26b26053d13d13b81dd2f523a2217e01f6edfe38fe107c091859/contents,workdir=/root/.tern/temp/workdir', '/root/.tern/temp/mergedir']' returned non-zero exit status 1.
Traceback (most recent call last):
File "ci/test_files_touched.py", line 130, in <module>
subprocess.check_output(t, shell=True)  # nosec
File "/opt/circleci/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/opt/circleci/.pyenv/versions/3.6.5/lib/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command './docker_run.sh workdir ternd "report -i golang:alpine"' returned non-zero exit status 1.
Exited with code 1

# 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()