0
0
MLOpsdevops~5 mins

Docker for ML workloads in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
Docker helps package your machine learning code, libraries, and environment into one container. This solves the problem of running ML models consistently on any computer or server without setup issues.
When you want to share your ML model with others and ensure it runs the same way on their machines.
When you need to deploy an ML model to a cloud server without worrying about missing dependencies.
When you want to test your ML code in a clean environment that matches production.
When you want to run multiple ML experiments with different library versions without conflicts.
When you want to automate ML training and deployment in a CI/CD pipeline.
Config File - Dockerfile
Dockerfile
FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . ./

CMD ["python", "train.py"]

This Dockerfile starts from a lightweight Python 3.10 image.

It sets the working directory to /app inside the container.

It copies the requirements.txt file and installs Python packages needed for ML.

It copies all your ML code into the container.

Finally, it runs the training script train.py when the container starts.

Commands
This command builds a Docker image named 'ml-training-image' from the Dockerfile in the current folder. It packages your ML code and dependencies into one image.
Terminal
docker build -t ml-training-image .
Expected OutputExpected
Sending build context to Docker daemon 12.3MB Step 1/6 : FROM python:3.10-slim ---> 123abc456def Step 2/6 : WORKDIR /app ---> Using cache ---> 789def012abc Step 3/6 : COPY requirements.txt ./ ---> Using cache ---> 345ghi678jkl Step 4/6 : RUN pip install --no-cache-dir -r requirements.txt ---> Running in abc123def456 Collecting numpy Installing collected packages: numpy Successfully installed numpy-1.24.2 Removing intermediate container abc123def456 ---> 567mno890pqr Step 5/6 : COPY . ./ ---> 234stu567vwx Step 6/6 : CMD ["python", "train.py"] ---> Running in def789ghi012 Removing intermediate container def789ghi012 ---> 890yz123abc Successfully built 890yz123abc Successfully tagged ml-training-image:latest
-t - Assigns a name and optionally a tag to the image
This command runs the container from the 'ml-training-image' image. The --rm flag removes the container after it finishes to keep your system clean.
Terminal
docker run --rm ml-training-image
Expected OutputExpected
Training started... Epoch 1/10 Loss: 0.45 Epoch 2/10 Loss: 0.30 Training completed successfully.
--rm - Automatically removes the container when it exits
This command lists all Docker images on your system so you can verify your ML image was created.
Terminal
docker images
Expected OutputExpected
REPOSITORY TAG IMAGE ID CREATED SIZE ml-training-image latest 890yz123abc 2 minutes ago 150MB
Key Concept

If you remember nothing else from this pattern, remember: Docker packages your ML code and environment together so it runs the same everywhere.

Common Mistakes
Not including all required ML libraries in requirements.txt
The container will miss needed packages and your code will fail to run.
List all Python dependencies your ML code needs in requirements.txt before building the image.
Running docker run without the --rm flag during testing
Containers accumulate and use disk space if not removed after running.
Use --rm to automatically clean up containers after they finish.
Not copying the ML code files into the Docker image
The container will have no code to run, causing errors.
Use COPY commands in the Dockerfile to include your ML scripts and files.
Summary
Write a Dockerfile to specify the Python environment and ML dependencies.
Build a Docker image with your ML code and libraries using 'docker build'.
Run the ML training inside a container with 'docker run' to ensure consistency.
Use 'docker images' to check your built images and manage them.