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.
Browser
|
React Frontend
|
---------------------------------
| |
Student Service Course Service
| |
PostgreSQL (students) PostgreSQL (courses)
Before starting this practical, ensure you have the following installed.
Download:
Verify installation:
node --version
npm --versionweek03/
│
├── student-service/
├── course-service/
├── frontend/
├── docker-compose.yml
├── README.md
└── .gitignore
- Clone the Repository
git clone https://github.com/sit722-devops/week03.git- Navigate to the Project
cd week03- Open the Project
Open the week03 folder 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-serviceCreate 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.ps1Install dependencies.
pip install -r requirements.txtReturn to the project root.
cd ..Navigate to the Course Service.
cd course-serviceCreate 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.ps1Install dependencies.
pip install -r requirements.txtReturn to the project root.
cd ..Navigate to the frontend.
cd frontendInstall the required Node.js packages.
npm installReturn to the project root.
cd ..Before deploying the application using Docker Compose, ensure all unit tests pass successfully.
cd student-service
pytest 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 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 build -dDocker 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