Simple Spring boot application with docker deployment

Creating a simple Spring Boot application and Docker deployment. 

This documentation illustrates the simple ways of deploying a spring boot application on a docker container.  This application is just a simple one with no DB interaction. We will cover the deployment of Spring boot application with DB in other post 

PreRequisite

We need to have the following softwares. 

Operating System :Window/Linux (CENTOS-7 is used during this course)
Container Docker (Should be installed) 
Application Spring boot application (CRUD Operation)

Application

We have a sample application named billing and is on Spring boot. This is a very simple application and does not have any interaction with any other downstream system. 

This application have the following api’s :

APIMethodUsage
\bill\api\createPUTCreate a new objects
\bill\api\updatePOSTUpdate the existing objects
\bill\api\getGETFetch all the objects
\bill\api\get\{id}GETFetch the specified objects

We will check these API after the deployment on docker. 

Technical Details:

Spring boot Project: 

We have a sample application on Springboot, which is a simple application with no interaction with downstream systems like DB. The package structure for this project looks like the below. 

9XK5p9VhYj HrBaoPfr9RN0Vfg4mXI3Ox0g71OnhMQWC6BI3QzlzeHQa4zyKy7g0bEZIf4PFS TpL21l6IgtAOAUQG9hZ7Tc07obrhLZjb TNR4 Ubh50hF0BdI4KUpOyhpHol4TdF7snVRWyOjG0i7b1mk6aKp3GAYspd9CWYkVhknw0Z6EOO5r0tDc
Project Structure

Let’s discuss some files which are related to containerisation. 

Dockerfile: This file is used to create an image for the project. This image is used to deploy on the containerised environment, like docker, rocket etc. This file should be present in the root folder of the project. Let’s check the content of it. 

K9CJwEKbZ3zMWeEoKUdlgmppphjfZG7RlkAG0JQMYrhryZDczVSpVyyRCdCIKaQx5mtlHfhlHaDbKCyCb9C8rYC6o5TLfwFH E0GyAyI980Csfq NpxCSpESk0pdECE873cLH 557ThhYLZiFbnXpBw6ykB88U7 LHsBbuK KwO2LghHerJfEsHMkIw8
Dockerfile content

Lets check few more options of instructions in file. Dockerfie is a file that contains the Instruction and argument and must start with FROM instruction. This is not case sensitive, but the convention is for instructions should be in uppercase.

WORKDIR: This instruction is used to switch to the directory specified. Then this directory is used to copy files, run commands in that container. This also help to create a new directory, which is used for building the image, copy code and jar file from local system. Example: WORKDIR /opt/apps

ENV: This is used to set the environment variable of docker image. with the help of this attribute we can override the values exposed as environment variable. Example: ENV variableName=variableValue

COPY: This instruction is used to copy from host system to container. Example COPY source destination

ADD: This instruction also help to copy the files into container from host system. It is just like a copy instruction with one more benefit, that it can copy the files from remote locations as well. You can define any URL as source. So it fetch the file from remote and copy it to container.

RUN: This instruction is used to run the command on container. Example: RUN ls -ltr RUN chown user:group dir

ENTRYPOINT: This instruction is used to configure the container that will run as an executable. There could be multiple command can be added as an instruction, or any shell script to execute. Example: ENTRYPOINT [“executable”, “param1”, “param2”, … , “paramN”]

Building Image

Let’s build the image of a billing application.  Command: docker build -t <USER_ID>/<IMAGE_NAME> .  Example: docker build -t pandeych009/itlogiclab-bill-image .

Note

  • User id is not required, simple image name will suffice. This is included as if you have a repository in docker hub and you want to push this image to the repository. 
  • Do not forget to add a dot (.) at the end. 

The above command produces the following result. 

