-
Notifications
You must be signed in to change notification settings - Fork 16
Containerized App and DB #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| SPRING_DATASOURCE_URL=jdbc:mysql://mysql-cont:3306/usersystem?useSSL=false&serverTimezone=UTC&createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true | ||
| SPRING_DATASOURCE_USERNAME=root | ||
| SPRING_DATASOURCE_PASSWORD=root | ||
| SPRING_JPA_HIBERNATE_DDL_AUTO=update | ||
| SPRING_JPA_SHOW_SQL=true | ||
|
|
||
| MYSQL_ROOT_PASSWORD=root | ||
| MYSQL_DATABASE=usersystem |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Use the Maven OpenJDK image as the base image | ||
| FROM maven:3.8.1-openjdk-17 AS build | ||
|
|
||
| # Set the working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy the pom.xml and the project source code | ||
| COPY pom.xml . | ||
| COPY src ./src | ||
| COPY .env . | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a need to copy this .env file inside the container ?. Copying the .env file in the container might not have any benefit. |
||
|
|
||
| # Package the application | ||
| RUN mvn clean package -DskipTests | ||
|
|
||
| # Use the official OpenJDK image to run the application | ||
| FROM openjdk:17-jdk-slim | ||
|
|
||
| # Set the working directory | ||
| WORKDIR /app | ||
|
|
||
| # Copy the packaged jar file from the build stage | ||
| COPY --from=build /app/target/rihal-0.0.1-SNAPSHOT.jar . | ||
| COPY --from=build /app/.env . | ||
|
|
||
| # Expose the port the application runs on | ||
| EXPOSE 8080 | ||
|
|
||
| # Run the application with .env variables | ||
| ENTRYPOINT ["sh", "-c", "java -jar rihal-0.0.1-SNAPSHOT.jar"] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any benefit of running java through "sh" and not directly using ENTRYPOINT ["java", "-jar", "/app/your-application.jar"] ? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| version: '3.8' | ||
|
|
||
| services: | ||
| mysql: | ||
| image: mysql:8.0 | ||
| container_name: mysql-cont | ||
| env_file: | ||
| - .env | ||
| ports: | ||
| - "3307:3306" | ||
| volumes: | ||
| - db_data:/var/lib/mysql | ||
|
|
||
| app: | ||
| build: . | ||
| container_name: userinfoapp | ||
| env_file: | ||
| - .env | ||
| ports: | ||
| - "8080:8080" | ||
| depends_on: | ||
| - mysql | ||
|
|
||
| volumes: | ||
| db_data: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| package com.docker.rihal; | ||
|
|
||
| import io.github.cdimascio.dotenv.Dotenv; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class RihalApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(RihalApplication.class, args); | ||
| } | ||
| public static void main(String[] args) { | ||
| // Load environment variables from .env file | ||
| Dotenv dotenv = Dotenv.load(); | ||
| System.setProperty("SPRING_DATASOURCE_URL", dotenv.get("SPRING_DATASOURCE_URL")); | ||
| System.setProperty("SPRING_DATASOURCE_USERNAME", dotenv.get("SPRING_DATASOURCE_USERNAME")); | ||
| System.setProperty("SPRING_DATASOURCE_PASSWORD", dotenv.get("SPRING_DATASOURCE_PASSWORD")); | ||
| System.setProperty("SPRING_JPA_HIBERNATE_DDL_AUTO", dotenv.get("SPRING_JPA_HIBERNATE_DDL_AUTO")); | ||
| System.setProperty("SPRING_JPA_SHOW_SQL", dotenv.get("SPRING_JPA_SHOW_SQL")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really needed ? |
||
|
|
||
| SpringApplication.run(RihalApplication.class, args); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| spring.application.name=rihal | ||
| spring.datasource.url=${SPRING_DATASOURCE_URL} | ||
| spring.datasource.username=${SPRING_DATASOURCE_USERNAME} | ||
| spring.datasource.password=${SPRING_DATASOURCE_PASSWORD} | ||
| spring.jpa.hibernate.ddl-auto=update | ||
| spring.jpa.show-sql=true | ||
| spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect | ||
| spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <!DOCTYPE html> | ||
| <html xmlns:th="http://www.thymeleaf.org"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Edit User</title> | ||
| </head> | ||
| <body> | ||
| <h1>Edit User</h1> | ||
| <form action="#" th:action="@{/users/{id}(id=${user.id})}" th:object="${user}" method="post"> | ||
| <label>Name: </label> | ||
| <input type="text" th:field="*{name}" /><br> | ||
| <label>Email: </label> | ||
| <input type="email" th:field="*{email}" /><br> | ||
| <button type="submit">Save</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <!DOCTYPE html> | ||
| <html xmlns:th="http://www.thymeleaf.org"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>User Management</title> | ||
| </head> | ||
| <body> | ||
| <h1>User Management</h1> | ||
| <a href="/users/new">Add New User</a> | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th>ID</th> | ||
| <th>Name</th> | ||
| <th>Email</th> | ||
| <th>Actions</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <tr th:each="user : ${users}"> | ||
| <td th:text="${user.id}"></td> | ||
| <td th:text="${user.name}"></td> | ||
| <td th:text="${user.email}"></td> | ||
| <td> | ||
| <a th:href="@{/users/edit/{id}(id=${user.id})}">Edit</a> | ||
| <a th:href="@{/users/delete/{id}(id=${user.id})}">Delete</a> | ||
| </td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <!DOCTYPE html> | ||
| <html xmlns:th="http://www.thymeleaf.org"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Add New User</title> | ||
| </head> | ||
| <body> | ||
| <h1>Add New User</h1> | ||
| <form action="#" th:action="@{/users}" th:object="${user}" method="post"> | ||
| <label>Name: </label> | ||
| <input type="text" th:field="*{name}" /><br> | ||
| <label>Email: </label> | ||
| <input type="email" th:field="*{email}" /><br> | ||
| <button type="submit">Save</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| artifactId=rihal | ||
| groupId=com.docker | ||
| version=0.0.1-SNAPSHOT |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| com\docker\rihal\repositories\UserRepository.class | ||
| com\docker\rihal\services\UserService.class | ||
| com\docker\rihal\controllers\UserController.class | ||
| com\docker\rihal\RihalApplication.class | ||
| com\docker\rihal\models\User.class |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| C:\Users\EtharAlAasmi\TrainingChallenge\UserInfoStoreApp\src\main\java\com\docker\rihal\controllers\UserController.java | ||
| C:\Users\EtharAlAasmi\TrainingChallenge\UserInfoStoreApp\src\main\java\com\docker\rihal\models\User.java | ||
| C:\Users\EtharAlAasmi\TrainingChallenge\UserInfoStoreApp\src\main\java\com\docker\rihal\repositories\UserRepository.java | ||
| C:\Users\EtharAlAasmi\TrainingChallenge\UserInfoStoreApp\src\main\java\com\docker\rihal\RihalApplication.java | ||
| C:\Users\EtharAlAasmi\TrainingChallenge\UserInfoStoreApp\src\main\java\com\docker\rihal\services\UserService.java |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For review purposes it's fine, but we should never push the changes with a .env file. The reason is that, this .env is environment specific. On our PC there might be a MySQL server running locally with a different username and password.
Similarly, Another team member may spin up a container and point to a MySQL server running on cloud.