In Week 03, you will extend the University Course Registration System by introducing a microservices architecture and deploying multiple services using Docker Compose.
Unlike Week 02, which contained a single FastAPI application, this week's solution consists of:
- Student Service (FastAPI)
- Course Service (FastAPI)
- Student Database (PostgreSQL)
- Course Database (PostgreSQL)
- Frontend (React)
Docker Compose will be used to build, configure, network, and run all services using a single command.
After completing this practical, you should be able to:
- Understand a basic microservices architecture.
- Build and deploy multiple containers using Docker Compose.
- Connect multiple FastAPI services with PostgreSQL databases.
- Configure communication between containers.
- Execute unit tests for backend and frontend applications.
- Verify communication between frontend and backend services.
-
Clone the Repository
git clone https://github.com/sit722-devops/week03.git
-
Navigate to the Project
cd week03 -
Open the Project
Open the
week03folder using Visual Studio Code.
The Student Service and Course Service each have their own Python environment and dependencies.
-
Navigate to the Student Service.
cd student-service -
Create a virtual environment.
# Create the virtual environment python -m venv .venv # Activate the virtual environment # On macOS/Linux: source ./.venv/bin/activate # On Windows (Command Prompt): # .\.venv\Scripts\activate.bat # On Windows (PowerShell): # .\.venv\Scripts\Activate.ps1
-
Install dependencies.
pip install -r requirements.txt
-
Deactivate the virtual environment and return to the project root.
deactivate cd ..
-
Navigate to the Course Service.
cd course-service -
Create a virtual environment.
# Create the virtual environment python -m venv .venv # Activate the virtual environment # On macOS/Linux: source ./.venv/bin/activate # On Windows (Command Prompt): # .\.venv\Scripts\activate.bat # On Windows (PowerShell): # .\.venv\Scripts\Activate.ps1
-
Install dependencies.
pip install -r requirements.txt
-
Deactivate the virtual environment and return to the project root.
deactivate cd ..
-
Navigate to the frontend.
cd frontend -
Install the required Node.js packages.
npm install
-
Return to the project root.
cd ..
Before deploying the application using Docker Compose, ensure all unit tests pass successfully.
cd student-service
pytest --verbose tests
cd ..
NOTE: The Student Service unit tests require a PostgreSQL database named students. If the database does not already exist, create it before running the tests. CREATE DATABASE students; The test suite will automatically create the required database tables if they do not already exist.
cd course-service
pytest --verbose tests
cd ..NOTE: The Course Service unit tests require a PostgreSQL database named courses. If the database does not already exist, create it before running the tests. CREATE DATABASE courses; The test suite will automatically create the required database tables if they do not already exist.
cd frontend
npm test
cd ..From the project root directory, execute:
docker compose build --no-cache
docker compose up -dNOTE: Depending on your version of Docker, you may need to replace
docker composewithdocker-compose, such as:docker-compose build --no-cache docker-compose up -d
Docker Compose will automatically:
- Build the Student Service image.
- Build the Course Service image.
- Build the Frontend image.
- Create the Student PostgreSQL database container.
- Create the Course PostgreSQL database container.
- Create the Docker network.
- Connect all services together.
Run following:
docker compose psYou should see the following containers running:
- frontend
- student-service
- course-service
- student-db
- course-db
View logs for all containers.
docker compose logsView logs for the Student Service.
docker compose logs student-serviceView logs for the Course Service.
docker compose logs course-serviceView logs for the Frontend.
docker compose logs frontendhttp://localhost:5173
http://localhost:8001/docs
http://localhost:8002/docs
Using the frontend:
- Create a new student.
- Verify the student appears in the Students table.
- Create a new course.
- Verify the course appears in the Courses table.
Using Swagger UI:
GET /studentsGET /students/{student_id}POST /students
GET /coursesGET /courses/{course_id}POST /courses
Stop all running containers.
docker compose downTo also remove the PostgreSQL volumes and start with a fresh database next time, run:
docker compose down -v