Building and Maintaining Containers for ML
This lesson explains how containers package code, libraries and runtime so training and inference behave the same in every environment, and how to build and maintain them.
Learning outcomes
After completing this lesson, you will be able to:
- Start from AWS Deep Learning Containers and framework images.
- Build images in CodeBuild and store them in ECR.
- Maintain images with Inspector scanning and rebuilds.
Start from AWS images
AWS publishes Deep Learning Containers and SageMaker AI framework images for PyTorch, TensorFlow, Hugging Face, scikit-learn, XGBoost and large model inference, with drivers, CUDA and performance libraries already configured and patched. The simplest maintainable approach is to extend one of these images with a Dockerfile that adds your libraries, rather than building a full image from scratch. When only Python packages are needed, script mode with a requirements.txt avoids building an image at all.
FROM 763104351884.dkr.ecr.us-east-1.amazonaws.com/pytorch-inference:2.3.0-gpu-py311-cu121-ubuntu20.04-sagemaker
RUN apt-get update && apt-get install -y --no-install-recommends libgl1 && rm -rf /var/lib/apt/lists/*
COPY requirements.txt /tmp/
RUN pip install --no-cache-dir -r /tmp/requirements.txt
COPY code/ /opt/ml/code/Building and storing images
Build images in AWS CodeBuild (privileged mode is required for Docker builds) as part of a pipeline, and push them to Amazon ECR. Use immutable tags, such as the Git commit hash, so a tag always refers to the same image, and reference images by tag or digest in model definitions. Lifecycle policies remove old untagged images. Cross-account repository policies let SageMaker AI in other accounts pull images, and the execution role needs ECR pull permissions.
Build an inference image
Maintaining images
Container images age: base images receive security patches, and frameworks release fixes. Amazon Inspector enhanced scanning finds vulnerabilities in images in Amazon ECR when they are pushed, and rescans them continuously as new vulnerabilities are published, so images already in use are re-evaluated.
Images should be rebuilt regularly from updated base images, with package versions pinned for reproducibility. They should be kept small, using multi-stage builds that leave build tools out of runtime images, which reduces both start-up time and the number of vulnerable packages. Images are tested in the pipeline before use. The following table summarises good practices.
| Practice | Purpose |
|---|---|
| Extend AWS framework images | Inherit tested drivers and optimisations |
| Immutable tags (commit hash) | Reproducible deployments |
| ECR lifecycle policy | Remove old images, control storage cost |
| Inspector enhanced scanning | Find vulnerabilities on push and continuously |
| Scheduled rebuilds | Pick up patched base images |
| Pinned package versions | Repeatable builds |
The following diagram shows the steps of fixing an inference container image that has a critical vulnerability.

Keep images healthy
A vulnerable inference image
Amazon Inspector reports a critical vulnerability in an OpenSSL library inside a custom inference image that has not been rebuilt for nine months.
- Update the Dockerfile's FROM line to the latest patched framework image version.
- Rebuild in CodeBuild, push with a new immutable tag and confirm Inspector shows no critical findings.
- Create a new model and endpoint configuration with the new image and update the endpoint with a canary deployment.
- Add a weekly scheduled rebuild and a pipeline gate that fails on critical findings.
Overwriting the existing tag with the rebuilt image would not update the running endpoint, which already pulled the old image, and would make it unclear which version is deployed.
What does CodeBuild need to build Docker images?
Show answer
Privileged mode.
Exam Tips
Remember for the exam
- Extend AWS Deep Learning Containers instead of building from scratch.
- Python packages only: requirements.txt in script mode, no custom image.
- CodeBuild needs privileged mode to build Docker images.
- Use immutable tags and ECR lifecycle policies.
- Scan images with Amazon Inspector and rebuild regularly.