0
0
MLOpsdevops~30 mins

Multi-stage builds for smaller images in MLOps - Mini Project: Build & Apply

Choose your learning style9 modes available
Multi-stage builds for smaller images
📖 Scenario: You are working on a machine learning project that requires a Docker image to run your model training script. You want to keep the final Docker image small to save storage and speed up deployment.Multi-stage builds in Docker help you do this by letting you use one image to build your code and another smaller image to run it.
🎯 Goal: Build a Dockerfile using multi-stage builds that first installs all dependencies and copies your training script, then creates a smaller final image that only contains the necessary runtime files.
📋 What You'll Learn
Create a first build stage named builder using the python:3.12-slim image
In the builder stage, copy train.py and install scikit-learn
Create a second stage named runtime using the python:3.12-alpine image
Copy only the train.py file from the builder stage to the runtime stage
Set the default command to run python train.py in the runtime stage
💡 Why This Matters
🌍 Real World
Multi-stage builds are used in real projects to reduce Docker image size, which saves bandwidth and speeds up deployment.
💼 Career
Understanding multi-stage builds is important for DevOps and MLOps roles to optimize containerized machine learning workflows.
Progress0 / 4 steps
1
Create the builder stage
Write a Dockerfile line to start a build stage named builder using the image python:3.12-slim.
MLOps
Need a hint?

Use the FROM instruction with AS builder to name the stage.

2
Copy train.py and install dependencies in builder
In the builder stage, add lines to copy train.py into the image and install the scikit-learn package using pip.
MLOps
Need a hint?

Use COPY train.py /app/train.py and RUN pip install scikit-learn.

3
Create the runtime stage and copy train.py
Start a new stage named runtime using python:3.12-alpine. Then copy train.py from the builder stage to /app/train.py in this stage.
MLOps
Need a hint?

Use FROM python:3.12-alpine AS runtime and COPY --from=builder /app/train.py /app/train.py.

4
Set the default command to run train.py
In the runtime stage, add a line to set the default command to python /app/train.py using the CMD instruction.
MLOps
Need a hint?

Use CMD ["python", "/app/train.py"] to set the default command.