IjAnrxWhfAHqPTKVf4Fq RNU1gqRNWi Y P5QrDI5t2itU5kaFQporGK8FUban 4jJ5QVAeY9cTb2eRVgqDbG92TElLvNSsBtbYBNWb0iFSAmVuYfwFq4uCytPIS3Qjz6xVrM5hHFXTbcgfrp89InKAnqpxRKh1XBksTbdAjTTyOXdCNS93gbtrlb01x

There are some other flavours of the build command. 

  • If we have to provide a Dockerfile then the following command would work. 
    • docker build – < Dockerfile
    • docker build . -f Dockerfile -t <IMAGE-NAME>

Running application on Container

Let’s start to run the application with the use of docker commands. Let’s check the simple command first.  Command: docker run <image-name>  <options> Example: docker run -p localhost:80:20002 -it pandeych009/itlogiclab-bill-image

LUCITIeTlxyGplYaTn2kMh7gxMMlpmk8CTM66G2klldfjCM4d4Cb 2cqWG2Yn GJZTyFpTkFzO9cLCGLwxwrcUA3tRwn8H5N5CsZjBAU2ZH2PLTB9qKgYJu3JFX0mfahKj9yW5oST 6HnCDftmjBrE7ryUvh46jv4rZk Z1DA9OC8V7Zru3qaMbDGvpe

We have used few options here and they are used for 

OptionsDescriptionExplanation
.
-i  –interactiveThis operation termin
-t –ttyAllocate a pseudo TTY
-p –publish0.0.0.0:80:20002This option exposes the container’s port to the host port. 
Application starts at the PORT 20002 at the container. 
This port is accessible for the client on 80 port of the host for all the IP’s
Options for Container

With this the container starts successfully. Let’s check the status of it. 

Validate container state: 

To validate the container we need to run the following command.

Command: docker ps

U33re YSrozJ0AGB4CUSmr3rk4 DUKf6KOfIldgGRhvIJV X9QwKPAqcUBUdi5 IdDDLPlYyGEF94cK44fadxA0EMCp9HJK7dV9En UgbTStBdKuq JnjjZkyQce 5xgqKqW Elyq6jIxDm3d1RrDiR9PWKUIfg57eAXIml5Qr7L6b2gXwAoZXhRSo8p

Test Application on container

Fetch Record (GET Operation)

To test the container we need to fire the Curl command in terminal, or we can use postman as well 

API Call : http:\\localhost:80\bill\api\get Command: Use Curl command as curl http:\\localhost:80\bill\api\get

With this command we will get the following output: 

E0Q8ZIygbgRI8nNpnWhuwwcwI598AHZPRL6Job22KiMCrdPLOKCKojIUpVZKWwiPpDMG0rdwqpOuaMBynteZi9Jw823kuOYfPAmYhSB5w7JLz1LCceaAgbeIlzxac 0Go65II1GRkwJjs0gv1RYoJ7JE4Y9VnsapMH7O0L6lQSp QNtcywIJ2RFrL3ge

Add Record (PUT Operation)

API Call: http://localhost:80/bill/api/add Command: Use Curl command as mention below.

