def _prepare_docker_command(self, options, run_mode, raw_arguments): """ Building a docker command line for running the model inside the docker - this command line can be used by the user independently of drum. Parameters Returns: docker command line to run as a string """ options.docker = self._maybe_build_image(options.docker) in_docker_model = "/opt/model" in_docker_input_file = "/opt/input.csv" in_docker_output_file = "/opt/output.csv" in_docker_fit_output_dir = "/opt/fit_output_dir" in_docker_fit_target_filename = "/opt/fit_target.csv" in_docker_fit_row_weights_filename = "/opt/fit_row_weights.csv" docker_cmd = "docker run --rm --interactive --user $(id -u):$(id -g) " docker_cmd_args = " -v {}:{}".format(options.code_dir, in_docker_model) in_docker_cmd_list = raw_arguments in_docker_cmd_list[0] = ArgumentsOptions.MAIN_COMMAND in_docker_cmd_list[1] = run_mode.value CMRunnerUtils.delete_cmd_argument(in_docker_cmd_list, ArgumentsOptions.DOCKER) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.CODE_DIR, in_docker_model) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, "-cd", in_docker_model) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.INPUT, in_docker_input_file) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_output_file) if run_mode == RunMode.SERVER: host_port_list = options.address.split(":", 1) if len(host_port_list) == 1: raise DrumCommonException( "Error: when using the docker option provide argument --server host:port" ) port = int(host_port_list[1]) host_port_inside_docker = "{}:{}".format("0.0.0.0", port) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.ADDRESS, host_port_inside_docker) docker_cmd_args += " -p {port}:{port}".format(port=port) if run_mode in [ RunMode.SCORE, RunMode.PERF_TEST, RunMode.VALIDATION, RunMode.FIT ]: docker_cmd_args += " -v {}:{}".format(options.input, in_docker_input_file) if run_mode == RunMode.SCORE and options.output: output_file = os.path.realpath(options.output) if not os.path.exists(output_file): # Creating an empty file so the mount command will mount the file correctly - # otherwise docker create an empty directory open(output_file, "a").close() docker_cmd_args += " -v {}:{}".format(output_file, in_docker_output_file) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_output_file) elif run_mode == RunMode.FIT: if options.output: fit_output_dir = os.path.realpath(options.output) docker_cmd_args += " -v {}:{}".format( fit_output_dir, in_docker_fit_output_dir) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_fit_output_dir) if options.target_csv: fit_target_filename = os.path.realpath(options.target_csv) docker_cmd_args += " -v {}:{}".format( fit_target_filename, in_docker_fit_target_filename) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.TARGET_FILENAME, in_docker_fit_target_filename, ) if options.row_weights_csv: fit_row_weights_filename = os.path.realpath( options.row_weights_csv) docker_cmd_args += " -v {}:{}".format( fit_row_weights_filename, in_docker_fit_row_weights_filename) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.WEIGHTS_CSV, in_docker_fit_row_weights_filename, ) docker_cmd += " {} {} {}".format(docker_cmd_args, options.docker, " ".join(in_docker_cmd_list)) self._print_verbose("docker command: [{}]".format(docker_cmd)) return docker_cmd
def _prepare_docker_command(self, options, run_mode, raw_arguments): """ Building a docker command line for running the model inside the docker - this command line can be used by the user independently of drum. Parameters Returns: docker command line to run as a string """ options.docker = self._maybe_build_image(options.docker) in_docker_model = "/opt/model" in_docker_input_file = "/opt/input.csv" in_docker_output_file = "/opt/output.csv" in_docker_fit_output_dir = "/opt/fit_output_dir" in_docker_fit_target_filename = "/opt/fit_target.csv" in_docker_fit_row_weights_filename = "/opt/fit_row_weights.csv" docker_cmd = "docker run --rm --entrypoint '' --interactive --user $(id -u):$(id -g)" docker_cmd_args = ' -v "{}":{}'.format(options.code_dir, in_docker_model) in_docker_cmd_list = raw_arguments in_docker_cmd_list[0] = ArgumentsOptions.MAIN_COMMAND in_docker_cmd_list[1] = run_mode.value # [RAPTOR-5607] Using -cd makes fit fail within docker, but not --code-dir. # Hotfix it by replacing -cd with --code-dir in_docker_cmd_list = [ ArgumentsOptions.CODE_DIR if arg == "-cd" else arg for arg in in_docker_cmd_list ] CMRunnerUtils.delete_cmd_argument(in_docker_cmd_list, ArgumentsOptions.DOCKER) CMRunnerUtils.delete_cmd_argument(in_docker_cmd_list, ArgumentsOptions.SKIP_DEPS_INSTALL) if options.memory: docker_cmd_args += " --memory {mem_size} --memory-swap {mem_size} ".format( mem_size=options.memory) CMRunnerUtils.delete_cmd_argument(in_docker_cmd_list, ArgumentsOptions.MEMORY) if options.class_labels and ArgumentsOptions.CLASS_LABELS not in in_docker_cmd_list: CMRunnerUtils.delete_cmd_argument( in_docker_cmd_list, ArgumentsOptions.CLASS_LABELS_FILE) in_docker_cmd_list.append(ArgumentsOptions.CLASS_LABELS) for label in options.class_labels: in_docker_cmd_list.append(label) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.CODE_DIR, in_docker_model) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, "-cd", in_docker_model) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.INPUT, in_docker_input_file) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_output_file) if run_mode == RunMode.SERVER: host_port_list = options.address.split(":", 1) if len(host_port_list) == 1: raise DrumCommonException( "Error: when using the docker option provide argument --server host:port" ) port = int(host_port_list[1]) host_port_inside_docker = "{}:{}".format("0.0.0.0", port) CMRunnerUtils.replace_cmd_argument_value(in_docker_cmd_list, ArgumentsOptions.ADDRESS, host_port_inside_docker) docker_cmd_args += " -p {port}:{port}".format(port=port) if run_mode in [ RunMode.SCORE, RunMode.PERF_TEST, RunMode.VALIDATION, RunMode.FIT ]: docker_cmd_args += ' -v "{}":{}'.format(options.input, in_docker_input_file) if run_mode == RunMode.SCORE and options.output: output_file = os.path.realpath(options.output) if not os.path.exists(output_file): # Creating an empty file so the mount command will mount the file correctly - # otherwise docker create an empty directory open(output_file, "a").close() docker_cmd_args += ' -v "{}":{}'.format( output_file, in_docker_output_file) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_output_file) elif run_mode == RunMode.FIT: if options.output: fit_output_dir = os.path.realpath(options.output) docker_cmd_args += ' -v "{}":{}'.format( fit_output_dir, in_docker_fit_output_dir) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.OUTPUT, in_docker_fit_output_dir) if options.target_csv: fit_target_filename = os.path.realpath(options.target_csv) docker_cmd_args += ' -v "{}":{}'.format( fit_target_filename, in_docker_fit_target_filename) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.TARGET_CSV, in_docker_fit_target_filename, ) if options.row_weights_csv: fit_row_weights_filename = os.path.realpath( options.row_weights_csv) docker_cmd_args += ' -v "{}":{}'.format( fit_row_weights_filename, in_docker_fit_row_weights_filename) CMRunnerUtils.replace_cmd_argument_value( in_docker_cmd_list, ArgumentsOptions.WEIGHTS_CSV, in_docker_fit_row_weights_filename, ) docker_cmd += " {} {} {}".format(docker_cmd_args, options.docker, " ".join(in_docker_cmd_list)) self._print_verbose("docker command: [{}]".format(docker_cmd)) return docker_cmd