Skip to content

This tutorial walks through the process of building an end-to-end service. It covers setting up a conda environment, creating functions, exposing it through an API, and running the API locally, how to dockerize the service using Dockerfile and docker-compose, and finally, how to access and interact with the containerized service.

License

Notifications You must be signed in to change notification settings

MZayed47/end-to-end-service

Repository files navigation

End-to-End Service Setup

A. Set Up a Conda Environment

  1. Follow basic conda commands for environment management here.

  2. Create and activate a new conda environment:

    conda create -n end-to-end python=3.9
    conda activate end-to-end

B. Write Basic Functionality

  1. Always use functions for repetitive tasks.

  2. Create two scripts:

    • main.py: Holds the main functionality and calls the necessary functions from utility_v1.py.
    • utility_v1.py: Contains reusable functions.

C. Create the API

  1. Install required libraries:

    pip install fastapi uvicorn
  2. Create an API script with a POST endpoint and functionality to process input and return a JSON response.

  3. 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
  4. 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.

D. Dockerization

  1. Export the environment libraries:

    conda list --export > requirements.txt
  2. Clean unnecessary library versions in requirements.txt (recommended).

  3. Dockerize the application:

    • Dockerfile: Copy necessary files and set up the environment.
    • docker-compose.yml: Automate Docker setup and start the service.
  4. Build and run the service:

    docker-compose build
    docker-compose up -d

    Or combine the steps:

    docker-compose up -d --build
  5. Check container logs:

    docker logs <container_id>

    Example:

    docker logs 07c91a7f6c40
  6. Test using curl or Postman as mentioned in step C.

E. Access and Interact with the Docker Container

  1. List running containers:

    docker ps
  2. Access the container shell:

    docker exec -it <container_id> /bin/bash

    Example:

    docker exec -it 07c91a7f6c40 /bin/bash
  3. Run Python scripts inside the container:

    python main.py
  4. Run one-time commands without entering the container shell:

    docker exec <container_id> python main.py

About

This tutorial walks through the process of building an end-to-end service. It covers setting up a conda environment, creating functions, exposing it through an API, and running the API locally, how to dockerize the service using Dockerfile and docker-compose, and finally, how to access and interact with the containerized service.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published