curl -X PUT -H “Content-Type: application/json” -d ‘{“customerId”:20003,”invoiceNo”:”Invoice_937082346397587239″,”billAmount”:9.3708236E17,”date”:”2022-10-09T07:16:31.635Z”,”orderId”:937082346397607240,”amount”:937082346397607241,”status”:”PENDING”,”billInfoList”:[{“billInfoId”:-319915949760543233,”invoiceNo”:null,”chargeName”:”My_Charge_-319915949760543233″,”chargeCost”:-1.59957982E17},{“billInfoId”:-7339925261522476103,”invoiceNo”:null,”chargeName”:”My_Charge_-7339925261522476103″,”chargeCost”:-3.66996256E18},{“billInfoId”:-4495016224261477917,”invoiceNo”:null,”chargeName”:”My_Charge_-4495016224261477917″,”chargeCost”:-2.24750814E18},{“billInfoId”:2432793819155524555,”invoiceNo”:null,”chargeName”:”My_Charge_2432793819155524555″,”chargeCost”:1.21639686E18},{“billInfoId”:-4476846372823844252,”invoiceNo”:null,”chargeName”:”My_Charge_-4476846372823844252″,”chargeCost”:-2.23842316E18},{“billInfoId”:2797949467354393091,”invoiceNo”:null,”chargeName”:”My_Charge_2797949467354393091″,”chargeCost”:1.39897475E18},{“billInfoId”:-6582284907281397716,”invoiceNo”:null,”chargeName”:”My_Charge_-6582284907281397716″,”chargeCost”:-3.29114232E18},{“billInfoId”:360364351120203589,”invoiceNo”:null,”chargeName”:”My_Charge_360364351120203589″,”chargeCost”:1.80182176E17},{“billInfoId”:1523579306349991233,”invoiceNo”:null,”chargeName”:”My_Charge_1523579306349991233″,”chargeCost”:7.6178962E17},{“billInfoId”:-2167995517030885175,”invoiceNo”:null,”chargeName”:”My_Charge_-2167995517030885175″,”chargeCost”:-1.08399772E18}]}’ http://localhost:80/bill/api/add

Update Record (Post Operation)

API Call: http://localhost:80/bill/api/update Command: Use Curl command as mention below.

curl -X PUT -H “Content-Type: application/json” -d ‘{“customerId”:20003,”invoiceNo”:”Invoice_937082346397587239″,”billAmount”:9.3708236E17,”date”:”2022-10-09T07:16:31.635Z”,”orderId”:937082346397607240,”amount”:937082346397607241,”status”:”PENDING”,”billInfoList”:[{“billInfoId”:-319915949760543233,”invoiceNo”:null,”chargeName”:”My_Charge_-319915949760543233″,”chargeCost”:-1.59957982E17},{“billInfoId”:-7339925261522476103,”invoiceNo”:null,”chargeName”:”My_Charge_-7339925261522476103″,”chargeCost”:-3.66996256E18},{“billInfoId”:-4495016224261477917,”invoiceNo”:null,”chargeName”:”My_Charge_-4495016224261477917″,”chargeCost”:-2.24750814E18},{“billInfoId”:2432793819155524555,”invoiceNo”:null,”chargeName”:”My_Charge_2432793819155524555″,”chargeCost”:1.21639686E18},{“billInfoId”:-4476846372823844252,”invoiceNo”:null,”chargeName”:”My_Charge_-4476846372823844252″,”chargeCost”:-2.23842316E18},{“billInfoId”:2797949467354393091,”invoiceNo”:null,”chargeName”:”My_Charge_2797949467354393091″,”chargeCost”:1.39897475E18},{“billInfoId”:-6582284907281397716,”invoiceNo”:null,”chargeName”:”My_Charge_-6582284907281397716″,”chargeCost”:-3.29114232E18},{“billInfoId”:360364351120203589,”invoiceNo”:null,”chargeName”:”My_Charge_360364351120203589″,”chargeCost”:1.80182176E17},{“billInfoId”:1523579306349991233,”invoiceNo”:null,”chargeName”:”My_Charge_1523579306349991233″,”chargeCost”:7.6178962E17},{“billInfoId”:-2167995517030885175,”invoiceNo”:null,”chargeName”:”My_Charge_-2167995517030885175″,”chargeCost”:-1.08399772E18}]}’ http://localhost:80/bill/api/add

Docker Compose:

Docker compose is a tool, which help to configure the deployment in a single configuration file. Consider an example if we have a complex application that interact with the multiple downstream system. If we deploy the system by running the docker commands makes this deployment tedious and cumbersome. We will see more details of docker compose in our upcoming blog.

Conclusion

Very basic steps for the beginner, how to deploy the application into the local docker environment. We will publish the another knowledge base, where this springboot application will interact with the mysql database and we see how to deploy this.

, ,