# Buggy code
def _create_dockerfile(output_path, mlflow_path=None):
    """
    Creates a Dockerfile containing additional Docker build steps to execute
    when building the Azure container image. These build steps perform the following tasks:

    - Install MLflow

    :param output_path: The path where the Dockerfile will be written.
    :param mlflow_path: Path to a local copy of the MLflow GitHub repository. If specified, the
                        Dockerfile command for MLflow installation will install MLflow from this
                        directory. Otherwise, it will install MLflow from pip.
    """
    docker_cmds = ["RUN pip install azureml-sdk"]

    if mlflow_path is not None:
        mlflow_install_cmd = "RUN pip install -e {mlflow_path}".format(
            mlflow_path=_get_container_path(mlflow_path)
        )
    elif not mlflow_version.endswith("dev"):
        mlflow_install_cmd = "RUN pip install mlflow=={mlflow_version}".format(
            mlflow_version=mlflow_version
        )
    else:
        raise MlflowException(
            "You are running a 'dev' version of MLflow: `{mlflow_version}` that cannot be"
            " installed from pip. In order to build a container image, either specify the"
            " path to a local copy of the MLflow GitHub repository using the `mlflow_home`"
            " parameter or install a release version of MLflow from pip".format(
                mlflow_version=mlflow_version
            )
        )
    docker_cmds.append(mlflow_install_cmd)

    with open(output_path, "w") as f:
        f.write("\n".join(docker_cmds))


# Traceback (Exception)
2019-08-16T16:43:37,939683552+00:00 - iot-server/run
2019-08-16T16:43:37,940440658+00:00 - gunicorn/run
2019-08-16T16:43:37,941200563+00:00 - rsyslog/run
2019-08-16T16:43:38,137124526+00:00 - nginx/run
EdgeHubConnectionString and IOTEDGE_IOTHUBHOSTNAME are not set. Exiting...
2019-08-16T16:43:40,241560271+00:00 - iot-server/finish 1 0
2019-08-16T16:43:40,245790300+00:00 - Exit code 1 is normal. Not restarting iot-server.
Starting gunicorn 19.6.0
Listening at: http://127.0.0.1:31311 (11)
Using worker: sync
worker timeout is set to 300
Booting worker with pid: 43
Initializing logger
Starting up app insights client
Starting up request id generator
Starting up app insight hooks
Invoking user's init function
2019-08-16 16:44:36,744 | azureml.core.run | DEBUG | Could not load run context RunEnvironmentException:
Message: Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run.
InnerException None
ErrorResponse {"error": {"message": "Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run."}}, switching offline: False
2019-08-16 16:44:36,744 | azureml.core.run | DEBUG | Could not load the run context and allow_offline set to False
2019-08-16 16:44:36,744 | azureml.core.model | DEBUG | RunEnvironmentException: RunEnvironmentException:
Message: Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run.
InnerException RunEnvironmentException:
Message: Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run.
InnerException None
ErrorResponse {"error": {"message": "Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run."}}
ErrorResponse {"error": {"message": "Could not load a submitted run, if outside of an execution context, use experiment.start_logging to initialize an azureml.core.Run."}}
2019-08-16 16:44:36,745 | azureml.core.model | DEBUG | Using passed in version 1
2019-08-16 16:44:36,745 | azureml.core.model | DEBUG | Found model path at azureml-models/mlflow-y4ct2veitj6uszrg9sjjfq/1/model2
2019/08/16 16:44:36 WARNING mlflow.pyfunc: The version of Python that the model was saved in, `Python 3.7.3`, differs from the version of Python that is currently running, `Python 3.6.8`, and may be incompatible
JAVA_HOME is not set
User's init function failed
Encountered Exception Traceback (most recent call last):
File "/var/azureml-server/aml_blueprint.py", line 162, in register
main.init()
File "/var/azureml-app/main.py", line 88, in init
driver_module.init()
File "execution_script.py", line 12, in init
model = load_pyfunc(model_path)
File "/opt/miniconda/lib/python3.6/site-packages/mlflow/pyfunc/__init__.py", line 314, in load_pyfunc
return importlib.import_module(conf[MAIN])._load_pyfunc(data_path)
File "/opt/miniconda/lib/python3.6/site-packages/mlflow/spark.py", line 383, in _load_pyfunc
.master("local[1]").getOrCreate()
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/sql/session.py", line 173, in getOrCreate
sc = SparkContext.getOrCreate(sparkConf)
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/context.py", line 367, in getOrCreate
SparkContext(conf=conf or SparkConf())
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/context.py", line 133, in __init__
SparkContext._ensure_initialized(self, gateway=gateway, conf=conf)
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/context.py", line 316, in _ensure_initialized
SparkContext._gateway = gateway or launch_gateway(conf)
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/java_gateway.py", line 46, in launch_gateway
return _launch_gateway(conf)
File "/opt/miniconda/lib/python3.6/site-packages/pyspark/java_gateway.py", line 108, in _launch_gateway
raise Exception("Java gateway process exited before sending its port number")
Exception: Java gateway process exited before sending its port number
Worker exiting (pid: 43)
Shutting down: Master
Reason: Worker failed to boot.
2019-08-16T16:44:41,539825892+00:00 - gunicorn/finish 3 0
2019-08-16T16:44:41,541275102+00:00 - Exit code 3 is not normal. Killing image.

# Fixed code
def _create_dockerfile(output_path, mlflow_path=None):
    """
    Creates a Dockerfile containing additional Docker build steps to execute
    when building the Azure container image. These build steps perform the following tasks:

    - Install MLflow

    :param output_path: The path where the Dockerfile will be written.
    :param mlflow_path: Path to a local copy of the MLflow GitHub repository. If specified, the
                        Dockerfile command for MLflow installation will install MLflow from this
                        directory. Otherwise, it will install MLflow from pip.
    """
    docker_cmds = ["RUN apt-get update && apt-get install -y default-jre"]
    docker_cmds.append("RUN pip install azureml-sdk")

    if mlflow_path is not None:
        mlflow_install_cmd = "RUN pip install -e {mlflow_path}".format(
            mlflow_path=_get_container_path(mlflow_path)
        )
    elif not mlflow_version.endswith("dev"):
        mlflow_install_cmd = "RUN pip install mlflow=={mlflow_version}".format(
            mlflow_version=mlflow_version
        )
    else:
        raise MlflowException(
            "You are running a 'dev' version of MLflow: `{mlflow_version}` that cannot be"
            " installed from pip. In order to build a container image, either specify the"
            " path to a local copy of the MLflow GitHub repository using the `mlflow_home`"
            " parameter or install a release version of MLflow from pip".format(
                mlflow_version=mlflow_version
            )
        )
    docker_cmds.append(mlflow_install_cmd)

    with open(output_path, "w") as f:
        f.write("\n".join(docker_cmds))