In this lab, you'll learn how to containerize a Flask application, train and deploy a machine learning model using Docker and Docker Compose. By the end of the lab, you'll have a Flask API running in a Docker container that can serve predictions using a machine learning model trained on the Iris dataset.
Clone the code from this repository.
- Setup Docker on your system
- Containerize training the ML Model
- Containerize the Flask App for inference
- Train and deploy the machine learning model using Docker
- Install Docker on your system
- Verify Docker installation
Follow the instructions for your operating system to install Docker. Then test your installation with:
docker run hello-world
Complete file named Dockerfile.train
Fill in the TODO sections.
- Copy requirements.txt and install dependencies
- Copy train.py to the working directory
- Set the command to run train.py
In the server.py file, there is a function called predict() where you need to:
- Load the trained machine learning model.
- Run inference using an input sent through a GET request.
- Return the prediction as the response.
Fill in the TODO sections.
Fill in the TODO sections.
- Set the working directory to /app
- Copy requirements.txt and install dependencies
- Copy server.py to the working directory
- Expose port 8080 or any other free port
- Set the command to run server.py
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a docker-compose.yml file to configure the application's services, networks, and volumes. In this lab, you'll use Docker Compose to set up two services:
- Training Service: This service will train a machine learning model using the Iris dataset.
- Inference Service: Once the model is trained, the inference service will load the trained model and serve predictions via a Flask API.
Fill in the TODO sections:
- Set the
Dockerfile.train
for the training service and Dockerfile.infer for the inference service. - Use a shared volume (model_storage) to store the trained model between the services.
- Expose port 8080 (or nay other port) for the Flask app in the inference service.
- Ensure the inference service starts only after the training service completes (depends_on).
- Declare the model_storage volume at the end for both services to access the trained model.
- Build and run services with Docker Compose
- Verify both services are running correctly
Use the following command to start your services:
docker-compose up --build
For this assignment, run the following CURL command to test your flask setup.
curl --location --request GET 'localhost:8080/predict' \
--header 'Content-Type: application/json' \
--data '{
"input": [6.3, 3.3, 6 , 2.5]
}'
Alternatively, you can test this on Postman as well - ensure that your body is JSON with the following data
{
"input": [6.3, 3.3, 6 , 2.5]
}
If you encounter issues:
- Check Docker daemon status
- Verify port availability
- Review service logs with
docker-compose logs
- Ensure the training service completes before the inference service starts