build¶
Build a Docker image for SageMaker training and deployment.
Synopsis¶
Description¶
The build command creates a Docker image containing your machine learning code, dependencies, and the necessary entry points for SageMaker training and serving. The image is built locally and tagged for use with both local testing and cloud deployment.
The build process:
- Validates the easy_sm directory structure
- Sets executable permissions on entry point scripts
- Builds a Docker image with your code and dependencies
- Tags the image as
{image_name}:{docker_tag}
Options¶
| Option | Short | Type | Required | Default | Description |
|---|---|---|---|---|---|
--app-name | -a | string | No | Auto-detected | App name for configuration. If not specified, auto-detects from *.json file in current directory |
--docker-tag | -t | string | No | latest | Tag for the Docker image. This is a global option that must come before the command |
Examples¶
Basic build¶
Build with auto-detected app name and default tag:
Build with specific app name¶
Override auto-detection:
Build with custom Docker tag¶
Use a specific version tag:
Or:
Build with custom tag and app name¶
Build for different environments¶
# Development
easy_sm -t dev build
# Staging
easy_sm -t staging build
# Production
easy_sm -t v1.0.0 build
Output¶
During the build, you'll see:
Started building SageMaker Docker image. It will take some minutes...
[Docker build output...]
Docker image built successfully!
The command creates a Docker image named {image_name}:{docker_tag} that you can verify with:
Expected output:
Prerequisites¶
- Docker installed and running
- A valid easy_sm project (created with
easy_sm init) - Configuration file (
{app_name}.json) in current directory - Required project structure with
easy_sm_base/directory
Build Process Details¶
The build command:
- Validates structure: Checks for required files:
build.shtraining/trainprediction/serveexecutor.sh-
Dockerfile -
Sets permissions: Makes scripts executable (755):
training/trainprediction/serve-
executor.sh -
Builds image: Runs Docker build with:
- Your source code
- Python version from config
- Dependencies from requirements.txt
- SageMaker entry points
What Gets Included¶
The Docker image contains:
- Python runtime (version specified in config)
- Your code from the easy_sm module directory
- Dependencies from requirements.txt
- Training entry point (
training/train) - Serving entry point (
prediction/serve) - Processing scripts from
processing/directory - SageMaker libraries (boto3, sagemaker, etc.)
Dockerfile Customization¶
You can customize the build by editing {app_name}/easy_sm_base/Dockerfile:
# Example: Add system packages
RUN apt-get update && apt-get install -y \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
# Example: Set environment variables
ENV MODEL_PATH=/opt/ml/model
# Example: Add custom build steps
COPY custom_config/ /opt/config/
After customizing the Dockerfile, run easy_sm build to rebuild the image.
Build Context¶
The build uses the following from your configuration:
image_name: Base name for the Docker imagepython_version: Python runtime version (3.10, 3.11, 3.12, or 3.13)easy_sm_module_dir: Source code locationrequirements_dir: Path to requirements.txt
Troubleshooting¶
"This is not a easy_sm directory" error¶
Problem: Missing required files in the project structure.
Solution: Ensure you have: - Initialized the project with easy_sm init - Required files exist in {app_name}/easy_sm_base/
"No such file or directory" during build¶
Problem: requirements.txt not found at specified location.
Solution: Check the requirements_dir in your config file:
Ensure the file exists:
Docker daemon not running¶
Problem: Cannot connect to the Docker daemon
Solution: Start Docker:
Permission denied errors¶
Problem: Permission errors when building.
Solution: Ensure Docker has proper permissions:
Build fails with "No space left on device"¶
Problem: Docker has filled up available disk space.
Solution: Clean up Docker:
# Remove unused images and containers
docker system prune -a
# Remove all images for your app
docker rmi $(docker images my-ml-app -q)
Requirements installation fails¶
Problem: Dependencies fail to install during build.
Solution: 1. Test requirements locally first:
2. Check for version conflicts or platform-specific packages 3. Pin versions in requirements.txtBuild Caching¶
Docker caches build layers for faster rebuilds. To force a complete rebuild:
Or use Docker's --no-cache by modifying the build script temporarily.
Performance Tips¶
- Order dependencies: Place stable dependencies before frequently-changing code in Dockerfile
- Use specific tags: Helps identify and deploy specific versions
- Minimize image size: Remove unnecessary packages and files
- Multi-stage builds: Use multi-stage Dockerfiles for smaller production images
Related Commands¶
init- Initialize a new projectpush- Push the built image to ECRlocal train- Test the built image locallytrain- Train on SageMaker using the imageupdate-scripts- Update build scripts