{"id":25639429,"url":"https://github.com/beneite/springwithdocker","last_synced_at":"2025-07-06T02:38:07.315Z","repository":{"id":278936135,"uuid":"937219535","full_name":"beneite/SpringWithDocker","owner":"beneite","description":"This repo has details on how to dockerize a backend application","archived":false,"fork":false,"pushed_at":"2025-02-22T16:24:44.000Z","size":525,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T17:25:29.567Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beneite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-22T16:10:52.000Z","updated_at":"2025-02-22T16:24:48.000Z","dependencies_parsed_at":"2025-02-22T17:25:38.729Z","dependency_job_id":"4c47d561-d65d-43ac-9c10-c29af1985bf3","html_url":"https://github.com/beneite/SpringWithDocker","commit_stats":null,"previous_names":["beneite/springwithdocker"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beneite%2FSpringWithDocker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beneite%2FSpringWithDocker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beneite%2FSpringWithDocker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beneite%2FSpringWithDocker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beneite","download_url":"https://codeload.github.com/beneite/SpringWithDocker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240265481,"owners_count":19774071,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-23T03:37:46.990Z","updated_at":"2025-07-06T02:38:07.309Z","avatar_url":"https://github.com/beneite.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🐳 Spring Boot with MySQL \u0026 Adminer using Docker Compose\n\nThis project demonstrates how to containerize a **Spring Boot application** with **MySQL** and **Adminer** using **Docker Compose**.\n\n\u003e - Swagger Url: http://localhost:8080/swagger-ui/index.html#/\n\u003e - UI Url: http://localhost:8080/\n\n## 📌 Project Structure\n\n```\nproject-root/\n├── src/main/java       # Java source code\n├── src/main/resources  # Configuration files\n├── Dockerfile          # Defines how to build the Spring Boot app image\n├── docker-compose.yml  # Defines multi-container setup\n├── README.md           # Project documentation\n```\n\n## 🧑‍💻 Technologies Used\n\n- Spring Boot\n- Spring Web\n- Thymeleaf\n- HTML/CSS/JavaScript\n\n## 🔧 Services Overview\n\n- **MySQL (****`mysql-service`****)**\n    - Runs MySQL database on port **3306** (internal Docker network)\n    - Stores data in a persistent volume\n- **Spring Boot Application (****`springApplication-service`****)**\n    - Connects to MySQL at `mysql-service:3306`\n    - Exposes API on port **8080**\n- **Adminer (****`adminer-service`****)**\n    - Provides a web-based database UI\n    - Runs on port **8081** (accessible from browser)\n\n## 📡 Docker Network Diagram\n\n***\n![applicationOnDockerAchitecture.png](src/main/resources/static/images/applicationOnDockerAchitecture.png)\n***\n## 🚀 Getting Started\n\n### 1️⃣ Clone the Repository\n\n```sh\ngit clone https://github.com/beneite/SpringBootBasics.git\ncd SpringWithDocker\n```\n\n### 2️⃣ Build and Run Containers\n\n```sh\ndocker compose --profile entireApp up --build\n```\n- `--profile entireApp` will spin up the tagged services with 'entireApp' only\n- `-d` runs containers in detached mode\n- `--build` forces a rebuild of the Spring Boot image\n- The docker compose will also take care of building the docker file/image.\n- The docker file used is a multistage where in stage 1st i am creating the .jar file using the same in 2nd stage to bring the application up.\n\n### 3️⃣ Check Running Containers\n\n```sh\ndocker ps\n```\n\n### 4️⃣ Access Services\n\n- **Spring Boot API** → `http://localhost:8080`\n- **Adminer UI** → `http://localhost:8081`\n    - **System:** `MySQL`\n    - **Server:** `mysql-service`\n    - **Username:** `ashish`\n    - **Password:** `ashish@123`\n    - **database:** `userManagementDb`\n\n### 5️⃣ Stop and Remove Containers\n\n```sh\ndocker-compose --profile entireApp down\n```\n\n# **🔹 Explanation of `docker-compose.yml` File**\n\n`docker-compose.yml` file defines **four services** and uses **Docker Compose profiles** to control which services start together.\n\n---\n\n## **🚀 Services Breakdown**\n\n### **1️⃣ MySQL Service for `entireApp` Profile** (`mysql-service`)\n```yaml\n  mysql-service:\n    image: mysql:latest\n    environment:\n      MYSQL_DATABASE: userManagementDb\n      MYSQL_ROOT_PASSWORD: ashish@123\n      MYSQL_USER: ashish\n      MYSQL_PASSWORD: ashish@123\n    volumes:\n      - ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d\n      - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql\n    profiles:\n      - entireApp\n```\n🔹 **Purpose:**\n- Runs a MySQL database **only when using the `entireApp` profile**.\n- Stores database files in **a persistent volume** (`mysql_data`).\n- Initializes the database using scripts from `init-script`.\n\n🔹 **Why No `ports` Mapping?**\n- The MySQL container **is not accessible from outside Docker** because there is **no port mapping** (`8082:3306`).\n- It can be accessed **only by other services inside Docker** (like `springApplication-service`).\n\n---\n\n### **2️⃣ MySQL Service for `onlyDb` Profile** (`mysql-service-onlyDb`)\n```yaml\n  mysql-service-onlyDb:\n    image: mysql:latest\n    environment:\n      MYSQL_DATABASE: userManagementDb\n      MYSQL_ROOT_PASSWORD: ashish@123\n      MYSQL_USER: ashish\n      MYSQL_PASSWORD: ashish@123\n    ports:\n      - \"8082:3306\"\n    volumes:\n      - ./src/main/resources/dockerizedEntireApp/init-script:/docker-entrypoint-initdb.d\n      - ./src/main/resources/dockerizedEntireApp/volumes/mysql_data:/var/lib/mysql\n    profiles:\n      - onlyDb\n```\n🔹 **Purpose:**\n- Runs a separate MySQL instance when using the `onlyDb` profile.\n- **Exposes port `8082:3306`** so you can connect to the database from outside Docker (e.g., via MySQL Workbench, a local application, etc.).\n\n🔹 **Why a Separate Service?**\n- **Docker Compose profiles do not support conditional port mappings.**\n- This duplication allows **one version of MySQL to expose a port (`8082:3306`)** while the other does not.\n\n---\n\n### **3️⃣ Adminer Service** (`adminer-service`)\n```yaml\n  adminer-service:\n    image: adminer\n    ports:\n      - \"8081:8080\"\n    profiles:\n      - onlyDb\n      - entireApp\n```\n🔹 **Purpose:**\n- Runs **Adminer**, a web-based database management tool (like phpMyAdmin).\n- Accessible at `http://localhost:8081` in your browser.\n- Included in **both profiles (`onlyDb` and `entireApp`)** so that it works in both modes.\n\n---\n\n### **4️⃣ Spring Boot Application** (`springApplication-service`)\n```yaml\n  springApplication-service:\n    build: .\n    image: userserviceimage:v1\n    ports:\n      - \"8080:8080\"\n    environment:\n      spring.datasource.url: \"jdbc:mysql://mysql-service:3306/userManagementDb?useSSL=false\u0026allowPublicKeyRetrieval=true\"\n    depends_on:\n      - mysql-service\n    profiles:\n      - entireApp\n```\n🔹 **Purpose:**\n- Runs a Spring Boot application.\n- Accessible at `http://localhost:8080`.\n- Connects to MySQL (`mysql-service`) using JDBC.\n\n🔹 **Key Configuration:**\n- **Depends on `mysql-service`**, meaning MySQL starts **before** Spring Boot.\n- **Database URL:**\n  ```plaintext\n  jdbc:mysql://mysql-service:3306/userManagementDb\n  ```\n  This tells Spring Boot to connect to the MySQL service inside Docker.\n\n---\n\n## **🎯 How Profiles Work in This Setup**\n### **1️⃣ Running Only the Database (`onlyDb` Profile)**\n```sh\ndocker compose --profile onlyDb up\n```\n✔ Starts:  \n✅ `mysql-service-onlyDb` (with `8082:3306` exposed).  \n✅ `adminer-service`.  \n❌ **Does NOT start Spring Boot** (`springApplication-service`).\n\n### **2️⃣ Running the Full Application (`entireApp` Profile)**\n```sh\ndocker compose --profile entireApp up\n```\n✔ Starts:  \n✅ `mysql-service` (without exposing ports).  \n✅ `adminer-service`.  \n✅ `springApplication-service`.  \n❌ **Does NOT start `mysql-service-onlyDb`** (no duplicate MySQL).\n\n---\n\n## **🔥 Why This Approach is Useful**\n✔ **Avoids exposing MySQL publicly when not needed.**  \n✔ **Allows local access (`8082:3306`) when using `onlyDb` profile.**  \n✔ **Keeps `entireApp` profile isolated (Spring Boot can access MySQL internally).**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeneite%2Fspringwithdocker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeneite%2Fspringwithdocker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeneite%2Fspringwithdocker/lists"}