@@ -44,9 +44,9 @@ mvn clean package -DskipTests
4444### 2️⃣ Build and Run Containers
4545
4646``` sh
47- docker- compose up -d --build
47+ docker compose --profile entireApp up --build
4848```
49-
49+ - ` --profile entireApp ` will spin up the tagged services with 'entireApp' only
5050- ` -d ` runs containers in detached mode
5151- ` --build ` forces a rebuild of the Spring Boot image
5252
@@ -69,7 +69,142 @@ docker ps
6969### 5️⃣ Stop and Remove Containers
7070
7171``` sh
72- docker-compose down -v
72+ docker-compose --profile entireApp down
73+ ```
74+
75+ # ** 🔹 Explanation of ` docker-compose.yml ` File**
76+
77+ ` docker-compose.yml ` file defines ** four services** and uses ** Docker Compose profiles** to control which services start together.
78+
79+ ---
80+
81+ ## ** 🚀 Services Breakdown**
82+
83+ ### ** 1️⃣ MySQL Service for ` entireApp ` Profile** (` mysql-service ` )
84+ ``` yaml
85+ mysql-service :
86+ image : mysql:latest
87+ restart : always
88+ environment :
89+ MYSQL_DATABASE : userManagementDb
90+ MYSQL_ROOT_PASSWORD : ashish@123
91+ MYSQL_USER : ashish
92+ MYSQL_PASSWORD : ashish@123
93+ volumes :
94+ - ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d
95+ - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
96+ profiles :
97+ - entireApp
98+ ` ` `
99+ 🔹 **Purpose:**
100+ - Runs a MySQL database **only when using the ` entireApp` profile**.
101+ - Stores database files in **a persistent volume** (`mysql_data`).
102+ - Initializes the database using scripts from `init-script`.
103+
104+ 🔹 **Why No `ports` Mapping?**
105+ - The MySQL container **is not accessible from outside Docker** because there is **no port mapping** (`8082:3306`).
106+ - It can be accessed **only by other services inside Docker** (like `springApplication-service`).
107+
108+ ---
109+
110+ # ## **2️⃣ MySQL Service for `onlyDb` Profile** (`mysql-service-onlyDb`)
111+ ` ` ` yaml
112+ mysql-service-onlyDb:
113+ image: mysql:latest
114+ restart: always
115+ environment:
116+ MYSQL_DATABASE: userManagementDb
117+ MYSQL_ROOT_PASSWORD: ashish@123
118+ MYSQL_USER: ashish
119+ MYSQL_PASSWORD: ashish@123
120+ ports:
121+ - "8082:3306"
122+ volumes:
123+ - ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d
124+ - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql
125+ profiles:
126+ - onlyDb
127+ ` ` `
128+ 🔹 **Purpose:**
129+ - Runs a separate MySQL instance when using the `onlyDb` profile.
130+ - **Exposes port `8082:3306`** so you can connect to the database from outside Docker (e.g., via MySQL Workbench, a local application, etc.).
131+
132+ 🔹 **Why a Separate Service?**
133+ - **Docker Compose profiles do not support conditional port mappings.**
134+ - This duplication allows **one version of MySQL to expose a port (`8082:3306`)** while the other does not.
135+
136+ ---
137+
138+ # ## **3️⃣ Adminer Service** (`adminer-service`)
139+ ` ` ` yaml
140+ adminer-service:
141+ image: adminer
142+ restart: always
143+ ports:
144+ - "8081:8080"
145+ profiles:
146+ - onlyDb
147+ - entireApp
148+ ` ` `
149+ 🔹 **Purpose:**
150+ - Runs **Adminer**, a web-based database management tool (like phpMyAdmin).
151+ - Accessible at `http://localhost:8081` in your browser.
152+ - Included in **both profiles (`onlyDb` and `entireApp`)** so that it works in both modes.
153+
154+ ---
155+
156+ # ## **4️⃣ Spring Boot Application** (`springApplication-service`)
157+ ` ` ` yaml
158+ springApplication-service:
159+ build: .
160+ image: userserviceimage:v1
161+ ports:
162+ - "8080:8080"
163+ environment:
164+ spring.datasource.url: "jdbc:mysql://mysql-service:3306/userManagementDb?useSSL=false&allowPublicKeyRetrieval=true"
165+ depends_on:
166+ - mysql-service
167+ profiles:
168+ - entireApp
169+ ` ` `
170+ 🔹 **Purpose:**
171+ - Runs a Spring Boot application.
172+ - Accessible at `http://localhost:8080`.
173+ - Connects to MySQL (`mysql-service`) using JDBC.
174+
175+ 🔹 **Key Configuration:**
176+ - **Depends on `mysql-service`**, meaning MySQL starts **before** Spring Boot.
177+ - **Database URL:**
178+ ` ` ` plaintext
179+ jdbc:mysql://mysql-service:3306/userManagementDb
180+ ` ` `
181+ This tells Spring Boot to connect to the MySQL service inside Docker.
182+
183+ ---
184+
185+ # # **🎯 How Profiles Work in This Setup**
186+ # ## **1️⃣ Running Only the Database (`onlyDb` Profile)**
187+ ` ` ` sh
188+ docker compose --profile onlyDb up
73189` ` `
190+ ✔ Starts :
191+ ✅ `mysql-service-onlyDb` (with `8082:3306` exposed).
192+ ✅ `adminer-service`.
193+ ❌ **Does NOT start Spring Boot** (`springApplication-service`).
74194
75- - ` -v ` removes named volumes to avoid leftover data
195+ # ## **2️⃣ Running the Full Application (`entireApp` Profile)**
196+ ` ` ` sh
197+ docker compose --profile entireApp up
198+ ` ` `
199+ ✔ Starts :
200+ ✅ `mysql-service` (without exposing ports).
201+ ✅ `adminer-service`.
202+ ✅ `springApplication-service`.
203+ ❌ **Does NOT start `mysql-service-onlyDb`** (no duplicate MySQL).
204+
205+ ---
206+
207+ # # **🔥 Why This Approach is Useful**
208+ ✔ **Avoids exposing MySQL publicly when not needed.**
209+ ✔ **Allows local access (`8082:3306`) when using `onlyDb` profile.**
210+ ✔ **Keeps `entireApp` profile isolated (Spring Boot can access MySQL internally).**
0 commit comments