Docker Setup of Maven- Java Spring-boot — MySQL service.
===== Complete Guide Setup Docker Maven — Java — Spring-boot Application with MySQL Database ===
This guideline is for those who have install Docker to their environment.
Let’s define the Environmental Variables for your application.
Java -11
Maven 3.6
Application Port — 6070
MySQL — 8
MySQL Port — 4406
** Setting up MySQL ***
1. Dump your database to a file — ‘your_db_name.sql’
2. Put it inside a folder that you prefer — ‘/root/dumps/’
3. Create a network for your application. This network will include database as one container and your application as another container.
docker network create employee_management_service
4. Run the following command on CLI. This will download the mysql official docker image, run it as a container and initialize the mysql environment with the relevant parameters.
docker run — name employee_management_mysql -d -p 4406:4406 -e MYSQL_ROOT_PASSWORD=shashika1234 -e MYSQL_DATABASE=employee_profile_management -v mysql:/var/lib/mysql — network employee_profile_management_service mysql:8
employee_management_mysql = local container name
4406:4406 — container mysql running port 4406 (exposed port) is connected to the host’s (server machine) 4406 port
shashika1234 — mysql root password
employee_profile_management = database to be used for our application
/var/lib/mysql — volume/directory to be used to save mysql data
mysql:8 — tag of mysql version to be run
After running this, you will see that the docker container is up and running under the name ‘employee_management_mysql’
5. But the db created is empty. Now we need to restore the db that we dumped before.
docker exec -i employee_management_mysql sh -c ‘exec mysql -uroot -p”shashika1234" employee_profile_management’ < /root/backups/employee_profile_management.sql
6.To inspect inside the docker container insert following to cli and run the relevant mysql commands
docker exec -it employee_management_mysql bash
*** Setting up spring-boot docker ***
1. Change application.properties file
spring.datasource.url=jdbc:mysql://host.docker.internal:4406/employee_profile_management?allowPublicKeyRetrieval=True&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username=root
spring.datasource.password=shashika1234
server.port = 6070
Here, spring.datasource.url=jdbc:mysql://host.docker.internal:4406 may have been configured to jdbc:mysql://127.0.0.1:4406. But in-order to access any database outside spring docker container (our previously set-up database for an example) it must be indicated as ‘host.docker.internal:4406’
2. Create Dockerfile with the following content inside the application root/base folder. Here it is the folder where the pom.xml file is in.
3. Add the following to the Dockerfile
=========================
FROM maven:3.6-jdk-11
EXPOSE 6070
WORKDIR /app
COPY . .
RUN mvn clean install
CMD [“mvn”, “spring-boot:run”]
======================
As mentioned earlier we should use a base image with Maven 3.6 and JDK-11 for this application. Here maven image with tag 3.6-jdk-11 has been used.
The port that should be exposed to the external environment from the docker container.
/app is the directory which can be considered as the root folder for the image. This is where the app source code and other relevant files are copied.
COPY . . — This will copy all the files from the current project folder in our local computer to image root folder.
mvn clean install — This command will run once you build the docker image. The state after the run command will be committed to the docker image. (the target folder will be resolved at the time of docker image generation.)
CMD [“mvn”, “spring-boot:run”] — This will run after every time the container is start and running from its CLI.
3. Now run the build image command
docker build — platform linux/amd64 -t shashikachamod/employee_management_service:XXPkUL5RrR .
Here shashikachamod — is the account holder name of docker hub
employee_management_service — is the repository that we intend to push our docker image
XXPkUL5RrR — is the tag of this particular to be identified the correct image once we run the container.
This will create the docker image in your local computer.
4. Now run the following command to push it to the relevant repository
docker push shashikachamod/employee_management_service:XXPkUL5RrR
5. Now you can use it anywhere.
6. To run a container from the image.
docker run -p 6070:6070 -t shashikachamod/employee_management_service:XXPkUL5RrR
6070 first- host port
6070 second — docker container port
7. Run the following to access the container via CLI
docker exec -it shashikachamod/employee_management_service:XXPkUL5RrR bash