You are tasked with deploying a three-tier web application using Docker Compose.
The stack consists of:
- Frontend - a static web page served via Nginx
- Backend - a Go-based REST API
- Database - a MySQL instance with preloaded data
/three-tier-app-challenge/
βββ docker-compose.yaml # Compose file defining all services
βββ frontend/ # Frontend static app
β βββ Dockerfile # Dockerfile for frontend
β βββ index.html # Frontend HTML
βββ backend/ # Backend Go API
βββ Dockerfile # Dockerfile for backend
βββ main.go # Go source code
βββ go.mod # Go modules
βββ go.sum # Go checksum file
Your goal is to connect all components properly and display a message from the database on the frontend along with a visit counter.
- Use MySQL from the provided image
ghcr.io/hayk96/three-tier-app-challenge-mysql:latest. - Database is already initialised with the following details and credentials. (to be used by backend):
| Key | Value |
|---|---|
| username | student |
| password | student1234 |
| database name | student_db |
-
Build a Go-based REST API from
./backendto serve:- JSON response: with the message and page visit counter from the database.
-
The backend uses environment variables to connect to the database. The variables are misconfigured in the codebase and must be set correctly in the
docker-compose.yaml. Check the correct values in the Database section above. -
Expose the backend on port 9000 to ensure it is reachable by the frontend (user browser).
-
The backend image must be optimized and must not exceed 10 MB in size.
- Serve a static HTML page from
./frontendusing Nginx. - The frontend must only communicate with the backend; direct access to the database is not allowed.
- Expose the frontend on port 8080 and ensure it displays:
- A message retrieved from the database via backend including visit counter that increments on every page refresh.
- Create two user-defined networks:
frontend-networkβ connectsfrontendβbackendbackend-networkβ connectsbackendβdatabase
- Assign the backend container a static IP:
172.20.0.10. (see reference here, how to assign a static IP address to the container. - Subnets:
frontend-network:X.X.X.X/Xbackend-network:Y.Y.Y.Y/Y
If you see that your frontend application correctly displays a message about completing the challenge and shows a visit
counter that increments with each refresh, that's great. However, you need to consider that in case you destroy the stack,
you will lose your data and current state. To prevent this, you need to enable persistence for your database. The database
holds data in the /var/lib/mysql directory, so you need to mount a volume to that directory. Name the volume as database-volume.
To verify if database persistence is working as expected, please bring down the Docker Compose setup using docker-compose down
and then recreate it. If, upon returning to the frontend page (http://localhost:8080), the visit counter continues incrementing from the previous
value, you have successfully completed the challenge! π
Distributed under the MIT License. See LICENSE for more information.
Hayk Davtyan | @hayk96
