Developing Spring Boot Application using Docker - Part 1
Recently I have been stormed by the now old buzz word Docker. I have been reading a lot about Docker for quite some time and found some abundant resources out there on internet. I am planning to add more to the series in my later posts. I will be posing my blogs as and when I am learning and discovering something new in my journey. It's never too late to start something is what I believe. Lets get started.
Prerequisites
Prerequisites for this project.
- An IDE of your choice
- Java 11
- Docker
Section 1: Lets Start from start.spring.io
If you already have a working Spring Boot Application, head to Section 2.
We can build the Spring Boot Project by just visiting the below link.
URL to generate Simple Spring Boot App on start.spting.io
This should get you started with a typical Spring Boot Application
Now lets download and extract the project to workspace. Extract files using
Now it is time for adding a simple Hello World Controller. Add new Controller as shown.
To ensure the Controller looks good, run the following command.
Now that you have a Dockerfile, lets create a docker-compose.yml.
Before heading to create a Container, we will have to generate the artifact to be shipped into the container.
The new packaged jar file should be located inside your project directory/build/libs.
Here is the command to run the docker/compose with –build option.
Starting the application with –build option, will be able to recreate containers every time.
We wanted that feature so that when we make changes to the jar file it is automatically picked.
Our Spring Boot App finally runs inside the container. Now, it is time to add stop our container.
To stop our running container run the following:
URL to generate Simple Spring Boot App on start.spting.io
This should get you started with a typical Spring Boot Application
Now lets download and extract the project to workspace. Extract files using
$ unzip -d ~/your/fav/location/ docker-example.zip
Now it is time for adding a simple Hello World Controller. Add new Controller as shown.
To ensure the Controller looks good, run the following command.
$ ./gradlew clean assemble bootRun
Now that you have a working application lets containerize it.
Section 2: Adding Docker to Spring Boot Application
Now that we have a simple Spring Boot Application running, it is time to get the Docker files added to the project and have that run.
Before we could add our Dockerfile we need to add the following to our gradle.build file.
This will ensure the generated artifact is given a name that can be used by docker to create the container image.
Create a Dockerfile as follows.
Now that you have a Dockerfile, lets create a docker-compose.yml.
Before heading to create a Container, we will have to generate the artifact to be shipped into the container.
Lets build our artifact using the Gradle command
$ ./gradlew clean assemble
The new packaged jar file should be located inside your project directory/build/libs.
Here is the command to run the docker/compose with –build option.
Starting the application with –build option, will be able to recreate containers every time.
We wanted that feature so that when we make changes to the jar file it is automatically picked.
$ docker-compose up -d --build
Our Spring Boot App finally runs inside the container. Now, it is time to add stop our container.
To stop our running container run the following:
$ docker-compose down
Recap of what we just did
- When we ran the Gradle task, we created the Spring Boot jar file that we wanted to deploy inside the container.
- When we added the Dockerfile, it had the details like what image we need to start with
- FROM adoptopenjdk:11-jdk: This specifies we need to download the Java 11 Image for building our container/image.
- EXPOSE 8080: We are exposing port 8080 to be accessed outside the container.
- ADD build/libs/docker-example.jar /app/: This copies the generated artifact into the container/image.
- WORKDIR /app: Before running any command, we are changing directory into the working application folder.
- CMD java -jar docker-example.jar: This runs the Spring Boot application as Java process.
- When we added docker-compose.yml it had details on how to start the Dockerfile and start the container
- version: "3": Specify the version of the composer.
- services.docker-example.build.context: .: This specifies the context for the docker to be executed.
- services.docker-example.build.dockerfile: Dockerfile: Mention the Docker file that needs to be executed.
- services.docker-example.ports: - 8080:8080: Exposing the docker port on local computer.
Next Stage
In our next stage we can use our IDE and debug application that is running inside the container.
The code for this blog can be found here: https://github.com/reflexdemon/docker-example/tree/part-1
If you liked the blog, poke me on my Twitter @reflexdemon
Comments
Post a Comment