Complete code (including a trained model) to deploy and inference a machine learning model (built on the iris dataset) using Docker and FastAPI.
docker build -t image_name .
docker run --name container_name -p 8000:8000 image_name
INFO: Uvicorn running on http://0.0.0.0:8000
Use this url in chrome to see the model frontend; use http://0.0.0.0:8000/docs for testing the model in the web interface.
- Via web interface (chrome):
http://0.0.0.0:8000/docs -> test model
- Via python client:
client.py
- Via curl request:
curl -X POST "http://0.0.0.0:8000/predict" -H "accept: application/json" -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}'
Basic Docker Compose Commands
docker-compose build
This command builds the images specified in the docker-compose.yml file.
docker-compose up
This command starts the services defined in the docker-compose.yml file. It builds the images if they are not already built.
docker-compose up -d
This command starts the services in the background (detached mode).
docker-compose stop
This command stops the running services without removing the containers.
docker-compose restart
This command restarts the running services.
docker-compose down
This command stops and removes the containers, networks, and volumes defined in the docker-compose.yml file.
docker-compose down -v
This command stops and removes the containers, networks, and volumes defined in the docker-compose.yml file, including the volumes associated with the services.
Credits: https://github.com/DanilZherebtsov/ml-docker-flask-api