Managing Environments
This lesson explains what an environment contains, how curated and custom environments differ, how versions keep runs reproducible, and how images are built in private workspaces.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the parts of an environment definition.
- Distinguish curated from custom environments.
- Explain why environment and package versions are pinned.
- Identify the build compute a private workspace needs.
What an environment contains
An environment specifies everything a script needs besides its code: the operating system and system libraries, the Python version, and the Python packages. It is defined by a Docker image, optionally with a conda file of packages added on top, or by a Docker build context, a folder with a Dockerfile and supporting files.
# env.yml
$schema: https://azuremlschemas.azureedge.net/latest/environment.schema.json
name: train-env
version: 2
image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu22.04:latest
conda_file: conda.yml
description: Training environment for churn models# conda.yml
name: train
channels:
- conda-forge
dependencies:
- python=3.11
- pip
- pip:
- scikit-learn==1.5.2
- pandas==2.2.3
- mlflow==2.17.2
- azureml-mlflow==1.59.0Curated and custom environments
Curated environments are published by Microsoft in the azureml registry for common frameworks such as scikit-learn, PyTorch and TensorFlow, with images that are already built and cached, so jobs that use them start faster. They are referenced as azureml://registries/azureml/environments/NAME/labels/latest or by version, and listed with az ml environment list --registry-name azureml.
Custom environments are created when a project needs packages or versions that curated environments do not provide. When a job first uses one, Azure Machine Learning builds its image in the workspace's container registry and caches it; later jobs with the same definition reuse the image. A common approach is to start from a curated environment's image and add the project's packages.
Versioning and reproducibility
Environments are versioned assets, and a job records the version it used. Two practices make them reproducible. First, pin package versions in the conda file, because an unpinned scikit-learn resolves to whatever is newest when the image is built, which can change behaviour or break the code. Second, pin the environment version in job and deployment definitions rather than using @latest in production.
Using the same environment for training and inference, or an inference environment with the same versions of the model's libraries, prevents a common failure in which a model serialized with one library version cannot be loaded by another. MLflow models record their dependencies for this reason, and no-code deployment builds a matching environment from them.
Image builds and private workspaces
Environment images are built by an image build service, by default on Azure-managed infrastructure. In a workspace whose container registry is behind a private endpoint, that service cannot reach the registry, and builds fail with an ImageBuildFailure error. The fix is to set an image build compute, a compute cluster in the virtual network, with az ml workspace update --image-build-compute or the corresponding workspace setting, or to use serverless compute for image builds where the managed network supports it.
Other build failures come from the definition itself: packages that conflict, a package that does not exist for the chosen Python version, or a base image that cannot be pulled. The build log, available from the job's details, shows the failing step.
An environment that breaks deployments
A model trained three months ago deploys successfully, but a model trained yesterday with the same code fails when the online deployment loads it, with an error about an incompatible scikit-learn pickle. The training environment's conda file lists scikit-learn without a version, and the inference environment pins scikit-learn==1.5.2.
The training environment's image was rebuilt recently and picked up a newer scikit-learn, so the new model was serialized with a version the inference environment cannot read. The fix is to pin scikit-learn in the training conda file to the version used for inference, create a new environment version, retrain, and reference that version explicitly in the training job. Registering the model as an MLflow model would also record its dependencies for deployment.
Upgrading the inference environment might fix this model but would break the older one. Retrying the deployment would fail the same way. Removing the version pin from the inference environment would make deployments unpredictable.
Training used scikit-learn 1.5 but the deployment's environment installs the latest version, and predictions fail. What practice prevents this?
Show answer
Pinning package and environment versions and using the same, or a matching, versioned environment for training and inference.
Exam Tips
Remember for the exam
- An environment is a Docker image plus an optional conda file, or a Docker build context.
- Curated environments live in the azureml registry and start fastest.
- Pin package and environment versions; use matching versions for training and inference.
- A private workspace needs image build compute for custom environment builds.