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.
Before you begin, ensure you have the following installed on your machine:
- Go to the original repository: Link of Repo
- Click the
Fork
button on the top right to create your own copy of the repository.
Note: Please watch this youtube video if you want to know just the submission process. Its the same as contributing to an open-source project.
- Clone your forked repository. You can simply do it by clicking on
Code
and copying the HTTPS URL. - 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.
Before implementing anything, make sure you are on a new branch.
You can create a new branch and switch to it by running:
git checkout -b your-name-containerize-app
Please replace your-name
with your actual name in lower case and without any spaces.
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:
- Install
OpenJDK 17
- Install
maven
- In the directory where
pom.xml
is located, Runmvn clean package -DskipTests
- 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. - Now navigate to the target directory by
cd target
- 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:
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
-
Verify that the DB server is accessible on port
3307
by connecting it using DBVisualizer or any other DB Client Application. -
Now set the following environment variables before running the java application:
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"
-
Run the application using the following command:
java -jar rihal-0.0.1-SNAPSHOT.jar
-
Test the application in the browser by navigating to http://localhost:8080/
-
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.
-
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
andmvn
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.
-
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.
Run the following command to build and start the containers:
docker-compose up --build
This command will:
- Build the Docker image for the Spring Boot application.
- Start the MySQL container and initialize the database.
- Start the Spring Boot application container.
Once the containers are up and running, you can access the application at http://localhost:8080
docker-compose down
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:
git add *
git commit -m "Added dockerfile and docker-compose"
git push
Now, goto to your browser and refresh the page for your forked github repository and do the following:
- Click on a green button that displays: Compare & pull request
- Put something understandable in the title like Containerized App and DB
- In the description, just briefly describe, how you came up with this solution having a Docker and Docker Compose file.
- Click on Create pull request
- 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