Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Docker for ML reproducibility
📖 Scenario: You are a data scientist working on a machine learning project. You want to make sure your code runs the same way on any computer. This helps your team avoid problems when sharing your work.Docker is a tool that packages your code, libraries, and settings into one container. This container works the same everywhere.
🎯 Goal: Build a simple Docker setup that runs a Python script for training a machine learning model. You will create a Dockerfile, add a configuration, run the training script inside the container, and see the output.
📋 What You'll Learn
Create a Python script named train.py that prints a message about training.
Write a Dockerfile that uses Python 3.12 image and copies train.py inside.
Add a configuration variable inside train.py to set the number of training epochs.
Run the training script inside the Docker container and print the training message with epochs.
💡 Why This Matters
🌍 Real World
Data scientists use Docker to share ML projects that run the same on any computer or cloud.
💼 Career
Knowing Docker helps ML engineers and DevOps teams ensure reproducible and reliable ML workflows.
Progress0 / 4 steps
1
Create the training script
Create a Python file named train.py with a variable message set to the string "Training model..." and a print statement that prints message.
MLOps
Hint
Use a variable called message and set it to the exact string. Then print it.
2
Write the Dockerfile
Create a file named Dockerfile that starts with FROM python:3.12-slim, copies train.py into the container, and sets the command to run python train.py.
MLOps
Hint
Use the official Python 3.12 slim image. Copy the script and set the working directory. Use CMD to run the script.
3
Add training epochs configuration
In train.py, add a variable epochs set to 5. Change the print statement to print f"Training model for {epochs} epochs..." using an f-string.
MLOps
Hint
Remove the old message variable. Use epochs = 5 and an f-string in print.
4
Run the Docker container and show output
Run the Docker container using the command docker build -t ml-train . to build the image, then docker run ml-train to run it. Write a print statement in your script that outputs exactly Training model for 5 epochs....
MLOps
Hint
Make sure your print statement matches the output exactly. Build and run the Docker container as instructed.
Practice
(1/5)
1. What is the main benefit of using Docker for machine learning projects?
easy
A. It replaces the need for writing ML code.
B. It automatically improves the accuracy of ML models.
C. It ensures the ML code runs the same way on any machine.
D. It speeds up the training process by using GPUs only.
Solution
Step 1: Understand Docker's purpose in ML
Docker packages the code and environment so it runs identically anywhere.
Step 2: Compare options
Only 'It ensures the ML code runs the same way on any machine.' describes this reproducibility benefit correctly.
Final Answer:
It ensures the ML code runs the same way on any machine. -> Option C
Quick Check:
Docker = consistent environment [OK]
Hint: Docker = same environment everywhere [OK]
Common Mistakes:
Thinking Docker improves model accuracy automatically
Believing Docker replaces ML coding
Assuming Docker only speeds up training
2. Which of the following is the correct way to start a Docker container from an image named ml-image?
easy
A. docker run ml-image
B. docker start ml-image
C. docker build ml-image
D. docker create ml-image
Solution
Step 1: Identify the command to run a container
The docker run command starts a container from an image.
Step 2: Understand other commands
docker start starts stopped containers, docker build creates images, docker create creates containers but does not start them.
Final Answer:
docker run ml-image -> Option A
Quick Check:
Run container = docker run [OK]
Hint: Use 'docker run' to start containers from images [OK]
Common Mistakes:
Using 'docker start' to run new containers
Confusing 'docker build' with running containers
Using 'docker create' without starting container
3. Given this Dockerfile snippet:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . ./
CMD ["python", "train.py"]
What happens when you build and run this Docker image?
medium
A. The container installs dependencies and runs train.py automatically.
B. The container only copies files but does not install dependencies.
C. The container runs train.py without installing dependencies.
D. The container fails because requirements.txt is missing.
Solution
Step 1: Analyze Dockerfile steps
The Dockerfile sets Python 3.12, copies requirements.txt, installs dependencies, copies code, then runs train.py.
Step 2: Understand build and run behavior
Building installs dependencies; running executes train.py automatically as CMD defines the command.
Final Answer:
The container installs dependencies and runs train.py automatically. -> Option A
Quick Check:
Dockerfile CMD runs train.py after setup [OK]
Hint: CMD runs script after dependencies installed [OK]
Common Mistakes:
Assuming dependencies are not installed
Thinking CMD runs during build, not run
Believing files are not copied before run
4. You wrote a Dockerfile but when running the container, your ML code fails with "ModuleNotFoundError". What is the most likely cause?
medium
A. You forgot to copy your code files into the image.
B. You did not expose the correct port in Dockerfile.
C. You used the wrong base image version.
D. You did not install the required Python packages.
Solution
Step 1: Understand ModuleNotFoundError meaning
This error means Python cannot find a required package or module.
Step 2: Identify cause related to Dockerfile
Not installing required packages (missing pip install) causes this error, not copying code or ports.
Final Answer:
You did not install the required Python packages. -> Option D
Quick Check:
ModuleNotFoundError = missing packages [OK]
Hint: Missing packages cause ModuleNotFoundError [OK]
Common Mistakes:
Blaming missing code files instead of packages
Confusing port exposure with module errors
Assuming base image version always causes this
5. You want to ensure your ML training runs reproducibly with Docker, including specific Python version, dependencies, and data files. Which Dockerfile snippet best achieves this?
hard
A. FROM ubuntu:latest
RUN apt-get update
COPY train.py ./
CMD ["python", "train.py"]
B. FROM python:3.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY data/ ./data/
COPY train.py ./
CMD ["python", "train.py"]
C. FROM python:latest
COPY train.py ./
CMD ["python", "train.py"]
D. FROM python:3.12
RUN pip install numpy
CMD ["python", "train.py"]
Solution
Step 1: Check for full environment setup
FROM python:3.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY data/ ./data/
COPY train.py ./
CMD ["python", "train.py"] sets Python 3.12, installs dependencies from requirements.txt, copies data and code, then runs training.
Step 2: Compare other options
The other options miss dependencies, data files, or use generic Python versions, risking non-reproducibility.
Final Answer:
FROM python:3.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY data/ ./data/
COPY train.py ./
CMD ["python", "train.py"] -> Option B
Quick Check:
Complete setup = reproducibility [OK]
Hint: Copy code, data, install deps, set Python version [OK]