Recitation 9: Microservices and Docker¶
Prerequisites¶
You should have downloaded Docker. If you haven't follow the installation instructions here
Setup Instructions (10 min):¶
Fork this repo and clone it.
Open the folder in VSCode. Then you should "reopen in continer" similar to NodeBB, but this container is setup for python.
Also start the docker engine.
Overview¶
During this recitation, students will create a simple FastAPI app, containerize it and deploy it.
Context¶
The course staff is building an internal API to support dashboards and check-ins for project teams in 17-313. An existing microservice already exposes team names by team ID.
Try it out here
Example:
{ "team_name": "nodegpt" }
Now, the staff needs a more complete endpoint that returns both the team name and the team mentor. Your task is to build a new service that provides an endpoint GET /team_info/<team_id> that calls the existing microservice and enriches the response with the mentor’s name.
The endpoint of your service has to return a JSON object in the following form:
{
"team_id": 1,
"team_name": "nodegpt",
"mentor": "Seckhen"
}
Activity¶
- Open the project in VS Code using the provided devcontainer.
Follow the instructions in the
README.mdfile to set up your environment. - Implement the
team_infoendpoint according to the specifications. Editapp/main.pyto do so. - Test it by running the app locally, from a terminal in the devcontainer:
and by verifying the endpoint visting localhost:8080/team_info/1. You should receive a JSON response with the team name, and CA name. Your job is to implement the information for all the teams.
pip install -r requirements.txt uvicorn app.main:app --host 0.0.0.0 --port 8080 - Now that the implementation is complete, let's dockerize the service. Complete the
Dockerfile. You can use the slides and this link as resources. - Create the docker image using the command below, and check that the image has been created.
docker build -t YOUR_IMAGE_NAME . - Try running your image (it should work similarly to when you run the app locally).
docker run --rm -p 8080:8080 YOUR_IMAGE_NAME - OPTIONAL: Implement
docker-compose.yml. You can use the slides for reference. - OPTIONAL: Launch a container using the docker image using the command below.
docker compose up -d --build - Check that your service is running correctly by visiting http://localhost:8080/team_info/1. You should receive a JSON response with the team name, and CA name. Your job is to implement the information for all the teams.
Submission:¶
Make sure to Submit on Gradescope after this recitation. Everyone should submit individually.