# Buggy code
def execute_docker_image(args):
    """Execution path if given a Docker image"""
    logger.debug("Setting up...")
    image_string = args.docker_image
    if not args.raw_image:
        # don't check docker daemon for raw images
        container.check_docker_setup()
    else:
        image_string = args.raw_image
    report.setup(image_tag_string=image_string)
    # attempt to get built image metadata
    full_image = report.load_full_image(image_string)
    if full_image.origins.is_empty():
        # image loading was successful
        # Add an image origin here
        full_image.origins.add_notice_origin(
            formats.docker_image.format(imagetag=image_string)
        )
        # analyze image
        analyze(full_image, args)
        # generate report
        report.report_out(args, full_image)
    else:
        # we cannot load the full image
        logger.warning("Cannot retrieve full image metadata")
    if not args.keep_wd:
        report.clean_image_tars(full_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_docker_image(args):
    """Execution path if given a Docker image"""
    logger.debug("Setting up...")
    image_string = args.docker_image
    if not args.raw_image:
        # don't check docker daemon for raw images
        container.check_docker_setup()
    else:
        image_string = args.raw_image
    report.setup(image_tag_string=image_string)
    # attempt to get built image metadata
    full_image = report.load_full_image(image_string)
    if full_image.origins.is_empty():
        # image loading was successful
        # Add an image origin here
        full_image.origins.add_notice_origin(
            formats.docker_image.format(imagetag=image_string)
        )
        # analyze image
        analyze(full_image, args)
        # generate report
        report.report_out(args, full_image)
    else:
        # we cannot load the full image
        logger.warning("Cannot retrieve full image metadata")
    if not args.keep_wd:
        report.clean_image_tars(full_image)
    logger.debug("Teardown...")
    report.teardown()
    if not args.keep_wd:
        report.clean_working_dir()