# Buggy code
def do_main(args):
    """Execute according to subcommands"""
    # create working directory
    create_top_dir()
    if args.log_stream:
        # set up console logs
        global logger
        global console
        logger.addHandler(console)
    logger.debug("Starting...")
    if args.clear_cache:
        logger.debug("Clearing cache...")
        cache.clear()
    if hasattr(args, "name") and args.name == "report":
        if args.dockerfile:
            run.execute_dockerfile(args)
        if args.docker_image:
            if common.check_tar(args.docker_image):
                logger.error("%s", errors.incorrect_raw_option)
            else:
                run.execute_docker_image(args)
                logger.debug("Report completed.")
        if args.raw_image:
            if not common.check_tar(args.raw_image):
                logger.error(
                    "%s", errors.invalid_raw_image.format(image=args.raw_image)
                )
            else:
                run.execute_docker_image(args)
                logger.debug("Report completed.")
    logger.debug("Finished")


# 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 do_main(args):
    """Execute according to subcommands"""
    # set bind mount location if working in a container
    rootfs.set_mount_dir(args.bind_mount)
    # create working directory
    create_top_dir()
    if args.log_stream:
        # set up console logs
        global logger
        global console
        logger.addHandler(console)
    logger.debug("Starting...")
    if args.clear_cache:
        logger.debug("Clearing cache...")
        cache.clear()
    if hasattr(args, "name") and args.name == "report":
        if args.dockerfile:
            run.execute_dockerfile(args)
        if args.docker_image:
            if common.check_tar(args.docker_image):
                logger.error("%s", errors.incorrect_raw_option)
            else:
                run.execute_docker_image(args)
                logger.debug("Report completed.")
        if args.raw_image:
            if not common.check_tar(args.raw_image):
                logger.error(
                    "%s", errors.invalid_raw_image.format(image=args.raw_image)
                )
            else:
                run.execute_docker_image(args)
                logger.debug("Report completed.")
    logger.debug("Finished")