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 for this project.
- An IDE of your choice
- Java 11
Section 1: Lets Start from start.spring.io
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
or your GUI tool installed on you computer.
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
If you want to test the endpoint, open a browser and visit, http://localhost:8080/hello.
Section 2: Adding Docker to Spring Boot Application
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
Time to see the output from container. Head to your browser and hit http://localhost:8080/hello.
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.