0
0
MLOpsdevops~10 mins

Docker for ML workloads in MLOps - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Docker for ML workloads
Write Dockerfile
Build Docker Image
Run Container with ML Code
Container Executes ML Task
Output Results / Logs
Stop Container / Save Model
This flow shows how you create a Docker image for ML, run it as a container to execute ML tasks, and then get results or save models.
Execution Sample
MLOps
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["python", "train.py"]
This Dockerfile sets up a Python environment, installs ML dependencies, copies code, and runs training.
Process Table
StepActionDetailsResult
1Read DockerfileFROM python:3.12-slimBase image set to Python 3.12 slim
2Set working directoryWORKDIR /appWorking directory inside container is /app
3Copy requirements.txtCOPY requirements.txt ./requirements.txt copied to /app
4Install dependenciesRUN pip install -r requirements.txtPython packages installed
5Copy source codeCOPY . ./All local files copied to /app
6Set commandCMD ["python", "train.py"]Container will run train.py on start
7Build imagedocker build -t ml-train .Docker image 'ml-train' created
8Run containerdocker run ml-trainContainer starts and runs training script
9Training executestrain.py runs ML trainingModel trains and outputs logs
10Container stopsTraining completesContainer exits after job done
💡 Training script finishes, container stops automatically
Status Tracker
VariableStartAfter Step 4After Step 8Final
Docker ImageNoneCreated with Python and dependenciesUsed to start containerImage remains for reuse
Container StateNot runningNot runningRunning training scriptStopped after training
Model OutputNoneNoneBeing generatedSaved or logged after training
Key Moments - 3 Insights
Why do we copy requirements.txt and run pip install before copying all code?
Because installing dependencies first uses Docker cache efficiently, so if code changes but dependencies don't, Docker skips reinstalling packages (see execution_table steps 3 and 4).
What happens when the container finishes running the training script?
The container stops automatically after the CMD command completes (see execution_table step 10). You must save outputs outside the container if needed.
Why use a slim Python base image?
A slim image is smaller and faster to download, making builds and deployments quicker without unnecessary files (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the Docker image created?
AStep 9
BStep 4
CStep 7
DStep 2
💡 Hint
Check the 'Result' column for the step mentioning 'Docker image created'
At which step does the container start running the ML training script?
AStep 6
BStep 8
CStep 10
DStep 3
💡 Hint
Look for the step where the container 'starts and runs training script'
If you change only the source code but not requirements.txt, which step's action can Docker skip during rebuild?
AInstall dependencies
BCopy source code
CCopy requirements.txt
DSet working directory
💡 Hint
Refer to key moment about Docker cache and execution_table steps 3 and 4
Concept Snapshot
Docker for ML workloads:
- Write Dockerfile with base Python image
- Copy requirements.txt and install dependencies first
- Copy ML code and set CMD to run training
- Build image with 'docker build'
- Run container with 'docker run' to execute ML task
- Container stops when training finishes
- Save outputs outside container if needed
Full Transcript
This visual execution shows how Docker helps run machine learning workloads. First, you write a Dockerfile starting from a Python base image. Then you copy the requirements.txt file and install dependencies to use Docker's cache efficiently. Next, you copy your ML code and set the command to run your training script. You build the Docker image, which packages everything needed. Running the container starts the training script inside an isolated environment. When training finishes, the container stops automatically. To keep your model or logs, save them outside the container. This process makes ML workloads portable and consistent across machines.