-
Notifications
You must be signed in to change notification settings - Fork 16
Containerized App and DB #3
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
Open
medd911
wants to merge
2
commits into
CodelineAtyab:main
Choose a base branch
from
medd911:ahmed-alafifi-containerize-app
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,24 @@ | ||
| # Compiled class file | ||
| *.class | ||
|
|
||
| # Log file | ||
| *.log | ||
|
|
||
| # BlueJ files | ||
| *.ctxt | ||
|
|
||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
|
|
||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
| replay_pid* | ||
| # Compiled class file | ||
| *.class | ||
| # Log file | ||
| *.log | ||
| # BlueJ files | ||
| *.ctxt | ||
| # Mobile Tools for Java (J2ME) | ||
| .mtj.tmp/ | ||
| # Package Files # | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
| *.zip | ||
| *.tar.gz | ||
| *.rar | ||
| # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
| hs_err_pid* | ||
| replay_pid* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Use the Maven image with OpenJDK 17 to build the application | ||
| FROM maven:3.8.1-openjdk-17 AS build | ||
|
|
||
| # Set the working directory to /app | ||
| WORKDIR /app | ||
|
|
||
| # Copy the pom.xml and the source code to the container | ||
| COPY pom.xml /app/ | ||
| COPY src /app/src | ||
|
|
||
| # Package the application without running tests | ||
| RUN mvn clean package -DskipTests | ||
|
|
||
| # Use a lightweight OpenJDK image to run the application | ||
| FROM openjdk:17-jdk-slim | ||
|
|
||
| # Set the working directory to /app | ||
| WORKDIR /app | ||
|
|
||
| # Copy the jar file from the build stage | ||
| COPY --from=build /app/target/rihal-0.0.1-SNAPSHOT.jar /app/rihal-0.0.1-SNAPSHOT.jar | ||
|
|
||
| # Expose port 8080 | ||
| EXPOSE 8080 | ||
|
|
||
| # Set environment variables for MySQL connection | ||
| ENV SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3307/usersystem?useSSL=false&serverTimezone=UTC&createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true | ||
| ENV SPRING_DATASOURCE_USERNAME=root | ||
| ENV SPRING_DATASOURCE_PASSWORD=root | ||
| ENV SPRING_JPA_HIBERNATE_DDL_AUTO=update | ||
| ENV SPRING_JPA_SHOW_SQL=true | ||
|
|
||
| # Run the Spring Boot application | ||
| ENTRYPOINT ["sh", "-c", "java -jar /app/rihal-0.0.1-SNAPSHOT.jar || (echo 'Application failed, sleeping indefinitely' && sleep infinity)"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,24 @@ | ||
| This is free and unencumbered software released into the public domain. | ||
|
|
||
| Anyone is free to copy, modify, publish, use, compile, sell, or | ||
| distribute this software, either in source code form or as a compiled | ||
| binary, for any purpose, commercial or non-commercial, and by any | ||
| means. | ||
|
|
||
| In jurisdictions that recognize copyright laws, the author or authors | ||
| of this software dedicate any and all copyright interest in the | ||
| software to the public domain. We make this dedication for the benefit | ||
| of the public at large and to the detriment of our heirs and | ||
| successors. We intend this dedication to be an overt act of | ||
| relinquishment in perpetuity of all present and future rights to this | ||
| software under copyright law. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
| OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
| ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| OTHER DEALINGS IN THE SOFTWARE. | ||
|
|
||
| For more information, please refer to <https://unlicense.org> | ||
| This is free and unencumbered software released into the public domain. | ||
| Anyone is free to copy, modify, publish, use, compile, sell, or | ||
| distribute this software, either in source code form or as a compiled | ||
| binary, for any purpose, commercial or non-commercial, and by any | ||
| means. | ||
| In jurisdictions that recognize copyright laws, the author or authors | ||
| of this software dedicate any and all copyright interest in the | ||
| software to the public domain. We make this dedication for the benefit | ||
| of the public at large and to the detriment of our heirs and | ||
| successors. We intend this dedication to be an overt act of | ||
| relinquishment in perpetuity of all present and future rights to this | ||
| software under copyright law. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
| IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
| OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
| ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| OTHER DEALINGS IN THE SOFTWARE. | ||
| For more information, please refer to <https://unlicense.org> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,131 +1,131 @@ | ||
| # User Information Store - Spring Boot MVC Application with MySQL | ||
|
|
||
| This repository contains a Spring Boot MVC application that stores user information in a MySQL database. | ||
|
|
||
| This guide will help you fork the repository, and provide you steps to manually run this application. | ||
|
|
||
| These manual steps can be used to write a Dockerfile for this application along with a docker-compose.yaml, and run both application and database containers locally by just using one command. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following installed on your machine: | ||
|
|
||
| - [Docker](https://docs.docker.com/get-docker/) | ||
| - [Docker Compose](https://docs.docker.com/compose/install/) | ||
| - [Git](https://git-scm.com/) | ||
|
|
||
| ## Getting Started | ||
|
|
||
| ### 1. Fork the Repository | ||
|
|
||
| 1. Go to the original repository: [Link of Repo](https://github.com/CodelineAtyab/UserInfoStoreApp) | ||
| 2. Click the `Fork` button on the top right to create your own copy of the repository. | ||
|
|
||
| Note: Please watch [this youtube video](https://www.youtube.com/watch?v=CML6vfKjQss) if you want to know just the submission process. Its the same as contributing to an open-source project. | ||
|
|
||
| ### 2. Clone Your Forked Repository | ||
|
|
||
| 1. Clone your forked repository. You can simply do it by clicking on `Code` and copying the HTTPS URL. | ||
| 2. Now simply clone the repo on your PC. Command will be like `git clone [URL]` where URL is the one, you got from step 1. | ||
|
|
||
| ### 3. Create a New Branch | ||
| Before implementing anything, make sure you are on a new branch. | ||
|
|
||
| You can create a new branch and switch to it by running: | ||
| ```bash | ||
| git checkout -b your-name-containerize-app | ||
| ``` | ||
| Please replace `your-name` with your actual name in lower case and without any spaces. | ||
|
|
||
| ### 4. Create Dockerfile for Spring Boot Application | ||
| You have to create Dockerfile for the application at the same location where `pom.xml` is located. | ||
|
|
||
| Here are the manual steps of building and running this application: | ||
|
|
||
| 1. Install `OpenJDK 17` | ||
| 2. Install `maven` | ||
| 3. In the directory where `pom.xml` is located, Run `mvn clean package -DskipTests` | ||
| 4. A jar file by the name `rihal-0.0.1-SNAPSHOT.jar` will be generated in `./target` directory. This target directory will be created as a result of command run in step 3. | ||
| 5. Now navigate to the target directory by `cd target` | ||
| 6. Before running the application, make sure that MySQL server is running with the right configuration. Here is an example of `docker run` command to run a MySQL container with the right configuration: | ||
|
|
||
| ```bash | ||
| docker run --name mysql-cont -p 3307:3307 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=usersystem -e MYSQL_PASSWORD=root -e MYSQL_TCP_PORT=3307 mysql:9.0.0 | ||
| ``` | ||
|
|
||
| 7. Verify that the DB server is accessible on port `3307` by connecting it using DBVisualizer or any other DB Client Application. | ||
|
|
||
| 8. Now set the following environment variables before running the java application: | ||
| ```bash | ||
| SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3307/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" | ||
| ``` | ||
|
|
||
| 9. Run the application using the following command: `java -jar rihal-0.0.1-SNAPSHOT.jar` | ||
|
|
||
| 10. Test the application in the browser by navigating to http://localhost:8080/ | ||
|
|
||
| 11. Make sure the data is save in the DB by creating some users, updating any specific user and deleting one user. If we restart the application, the data should remain as is. | ||
|
|
||
| #### Some important things to note: | ||
|
|
||
| - If you want, you can skip the installation of OpenJDK 17 and maven by simply using this Docker base image in your Dockerfile: `FROM maven:3.8.1-openjdk-17 AS build` | ||
|
|
||
| - The above image will give you OpenJDK and Maven, and now any `java` and `mvn` commands can be run. | ||
|
|
||
| - You can simply write the Dockerfile by looking at the commands mentioned in steps 3 and 8. This will let you run the application within a container without the need to install java and maven on your PC. | ||
|
|
||
| ### 5. Create docker-compose.yml | ||
|
|
||
| - Create a docker-compose.yml file in the root directory. | ||
|
|
||
| - Make sure to specify a volume for MySQL Database, so the data can be stored on your local machine. This is useful for backing up the data in case the container is deleted. | ||
|
|
||
| - This is the container's path `/var/lib/mysql` that should be mounted to a volume. | ||
|
|
||
| - Make sure, all environment variables are set for the Java application as well as the Database. | ||
|
|
||
| - Make sure the correct port mapping is specified for both applications. | ||
|
|
||
| - Containers of both applications should be part of the same internal network. | ||
|
|
||
|
|
||
| ### 6. Build and Run the Containers | ||
| Run the following command to build and start the containers: | ||
| ```bash | ||
| docker-compose up --build | ||
| ``` | ||
| This command will: | ||
|
|
||
| 1. Build the Docker image for the Spring Boot application. | ||
| 2. Start the MySQL container and initialize the database. | ||
| 3. Start the Spring Boot application container. | ||
|
|
||
| ### 7. Access the Application | ||
| Once the containers are up and running, you can access the application at `http://localhost:8080` | ||
|
|
||
| ### 8. Stopping the Containers | ||
| ```bash | ||
| docker-compose down | ||
| ``` | ||
|
|
||
| ### 9. Steps for submitting the solution | ||
| Once you have verified that the application can be accessed after running `docker-compose up`. Please push all of the changes by running the following commands: | ||
|
|
||
| 1. `git add *` | ||
| 2. `git commit -m "Added dockerfile and docker-compose"` | ||
| 3. `git push` | ||
|
|
||
| Now, goto to your browser and refresh the page for your forked github repository and do the following: | ||
|
|
||
| 1. Click on a green button that displays: **Compare & pull request** | ||
| 2. Put something understandable in the title like **Containerized App and DB** | ||
| 3. In the description, just briefly describe, how you came up with this solution having a Docker and Docker Compose file. | ||
| 4. Click on **Create pull request** | ||
| 5. Please Send a message on slack in **general** channel with the link of your Pull Request and ask for code reviews. | ||
|
|
||
| Thats it ! | ||
| Best of luck | ||
| # User Information Store - Spring Boot MVC Application with MySQL | ||
| This repository contains a Spring Boot MVC application that stores user information in a MySQL database. | ||
| This guide will help you fork the repository, and provide you steps to manually run this application. | ||
| These manual steps can be used to write a Dockerfile for this application along with a docker-compose.yaml, and run both application and database containers locally by just using one command. | ||
| ## Prerequisites | ||
| Before you begin, ensure you have the following installed on your machine: | ||
| - [Docker](https://docs.docker.com/get-docker/) | ||
| - [Docker Compose](https://docs.docker.com/compose/install/) | ||
| - [Git](https://git-scm.com/) | ||
| ## Getting Started | ||
| ### 1. Fork the Repository | ||
| 1. Go to the original repository: [Link of Repo](https://github.com/CodelineAtyab/UserInfoStoreApp) | ||
| 2. Click the `Fork` button on the top right to create your own copy of the repository. | ||
| Note: Please watch [this youtube video](https://www.youtube.com/watch?v=CML6vfKjQss) if you want to know just the submission process. Its the same as contributing to an open-source project. | ||
| ### 2. Clone Your Forked Repository | ||
| 1. Clone your forked repository. You can simply do it by clicking on `Code` and copying the HTTPS URL. | ||
| 2. Now simply clone the repo on your PC. Command will be like `git clone [URL]` where URL is the one, you got from step 1. | ||
| ### 3. Create a New Branch | ||
| Before implementing anything, make sure you are on a new branch. | ||
| You can create a new branch and switch to it by running: | ||
| ```bash | ||
| git checkout -b your-name-containerize-app | ||
| ``` | ||
| Please replace `your-name` with your actual name in lower case and without any spaces. | ||
| ### 4. Create Dockerfile for Spring Boot Application | ||
| You have to create Dockerfile for the application at the same location where `pom.xml` is located. | ||
| Here are the manual steps of building and running this application: | ||
| 1. Install `OpenJDK 17` | ||
| 2. Install `maven` | ||
| 3. In the directory where `pom.xml` is located, Run `mvn clean package -DskipTests` | ||
| 4. A jar file by the name `rihal-0.0.1-SNAPSHOT.jar` will be generated in `./target` directory. This target directory will be created as a result of command run in step 3. | ||
| 5. Now navigate to the target directory by `cd target` | ||
| 6. Before running the application, make sure that MySQL server is running with the right configuration. Here is an example of `docker run` command to run a MySQL container with the right configuration: | ||
| ```bash | ||
| docker run --name mysql-cont -p 3307:3307 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=usersystem -e MYSQL_PASSWORD=root -e MYSQL_TCP_PORT=3307 mysql:9.0.0 | ||
| ``` | ||
| 7. Verify that the DB server is accessible on port `3307` by connecting it using DBVisualizer or any other DB Client Application. | ||
| 8. Now set the following environment variables before running the java application: | ||
| ```bash | ||
| SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3307/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" | ||
| ``` | ||
| 9. Run the application using the following command: `java -jar rihal-0.0.1-SNAPSHOT.jar` | ||
| 10. Test the application in the browser by navigating to http://localhost:8080/ | ||
| 11. Make sure the data is save in the DB by creating some users, updating any specific user and deleting one user. If we restart the application, the data should remain as is. | ||
| #### Some important things to note: | ||
| - If you want, you can skip the installation of OpenJDK 17 and maven by simply using this Docker base image in your Dockerfile: `FROM maven:3.8.1-openjdk-17 AS build` | ||
| - The above image will give you OpenJDK and Maven, and now any `java` and `mvn` commands can be run. | ||
| - You can simply write the Dockerfile by looking at the commands mentioned in steps 3 and 8. This will let you run the application within a container without the need to install java and maven on your PC. | ||
| ### 5. Create docker-compose.yml | ||
| - Create a docker-compose.yml file in the root directory. | ||
| - Make sure to specify a volume for MySQL Database, so the data can be stored on your local machine. This is useful for backing up the data in case the container is deleted. | ||
| - This is the container's path `/var/lib/mysql` that should be mounted to a volume. | ||
| - Make sure, all environment variables are set for the Java application as well as the Database. | ||
| - Make sure the correct port mapping is specified for both applications. | ||
| - Containers of both applications should be part of the same internal network. | ||
| ### 6. Build and Run the Containers | ||
| Run the following command to build and start the containers: | ||
| ```bash | ||
| docker-compose up --build | ||
| ``` | ||
| This command will: | ||
| 1. Build the Docker image for the Spring Boot application. | ||
| 2. Start the MySQL container and initialize the database. | ||
| 3. Start the Spring Boot application container. | ||
| ### 7. Access the Application | ||
| Once the containers are up and running, you can access the application at `http://localhost:8080` | ||
| ### 8. Stopping the Containers | ||
| ```bash | ||
| docker-compose down | ||
| ``` | ||
| ### 9. Steps for submitting the solution | ||
| Once you have verified that the application can be accessed after running `docker-compose up`. Please push all of the changes by running the following commands: | ||
| 1. `git add *` | ||
| 2. `git commit -m "Added dockerfile and docker-compose"` | ||
| 3. `git push` | ||
| Now, goto to your browser and refresh the page for your forked github repository and do the following: | ||
| 1. Click on a green button that displays: **Compare & pull request** | ||
| 2. Put something understandable in the title like **Containerized App and DB** | ||
| 3. In the description, just briefly describe, how you came up with this solution having a Docker and Docker Compose file. | ||
| 4. Click on **Create pull request** | ||
| 5. Please Send a message on slack in **general** channel with the link of your Pull Request and ask for code reviews. | ||
| Thats it ! | ||
| Best of luck |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Usually we want a container to stop if Java application fails to run. In this case if the application doesn't run then the container would still be up which might cause confusion until someone looks at the logs.
Please share some thoughts about this ?