# Buggy code
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        prog="Tern",
        description="""
    Tern is a container image component curation tool. Tern retrieves
    information about packages that are installed in a container image.
    Learn more at https://github.com/vmware/tern""",
    )
    parser.add_argument(
        "-l",
        "--log-stream",
        action="store_true",
        help="Stream logs to the console; Useful when running in a shell",
    )
    parser.add_argument(
        "-c",
        "--clear-cache",
        action="store_true",
        help="Clear the cache before running",
    )
    parser.add_argument(
        "-k",
        "--keep-wd",
        action="store_true",
        help="Keep the working directory after execution."
        " Useful when debugging container images",
    )
    parser.add_argument(
        "-b",
        "--bind-mount",
        action="store_true",
        help="Treat working directory as a bind mount."
        " Needed when running from within a container",
    )
    parser.add_argument(
        "-r",
        "--redo",
        action="store_true",
        help="Repopulate the cache for found layers",
    )
    # sys.version gives more information than we care to print
    py_ver = sys.version.replace("\n", "").split("[")[0]
    parser.add_argument(
        "-v",
        "--version",
        action="version",
        version="{ver_str}\n   python version = {py_v}".format(
            ver_str=get_version(), py_v=py_ver
        ),
    )
    subparsers = parser.add_subparsers(help="Subcommands")
    # subparser for report
    parser_report = subparsers.add_parser(
        "report",
        help="Create a BoM report. Run 'tern report -h' for report format options.",
    )
    parser_report.add_argument(
        "-d",
        "--dockerfile",
        type=check_file_existence,
        help="Dockerfile used to build the Docker image",
    )
    parser_report.add_argument(
        "-i",
        "--docker-image",
        help="Docker image that exists locally -"
        " image:tag"
        " The option can be used to pull docker"
        " images by digest as well -"
        " <repo>@<digest-type>:<digest>",
    )
    parser_report.add_argument(
        "-w",
        "--raw-image",
        metavar="FILE",
        help="Raw container image that exists locally in the form of a tar archive.",
    )
    parser_report.add_argument(
        "-x",
        "--extend",
        metavar="EXTENSION",
        help="Use an extension to analyze a container "
        "image. Available extensions: cve-bin-tool",
    )
    parser_report.add_argument(
        "-f",
        "--report-format",
        metavar="REPORT_FORMAT",
        help="Format the report using one of the "
        "available formats: "
        "spdxtagvalue, json, yaml",
    )
    parser_report.add_argument(
        "-o",
        "--output-file",
        default=None,
        metavar="FILE",
        help="Write the report to a file. "
        "If no file is given the default file in "
        "utils/constants.py will be used",
    )
    parser_report.set_defaults(name="report")
    args = parser.parse_args()

    # execute
    if sys.version_info < (3, 0):
        sys.stderr.write(
            "Error running Tern. Please check that python3 is configured as default.\n"
        )
    else:
        do_main(args)


# 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 main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        prog="Tern",
        description="""
    Tern is a container image component curation tool. Tern retrieves
    information about packages that are installed in a container image.
    Learn more at https://github.com/vmware/tern""",
    )
    parser.add_argument(
        "-l",
        "--log-stream",
        action="store_true",
        help="Stream logs to the console; Useful when running in a shell",
    )
    parser.add_argument(
        "-c",
        "--clear-cache",
        action="store_true",
        help="Clear the cache before running",
    )
    parser.add_argument(
        "-k",
        "--keep-wd",
        action="store_true",
        help="Keep the working directory after execution."
        " Useful when debugging container images",
    )
    parser.add_argument(
        "-b",
        "--bind-mount",
        metavar="BIND_DIR",
        help="Absolute path to bind mount target. Needed"
        " when running from within a container.",
    )
    parser.add_argument(
        "-r",
        "--redo",
        action="store_true",
        help="Repopulate the cache for found layers",
    )
    # sys.version gives more information than we care to print
    py_ver = sys.version.replace("\n", "").split("[")[0]
    parser.add_argument(
        "-v",
        "--version",
        action="version",
        version="{ver_str}\n   python version = {py_v}".format(
            ver_str=get_version(), py_v=py_ver
        ),
    )
    subparsers = parser.add_subparsers(help="Subcommands")
    # subparser for report
    parser_report = subparsers.add_parser(
        "report",
        help="Create a BoM report. Run 'tern report -h' for report format options.",
    )
    parser_report.add_argument(
        "-d",
        "--dockerfile",
        type=check_file_existence,
        help="Dockerfile used to build the Docker image",
    )
    parser_report.add_argument(
        "-i",
        "--docker-image",
        help="Docker image that exists locally -"
        " image:tag"
        " The option can be used to pull docker"
        " images by digest as well -"
        " <repo>@<digest-type>:<digest>",
    )
    parser_report.add_argument(
        "-w",
        "--raw-image",
        metavar="FILE",
        help="Raw container image that exists locally in the form of a tar archive.",
    )
    parser_report.add_argument(
        "-x",
        "--extend",
        metavar="EXTENSION",
        help="Use an extension to analyze a container "
        "image. Available extensions: cve-bin-tool",
    )
    parser_report.add_argument(
        "-f",
        "--report-format",
        metavar="REPORT_FORMAT",
        help="Format the report using one of the "
        "available formats: "
        "spdxtagvalue, json, yaml",
    )
    parser_report.add_argument(
        "-o",
        "--output-file",
        default=None,
        metavar="FILE",
        help="Write the report to a file. "
        "If no file is given the default file in "
        "utils/constants.py will be used",
    )
    parser_report.set_defaults(name="report")
    args = parser.parse_args()

    # execute
    if sys.version_info < (3, 0):
        sys.stderr.write(
            "Error running Tern. Please check that python3 is configured as default.\n"
        )
    else:
        do_main(args)