Developing Spring Boot Application using Docker - Part 3

Introduction

 Now that we are able to run and Debug the application inside the container let us add Database connection for our service. In this part we will be adding Database service to our Docker Compose file and use that inside the application. For the purposes of simplicity we can use MYSQL as backend database.

Get started to build CRUD using Spring Boot

To begin with let us add the below to our gradle file to get the required support for our DB
We can now define our data model to be added.
It is time to create our CRUD repository
Lets add our own Service that can be used by the REST Controller
Lets add our own Rest Service that can be invoked to access our database
With the above code, we should have something to get into our Docker friend.

Get your docker stuff ready

Let us now add the database for us to test the docker compose
This docker compose file has inline comments help you understand what is happening here
If you pay close attention to what has been added, we just added very few lines to create a database container. Also it is worth mentioning that we could potentially persist the database on to local file mounts. This can be useful if you want to maintain the state at local. However, I prefer to create it from beginning so that we can run tests that are repetitive in nature.
Note: We will have to make sure we also update the docker-compose-debug.yml so that we are able to have debugger setting also working.


Let us now add the .env file that is used by docker

Lets get into action

It is time for us to run our code and see the output. Let us compile and make sure the get the jar file built.
$./gradlew clean assemble
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 12s
6 actionable tasks: 6 executed
  
Now let us start the containers
$docker-compose up -d --build
Building docker-example
Step 1/5 : FROM adoptopenjdk:11-jdk
 ---> 20d606fe6719
Step 2/5 : EXPOSE 8080
 ---> Using cache
 ---> c79ca56ce92e
Step 3/5 : ADD build/libs/docker-example.jar /app/
 ---> 764658f0c6e9
Step 4/5 : WORKDIR /app
 ---> Running in 8b81d2e4b841
Removing intermediate container 8b81d2e4b841
 ---> cc880f62d5a2
Step 5/5 : CMD java -jar docker-example.jar
 ---> Running in 6c454a062fc1
Removing intermediate container 6c454a062fc1
 ---> 04f49b6c2592

Successfully built 04f49b6c2592
Successfully tagged docker-example_docker-example:latest
docker-example_db_1 is up-to-date
Recreating docker-example_docker-example_1 ... done

  
Time to test our application
$curl -v http://localhost:8080/employee
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /employee HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 04 Sep 2021 12:17:24 GMT
< 
* Connection #0 to host localhost left intact
[]                       

  
There is no records. Let add couple of employees
$curl -v -X POST http://localhost:8080/employee -d '{"name":"Venkateswara", "department":"IT"}' -H 'Content-Type:application/json'
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /employee HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type:application/json
> Content-Length: 42
> 
* upload completely sent off: 42 out of 42 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 04 Sep 2021 12:20:55 GMT
< 
* Connection #0 to host localhost left intact
{"id":1,"name":"Venkateswara","department":"IT"}                 

  
$curl -v -X POST http://localhost:8080/employee -d '{"name":"Prasanna", "department":"Finance"}' -H 'Content-Type:application/json'
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /employee HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Type:application/json
> Content-Length: 43
> 
* upload completely sent off: 43 out of 43 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 04 Sep 2021 12:24:33 GMT
< 
* Connection #0 to host localhost left intact
{"id":2,"name":"Prasanna","department":"Finance"}                      

  
Now, lets to fetch them all.
$curl -v http://localhost:8080/employee
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /employee HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Sat, 04 Sep 2021 12:26:46 GMT
< 
* Connection #0 to host localhost left intact
[{"id":1,"name":"Venkateswara","department":"IT"},{"id":2,"name":"Prasanna","department":"Finance"}] 

  
Hurry!!! We got them back!

Thanks

We have successfully able to debug code inside docker and able to connect to database. This is a good start for anyone who wants to get started with Docker and Java coding. Happy coding!

The code for this part can be found here: https://github.com/reflexdemon/docker-example/tree/part-3
If you like the blog, poke me on my Twitter @reflexdemon

Comments

Popular posts from this blog

Java SSL/TLS Testing Tool: Cipher Suite

Developing Spring Boot Application using Docker - Part 1

Undocumented JAVA: Web Application: Deploy to the Cloud – Part 1