-
Follow basic conda commands for environment management here.
-
Create and activate a new conda environment:
conda create -n end-to-end python=3.9 conda activate end-to-end
-
Always use functions for repetitive tasks.
-
Create two scripts:
- main.py: Holds the main functionality and calls the necessary functions from
utility_v1.py
. - utility_v1.py: Contains reusable functions.
- main.py: Holds the main functionality and calls the necessary functions from
-
Install required libraries:
pip install fastapi uvicorn
-
Create an API script with a POST endpoint and functionality to process input and return a JSON response.
-
Example of API startup:
uvicorn api_v1:app --reload
Or, include the following in your script:
if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=5001)
Run the script:
python api_v1.py
-
Test the API:
Using curl:
curl -X 'POST' 'http://127.0.0.1:5001/echo/' -H 'Content-Type: application/json' -d '{"message": "Hello World"}'
Or use Postman.
-
Export the environment libraries:
conda list --export > requirements.txt
-
Clean unnecessary library versions in
requirements.txt
(recommended). -
Dockerize the application:
- Dockerfile: Copy necessary files and set up the environment.
- docker-compose.yml: Automate Docker setup and start the service.
-
Build and run the service:
docker-compose build docker-compose up -d
Or combine the steps:
docker-compose up -d --build
-
Check container logs:
docker logs <container_id>
Example:
docker logs 07c91a7f6c40
-
Test using curl or Postman as mentioned in step C.
-
List running containers:
docker ps
-
Access the container shell:
docker exec -it <container_id> /bin/bash
Example:
docker exec -it 07c91a7f6c40 /bin/bash
-
Run Python scripts inside the container:
python main.py
-
Run one-time commands without entering the container shell:
docker exec <container_id> python main.py